| 1 | package Media::Convert::AvSync; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Media::Convert::Asset; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Media::Convert::Pipe; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Media::Convert::Map; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use File::Temp qw/tempdir/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Media::Convert::AvSync - Helper module to fix A/V synchronization issues. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Media::Convert::Asset; | ||||||||||
| 16 | use Media::Convert::AvSync; | ||||||||||
| 17 | |||||||||||
| 18 | my $input = Media::Convert::Asset->new(url => $input_filename); | ||||||||||
| 19 | my $output = Media::Convert::Asset->new(url => $output_filename); | ||||||||||
| 20 | Media::Convert::AvSync->new(input => $input, output => $output, audio_delay => 0.1)->run(); | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | Media::Convert::AvSync is a higher-level helper module to correct | ||||||||||
| 25 | audio/video synchronization issues. | ||||||||||
| 26 | |||||||||||
| 27 | It does this by instructing ffmpeg to read the input asset twice (once | ||||||||||
| 28 | for audio, once for video), and to apply a time offset to the audio | ||||||||||
| 29 | stream. This offset may be negative, in case audio precedes video. | ||||||||||
| 30 | |||||||||||
| 31 | Note that after A/V sync correction, the asset file will be reduced in | ||||||||||
| 32 | length by twice the length of the correction value; this is because | ||||||||||
| 33 | otherwise, in the case of audio preceding video, the resulting media | ||||||||||
| 34 | file would end up with a snippet of audio with no video at the start, | ||||||||||
| 35 | and one of video with no audio at the end (and vice versa for a | ||||||||||
| 36 | correction value for video preceding audio). | ||||||||||
| 37 | |||||||||||
| 38 | =head1 ATTRIBUTES | ||||||||||
| 39 | |||||||||||
| 40 | The following attributes are supported by Media::Convert::AvSync: | ||||||||||
| 41 | |||||||||||
| 42 | =head2 input | ||||||||||
| 43 | |||||||||||
| 44 | The input asset. Must be a Media::Convert::Asset object. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 1 | has "input" => ( | |||||||||
| 49 | is => 'rw', | ||||||||||
| 50 | required => 1, | ||||||||||
| 51 | isa => 'Media::Convert::Asset', | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | =head2 output | ||||||||||
| 55 | |||||||||||
| 56 | The output asset. Must be a Media::Convert::Asset object. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 1 | has 'output' => ( | |||||||||
| 61 | is => 'rw', | ||||||||||
| 62 | required => 1, | ||||||||||
| 63 | isa => 'Media::Convert::Asset', | ||||||||||
| 64 | ); | ||||||||||
| 65 | |||||||||||
| 66 | =head2 audio_delay | ||||||||||
| 67 | |||||||||||
| 68 | The delay that should be applied to the audio in the input asset. Should | ||||||||||
| 69 | be expressed in seconds; may be a fractional and/or negative value. | ||||||||||
| 70 | |||||||||||
| 71 | Postive values delay the audio; negative values delay the video. | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 1 | has "audio_delay" => ( | |||||||||
| 76 | is => 'rw', | ||||||||||
| 77 | required => 1, | ||||||||||
| 78 | ); | ||||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | sub run() { | ||||||||
|
Subroutine
run: called CC=2 SCAR=11.2
Pod
undocumented
| |||||||||||
| 81 | 1 | my $self = shift; | |||||||||
| 82 | 1 | my $tempdir = tempdir("avsXXXXXX", CLEANUP => 1, TMPDIR => 1); | |||||||||
| 83 | 1 | ▶ | if($self->audio_delay == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | # Why are we here?? | ||||||||||
| 85 | 1 | Media::Convert::Pipe->new(inputs => [$self->input], output => $self->output)->run(); | |||||||||
| 86 | 0 | return; | |||||||||
| 87 | } | ||||||||||
| 88 | 0 | Media::Convert::Pipe->new(inputs => [$self->input], output => Media::Convert::Asset->new(url => "$tempdir/pre.mkv"))->run(); | |||||||||
| 89 | 0 | my $input_audio = Media::Convert::Asset->new(url => "$tempdir/pre.mkv", time_offset => $self->audio_delay); | |||||||||
| 90 | 0 | my $input_video = Media::Convert::Asset->new(url => "$tempdir/pre.mkv"); | |||||||||
| 91 | 0 | my $sync_video = Media::Convert::Asset->new(url => "$tempdir/synced.mkv"); | |||||||||
| 92 | 0 | Media::Convert::Pipe->new(inputs => [$input_audio, $input_video], map => [Media::Convert::Map->new(input => $input_audio, type => "stream", choice => "audio"), Media::Convert::Map->new(input => $input_video, type => "stream", choice => "video")], output => $sync_video)->run(); | |||||||||
| 93 | 0 | $self->output->fragment_start(abs($self->audio_delay)); | |||||||||
| 94 | 0 | Media::Convert::Pipe->new(inputs => [$sync_video], output => $self->output)->run(); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 1 | 1; | |||||||||