| 1 | package Media::Convert::Normalizer::Ffmpeg; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | extends 'Media::Convert::Normalizer'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use JSON::MaybeXS qw/decode_json/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Symbol 'gensym'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use IPC::Open3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Media::Convert::CodecMap qw/detect_to_write/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Media::Convert::Normalizer::Ffmpeg - normalize the audio of a video asset using the ffmpeg 'loudnorm' filter | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | Media::Convert::Normalizer::Ffmpeg->new(input => Media::Convert::Asset->new(...), output => Media::Convert::Asset->new(...))->run(); | ||||||||||
| 19 | |||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||
| 21 | |||||||||||
| 22 | C<Media::Convert::Normalizer> is a class to normalize the audio of a given | ||||||||||
| 23 | C<Media::Convert::Asset>. This class is an implementation of the API using | ||||||||||
| 24 | the ffmpeg "loudnorm" filter. | ||||||||||
| 25 | |||||||||||
| 26 | =head1 ATTRIBUTES | ||||||||||
| 27 | |||||||||||
| 28 | C<Media::Convert::Normalizer::Ffmpeg> supports all the attributes of | ||||||||||
| 29 | L<Media::Convert::Normalizer> | ||||||||||
| 30 | |||||||||||
| 31 | =head1 METHODS | ||||||||||
| 32 | |||||||||||
| 33 | =head2 run | ||||||||||
| 34 | |||||||||||
| 35 | Performs the normalization | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=6 SCAR=33.0
Pod
documented
| |||||||||||
| 40 | 1 | my $self = shift; | |||||||||
| 41 | |||||||||||
| 42 | 1 | my $input = $self->input; | |||||||||
| 43 | |||||||||||
| 44 | 1 | my @command = ("ffmpeg", "-y", "-i", $input->url, "-af", "loudnorm=i=-23.0:print_format=json", "-f", "null", "-"); | |||||||||
| 45 | 1 | print "Running: '" . join("' '", @command) . "'\n"; | |||||||||
| 46 | 1 | open3 (my $in, my $out, my $ffmpeg = gensym, @command); | |||||||||
| 47 | 0 | my $json = ""; | |||||||||
| 48 | 0 | my $reading_json = 0; | |||||||||
| 49 | 0 | ▶ | while(<$ffmpeg>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | ▶ | if(/{/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | $reading_json++; | |||||||||
| 52 | } | ||||||||||
| 53 | 0 | ▶ | if($reading_json) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $json .= $_; | |||||||||
| 55 | } | ||||||||||
| 56 | 0 | ▶ | if(/}/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | $reading_json--; | |||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | 0 | $json = decode_json($json); | |||||||||
| 61 | |||||||||||
| 62 | # TODO: abstract filters so they can be applied to an | ||||||||||
| 63 | # Media::Convert::Pipe. Not now. | ||||||||||
| 64 | 0 | my $codec = $self->output->audio_codec; | |||||||||
| 65 | 0 | ▶ | if(!defined($codec)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | $codec = detect_to_write($input->audio_codec); | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | @command = ("ffmpeg", "-loglevel", "warning", "-y", "-i", $input->url, "-af", "loudnorm=i=-23.0:dual_mono=true:measured_i=" . $json->{input_i} . ":measured_tp=" . $json->{input_tp} . ":measured_lra=" . $json->{input_lra} . ":measured_thresh=" . $json->{input_thresh}, "-c:v", "copy", "-c:a", $codec, $self->output->url); | |||||||||
| 69 | 0 | print "Running: '" . join("' '", @command) . "'\n"; | |||||||||
| 70 | 0 | system(@command); | |||||||||
| 71 | } | ||||||||||