| 1 | package Media::Convert::Map; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||
| 8 | Media::Convert::Map - Map streams and/or channels from an input asset into an output | ||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||
| 12 | use Media::Convert::Asset; | ||||||||||||||||||||||||||
| 13 | use Media::Convert::Map; | ||||||||||||||||||||||||||
| 14 | use Media::Convert::Pipe; | ||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||
| 16 | my $input_video = Media::Convert::Asset->new(url => $input_with_video); | ||||||||||||||||||||||||||
| 17 | my $input_audio = Media::Convert::Asset->new(url => $input_with_two_stereo_audio_streams); | ||||||||||||||||||||||||||
| 18 | my $output = Media::Convert::Asset->new(url => $output_filename); | ||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||
| 20 | # Merge the video from $input_video with the first audio stream from | ||||||||||||||||||||||||||
| 21 | # $input_audio: | ||||||||||||||||||||||||||
| 22 | my $map_video = Media::Convert::Map->new(input => $input_video, type => "stream", choice => "video"); | ||||||||||||||||||||||||||
| 23 | my $map_audio = Media::Convert::Map->new(input => $input_audio, type => "stream", choice => "audio"); | ||||||||||||||||||||||||||
| 24 | Media::Convert::Pipe->new(input => [$input_video, $input_audio], map => [$map_video, $map_audio], output => $output, vcopy => 1, acopy => 1)->run; | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | # or, extract only the first audio stream: | ||||||||||||||||||||||||||
| 27 | Media::Convert::Pipe->new(input => [$input_audio], map => [$map_audio], output => $output, acopy => 1, vskip => 1)->run; | ||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||
| 29 | # or, extract the second audio stream: | ||||||||||||||||||||||||||
| 30 | my $map_2nd_audio = Media::Convert::Map->new(input => $input_audio, type => "astream", choice => "2"); | ||||||||||||||||||||||||||
| 31 | Media::Convert::Pipe->new(input => [$input_audio], map => [$map_2nd_audio], output => $output, vskip => 1, acopy => 1); | ||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||
| 33 | # or, extract only the left audio channel from the first audio stream | ||||||||||||||||||||||||||
| 34 | # into a mono audio asset: | ||||||||||||||||||||||||||
| 35 | my $map_left_audio = Media::Convert::Map->new(input => $input_audio, type => "channel", choice => "left"); | ||||||||||||||||||||||||||
| 36 | Media::Convert::Pipe->new(input => [$input_audio], map => [$map_left_audio], output => $output, vskip => 1, acopy => 1); | ||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||
| 40 | Media::Convert::Map is a helper object used to configure a | ||||||||||||||||||||||||||
| 41 | L<Media::Asset::Pipe> to route various audio or video streams and | ||||||||||||||||||||||||||
| 42 | channels into particular locations. | ||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||
| 44 | It has three options: C<input>, which must be the | ||||||||||||||||||||||||||
| 45 | L<Media::Convert::Asset> from which we want to map a stream or channel; | ||||||||||||||||||||||||||
| 46 | C<type>, which selects the type of routing to configure, and C<choice>, | ||||||||||||||||||||||||||
| 47 | which selects which input stream or channel to use from the selected | ||||||||||||||||||||||||||
| 48 | type. | ||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||
| 50 | The values for C<type> and C<choice> work together; the values of | ||||||||||||||||||||||||||
| 51 | C<type> define what the valid values of C<choice> are. | ||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||
| 53 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||
| 55 | =head2 input | ||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||
| 57 | The asset from which we are reading data. I<Must> be passed to the same | ||||||||||||||||||||||||||
| 58 | C<Media::Convert::Pipe> as one of the elements in the input array. | ||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||
| 62 | 4 | has 'input' => ( | |||||||||||||||||||||||||
| 63 | required => 1, | ||||||||||||||||||||||||||
| 64 | is => 'rw', | ||||||||||||||||||||||||||
| 65 | isa => 'Media::Convert::Asset', | ||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||
| 68 | =head2 type | ||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||
| 70 | The type of map that is being created. Must be one these options: | ||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||
| 72 | =head3 channel | ||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||
| 74 | Selects an audio channel from the first audio stream in the file, or | ||||||||||||||||||||||||||
| 75 | mixes the first two audio channels from the first audio stream into one | ||||||||||||||||||||||||||
| 76 | mono stream in the output file. | ||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||
| 78 | This value is the default. | ||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||
| 80 | Valid values for C<choice> when this type is selected, are: | ||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||
| 82 | =over | ||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||
| 84 | =item left | ||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||
| 86 | Select the left channel in the first audio stream (assuming it is a | ||||||||||||||||||||||||||
| 87 | stereo stream) | ||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||
| 89 | =item right | ||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||
| 91 | Select the right channel in the first audio stream | ||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||
| 93 | =item both | ||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||
| 95 | Use C<-ac 1> to perform a downmix of all audio channels in the first | ||||||||||||||||||||||||||
| 96 | audio stream into a single mono channel. | ||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||
| 98 | =back | ||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||
| 100 | =head3 stream | ||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||
| 102 | Selects either the first audio or the first video stream in the file | ||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||
| 104 | Valid values for C<choice> when this type is selected, are: | ||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||
| 106 | =over | ||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||
| 108 | =item audio | ||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||
| 110 | Select the first audio stream from the input file | ||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||
| 112 | =item video | ||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||
| 114 | Select the first video stream from the input file | ||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||
| 116 | =back | ||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||
| 118 | =head3 astream | ||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||
| 120 | Selects a specific audio stream from the file, or allows to merge the | ||||||||||||||||||||||||||
| 121 | first two audio streams into a single audio stream, using the C<amix> | ||||||||||||||||||||||||||
| 122 | ffmpeg filter. | ||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||
| 124 | Valid values for C<choice> when this type is selected, are: | ||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||
| 126 | =over | ||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||
| 128 | =item -1 | ||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||
| 130 | Use the C<amix> ffmpeg filter to downmix the first two audio streams | ||||||||||||||||||||||||||
| 131 | from the file into a single audio stream. | ||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||
| 133 | =item any other number | ||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||
| 135 | Select the Nth audio stream from the input file, where N is the number | ||||||||||||||||||||||||||
| 136 | given. | ||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||
| 138 | It is an error to choose an audio stream with an index that is higher | ||||||||||||||||||||||||||
| 139 | than the total number of audio streams in the input file. To discover the | ||||||||||||||||||||||||||
| 140 | number of audio streams in a file, use the | ||||||||||||||||||||||||||
| 141 | L<Media::Convert::Asset/astream_count> method. | ||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||
| 143 | =back | ||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||
| 145 | =head3 allcopy | ||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||
| 147 | Copy all streams. | ||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||
| 149 | The default behavior of ffmpeg is to copy only the first audio stream, | ||||||||||||||||||||||||||
| 150 | the first video stream, and the first subtitle stream, from the input | ||||||||||||||||||||||||||
| 151 | file into the output file. | ||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||
| 153 | When this is not wanted, a Media::Convert::Map object of type "allcopy" | ||||||||||||||||||||||||||
| 154 | will copy I<all> streams, not just the first of each type, into the | ||||||||||||||||||||||||||
| 155 | output file. | ||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||
| 157 | If this option is chosen, no choice value should be selected (any value | ||||||||||||||||||||||||||
| 158 | given is ignored). | ||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||
| 160 | =head3 none | ||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||
| 162 | No routing is done. This has the same effect as not passing any | ||||||||||||||||||||||||||
| 163 | C<Media::Convert::Map> object to C<Media::Convert::Pipe>. | ||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||
| 167 | 4 | has 'type' => ( | |||||||||||||||||||||||||
| 168 | isa => 'Str', | ||||||||||||||||||||||||||
| 169 | is => 'rw', | ||||||||||||||||||||||||||
| 170 | default => 'channel', | ||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||
| 173 | 4 | has 'choice' => ( | |||||||||||||||||||||||||
| 174 | isa => 'Str', | ||||||||||||||||||||||||||
| 175 | is => 'rw', | ||||||||||||||||||||||||||
| 176 | default => 'left', | ||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub arguments { | ||||||||||||||||||||||||
|
Subroutine
arguments: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||
| 180 | 0 | my $self = shift; | |||||||||||||||||||||||||
| 181 | 0 | my $index = shift; | |||||||||||||||||||||||||
| 182 | 0 | my $stream_id; | |||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||
| 184 | 0 | ▶ | if($self->type eq "channel") { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 185 | 0 | ▶ | if($self->choice eq "both") { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 186 | 0 | return ('-ac', '1'); | |||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||
| 188 | 0 | my $choice; | |||||||||||||||||||||||||
| 189 | 0 | ▶ | if($self->choice eq "left") { | ||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||
| 190 | 0 | $choice = "FL"; | |||||||||||||||||||||||||
| 191 | } elsif($self->choice eq "right") { | ||||||||||||||||||||||||||
| 192 | 0 | $choice = "FR"; | |||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||
| 194 | # not supported | ||||||||||||||||||||||||||
| 195 | 0 | croak("Invalid audio channel choice"); | |||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||
| 197 | 0 | $stream_id = $self->input->astream_id; | |||||||||||||||||||||||||
| 198 | 0 | return ("-filter_complex", "[$index:$stream_id]channelmap=channel_layout=mono:map=${choice}-0[out]", "-map", "[out]"); | |||||||||||||||||||||||||
| 199 | } elsif($self->type eq "stream") { | ||||||||||||||||||||||||||
| 200 | 0 | ▶ | if($self->choice eq 'audio') { | ||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||
| 201 | 0 | return ('-map', "$index:a"); | |||||||||||||||||||||||||
| 202 | } elsif($self->choice eq 'video') { | ||||||||||||||||||||||||||
| 203 | 0 | return ('-map', "$index:v"); | |||||||||||||||||||||||||
| 204 | } else { | ||||||||||||||||||||||||||
| 205 | ... | ||||||||||||||||||||||||||
| 206 | 0 | } | |||||||||||||||||||||||||
| 207 | } elsif($self->type eq "astream") { | ||||||||||||||||||||||||||
| 208 | 0 | my $choice = $self->choice; | |||||||||||||||||||||||||
| 209 | 0 | ▶ | if($choice > $self->input->astream_count) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 210 | 0 | croak("Invalid audio stream, not supported by input video"); | |||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||
| 212 | 0 | ▶ | if($choice == -1) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 213 | 0 | my $ids = $self->input->astream_ids; | |||||||||||||||||||||||||
| 214 | 0 | my $id1 = $ids->[0]; | |||||||||||||||||||||||||
| 215 | 0 | my $id2 = $ids->[1]; | |||||||||||||||||||||||||
| 216 | 0 | return ('-filter_complex', "[$index:$id1][$index:$id2]amix=inputs=2:duration=first"); | |||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||
| 218 | 0 | return ('-map', "$index:a:$choice"); | |||||||||||||||||||||||||
| 219 | } elsif($self->type eq "allcopy") { | ||||||||||||||||||||||||||
| 220 | 0 | return ('-map', '0'); | |||||||||||||||||||||||||
| 221 | } elsif($self->type eq "none") { | ||||||||||||||||||||||||||
| 222 | 0 | return (); | |||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||
| 224 | ... | ||||||||||||||||||||||||||
| 225 | 0 | } | |||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||
| 228 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||
| 230 | 4 | 1; | |||||||||||||||||||||||||