ubuntuusers.de

Anhang: conv2mp4.pl

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#!/usr/bin/perl
#########################################################################################
#    
#    conv2mp4pl -:- Convert video files to mp4
#    
#    VERSION:   0.1
#    AUTHOR:    Vincent Wochnik
#    EMAIL:     v.wochnik@googlemail.com
#    COPYRIGHT: (c) by Vincent Wochnik 2009
#    
#    Permission is hereby granted, free of charge, to any person obtaining a copy
#    of this software and associated documentation files (the "Software"), to
#    deal in the Software without restriction, including without limitation the
#    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
#    sell copies of the Software, and to permit persons to whom the Software is
#    furnished to do so, subject to the following conditions:
#    
#    The above copyright notice and this permission notice shall be included in
#    all copies or substantial portions of the Software.
#    
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
#    IN THE SOFTWARE.
#    
#########################################################################################

use strict;
use warnings;
use POSIX;
use File::Basename;

# Video file names
my $source = '', my $format = '';
my $destination = '', my $pattern = '';
my $tmpdir = '';

# source audio and video information
my @audio, my @video;

# options, encoder preferences, temporary command place holder
my %opts, my %prefs, my %formats, my %codecs;
my $cmd, my $verbose = 0, my $info = 0, my $line;

%opts = (
	'acodec'    => '',  # preferred audio codec
	'vcodec'    => '',  # preferred video codec
	'forcea'    => 0,   # force audio re-encoding
	'forcev'    => 0,   # force video re-encoding
	'nogain'    => 0,   # skip audio normalization
	'overwrite' => 0,   # overwrite destination file, else rename
	'skip'      => 0,   # skip existing destination files
	'profile'   => '',  # quality profile
	'target'    => ''   # target platform, e.g. phone
);

%formats = (
	'mp4' => 0,  # MPEG-4 format
	'm4a' => 0,  # MPEG-4 audio format
	'mov' => 0,  # Apple quicktime
	'3gp' => 0,  # 3-GP format
	'3g2' => 0,  # 3-GP 2 format
	'avi' => 0   # AVI format
);

%codecs = (
	'x264'  => 0,  # H 264 video codec
	'xvid'  => 0,  # XviD video codec
	'faac'  => 0,  # MPEG-4 AAC codec
	'naac'  => 0,  # proprietary Nero AAC codec
	'lame'  => 0,  # lame mp3 codec
);

%prefs = (
	'rate'        => 44100,
	'width'       => 0,
	'height'      => 0,
	'fps'         => 0,
	'faac_qual'   => '100',
	'naac_qual'   => '0.4',
	'lame_qual'   => '6',
	'x264_crf'    => '28',
	'x264_bf'     => '5',
	'x264_refs'   => '4',
	'x264_preset' => 'hq',
	'xvid_qscale' => '3.75'
);

# parse options
for (my $i = 0; $i < @ARGV; $i++) {
	if ( $ARGV[$i] =~ m/^\-(h|help)$/ ) {
		print STDERR <DATA>;
		exit 2;
	} elsif ( $ARGV[$i] =~ m/^\-(v|verbose)$/ ) {
		$verbose += 1;
	} elsif ( $ARGV[$i] =~ m/^\-(vv|debug)$/ ) {
		$verbose += 2;
	} elsif ( $ARGV[$i] =~ m/^\-(i|info)$/ ) {
		$info = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(f|force)$/ ) {
		$opts{'forcea'} = 1;
		$opts{'forcev'} = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(fa|force\-audio)$/ ) {
		$opts{'forcea'} = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(fv|force\-video)$/ ) {
		$opts{'forcev'} = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(ng|no\-gain)$/ ) {
		$opts{'nogain'} = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(y|overwrite)$/ ) {
		if ( $opts{'skip'} != 0 ) {
			print STDERR "Invalid arguments.\n";
			print STDERR <DATA>;
			exit 2;
		}
		$opts{'overwrite'} = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(n|skip)$/ ) {
		if ( $opts{'overwrite'} != 0 ) {
			print STDERR "Invalid arguments.\n";
			print STDERR <DATA>;
			exit 2;
		}
		$opts{'skip'} = 1;
	} elsif ( $ARGV[$i] =~ m/^\-(p|profile)$/ ) {
		$i++;
		$opts{'profile'} = $ARGV[$i];
	} elsif ( $ARGV[$i] =~ m/^\-(t|target)$/ ) {
		$i++;
		$opts{'target'} = $ARGV[$i];
	} elsif ( $ARGV[$i] =~ m/^\-(d|tmpdir)$/ ) {
		$i++;
		if ( ! -d $ARGV[$i] ) {
			print STDERR "Invalid arguments.\n";
			print STDERR <DATA>;
			exit 2;
		}
		$tmpdir = $ARGV[$i];
	} elsif ( $ARGV[$i] =~ m/^\-/ ) {
		print STDERR "Invalid arguments.\n";
		exit 2;
	} elsif (( -f $ARGV[$i] ) && ( $source eq '' )) {
		$source = $ARGV[$i];
	} elsif ( $source ne '' ) {
		$destination = $ARGV[$i];
	} else {
		print STDERR "Invalid arguments.\n";
		print STDERR <DATA>;
		exit 2;
	}
}
if ( ! -f $source ) {
	print STDERR "Invalid arguments.\n";
	print STDERR <DATA>;
	exit 2;
}

# check which codecs are available
$cmd = 'ffmpeg -formats';
printf "Opening ``%s'' for reading.\n", $cmd if ( $verbose > 0 );
open( FORMATS, $cmd .' 2>/dev/null |' )
	or die( 'Couldn\'t start ffmpeg.' );
while ( $line = <FORMATS> ) {
	if ( $line =~ m/^File formats:/ ) {
		while ( $line = <FORMATS> ) {
			last if ( $line !~ m/^\ / );

			if ( $line =~ m/^..E.ipod/ ) {
				$formats{'m4a'} = 2;
			} elsif ( $line =~ m/^..E.mp4/ ) {
				$formats{'mp4'} = 1;
			} elsif ( $line =~ m/^..E.mov/ ) {
				$formats{'mov'} = 1;
			} elsif ( $line =~ m/^..E.3gp/ ) {
				$formats{'3gp'} = 1;
			} elsif ( $line =~ m/^..E.3g2/ ) {
				$formats{'3g2'} = 1;
			} elsif ( $line =~ m/^..E.avi/ ) {
				$formats{'avi'} = 1;
			}
		}
	}
	if ( $line =~ m/^Codecs:/ ) {
		while ( $line = <FORMATS> ) {
			last if ( $line !~ m/^\ / );

			if ( $line =~ m/^..E.....libx264/ ) {
				$codecs{'x264'} = 1;
			} elsif ( $line =~ m/^..E.....libxvid/ ) {
				$codecs{'xvid'} = 1;
			} elsif ( $line =~ m/^..E.....libfaac/ ) {
				$codecs{'faac'} = 1;
			} elsif ( $line =~ m/^..E.....libmp3lame/ ) {
				$codecs{'lame'} = 1;
			}
		}
	}
}
close( FORMATS );

# check if we have access to the neroAacEnc program
# you possibly have to set PATH variable
$cmd = 'which neroAacEnc';
printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
$codecs{'naac'} = 1
	if ( system( $cmd ) == 0 );

# First we need to know the audio/video stream format and mapping
print "Analyzing source file ...\n";
$cmd = sprintf( 'ffmpeg -i "%s"', $source );
printf "Opening ``%s'' for reading.\n", $cmd if ( $verbose > 0 );
open( SRC, $cmd .' 2>&1 1>/dev/null |' )
	or die( 'Couldn\'t start ffmpeg.' );

while ( $line = <SRC> ) {
	# we need to get to the input section
	if ( $line =~ m/^Input\ #/ ) {
		while ( $line = <SRC> ) {
			next if ( $line =~ m/^\ \ [A-Z]/ );
			last if ( $line !~ m/^\ \ \ \ / );
			if ( $line =~ m/^\ \ \ \ Stream\ #([0-9]+)\.([0-9]+)(.*):\ Audio:\ (.*?),\ (.*?)\ Hz,\ (.*?),/ ) {
				push( @audio, "$1.$2" );  # mapping
				push( @audio, $4 );       # codec
				push( @audio, $5 );       # sampling rate
				push( @audio, $6 );       # channels
				push( @audio, '' );       # empty string, later containing file name
			} elsif ( $line =~ m/^\ \ \ \ Stream\ #([0-9]+)\.([0-9]+)(.*):\ Video:\ (.*?),\ (.*?),\ (.*?),/ ) {
				push( @video, "$1.$2" );  # mapping
				push( @video, $4 );       # codec
				push( @video, $5 );
				push( @video, $6 );       # resolution
				push( @video, '' );       # empty string, later containing file name
			}
		}
	}
}
close( SRC );
print "Done. Found ". @video/5 ." video stream(s) and ". @audio/5 ." audio stream(s).\n\n";

# if there are no streams, no work is done
if (( @audio == 0) && ( @video == 0 )) {
	print "Nothing to convert.\n";
	exit 2;
}

if ( @audio > 0 ) {
	print "Audio streams:\n";
	print "  ID  CODEC   RATE CHANNELS\n";
	for ( my $i = 0, my $a = 0; $i < @audio; $i += 5, $a++ ) {
		printf "%4u %6s %6u %8s\n", $a, $audio[$i+1], $audio[$i+2], $audio[$i+3];
	}
}

if ( @video > 0 ) {
	print "Video streams:\n";
	print "  ID  CODEC   FORMAT RESOLUTION\n";
	for ( my $i = 0, my $a = 0; $i < @video; $i += 5, $a++ ) {
		printf "%4u %6s %8s %10s\n", $a, $video[$i+1], $video[$i+2], $video[$i+3];
	}
}
print "\n";

# we only want to give information
exit 0 if ( $info != 0 );

if (( $destination ne '' ) && ( ! -d $destination )) {
	# fine! user has specified a file as destination
	$format = ($destination =~ m/([^.]+)$/)[0];
	die( 'Format `'. $format .'\' not supported.' )
		if (( ! defined $formats{$format} ) || ( $formats{$format} == 0 ));
} else {
	if ( @video > 0 ) {
		if ( $formats{'mp4'} != 0 ) {
			$format = 'mp4';
		} elsif ( $formats{'avi'} != 0 ) {
			$format = 'avi';
		} else {
			die( 'Formats `mp4\' and `avi\' not supported.' );
		}
	} else {
		if ( $formats{'m4a'} != 0 ) {
			$format = 'm4a';
		} elsif ( $formats{'mp4'} != 0 ) {
			$format = 'mp4';
		} else {
			die( 'Formats `m4a\' and `mp4\' not supported.' );
		}
	}

	# set destination if not specified
	if ( -d $destination ) {
		$destination =~ s/\/$//;
		$destination .= '/'. basename( $source );
		$destination =~ s/\.[A-Za-z0-9]+$//;
	} else {
		$destination = $source;
		$destination =~ s/\.[A-Za-z0-9]+$//;
	}
	$destination .= '.'. $format;

	if (( $opts{'overwrite'} == 0 ) && ( $opts{'skip'} == 0 )) {
		while ( -f $destination ) {
			$destination =~ s/\.[A-Za-z0-9]+$//;
			$destination .= '_copy';
			$destination .= '.'. $format;
		}
	}
}

# skip here if output file exists
if (( $opts{'skip'} != 0 ) && ( -f $destination )) {
	print "Output file already exists.\n";
	exit 2;
}

# determining encoders
if ( $format =~ m/^(mp4|mov|3gp|3g2|avi)$/ ) {
	if ( $codecs{'naac'} != 0 ) {
		$opts{'acodec'} = 'naac';
	} elsif ( $codecs{'faac'} != 0 ) {
		$opts{'acodec'} = 'faac';
	} elsif ( $codecs{'lame'} != 0 ) {
		$opts{'acodec'} = 'lame';
	}

	if ( $opts{'target'} eq 'mobile' ) {
		# a simpler codec would be better here
		if ( $codecs{'xvid'} != 0 ) {
			$opts{'vcodec'} = 'xvid';
		} else {
			$opts{'vcodec'} = 'x264';
		}
	} else {
		if ( $codecs{'x264'} != 0 ) {
			$opts{'vcodec'} = 'x264';
		} else {
			$opts{'vcodec'} = 'xvid';
		}
	}
} elsif ( $format eq 'm4a' ) {
	if ( $codecs{'naac'} != 0 ) {
		$opts{'acodec'} = 'naac';
	} else {
		$opts{'acodec'} = 'faac';
	}
}

print "Determining encoder settings ...\n";
if (( $opts{'profile'} ne '' ) || ( $opts{'target'} ne '' )) {
	$opts{'profile'} = 'normal'
		if ( $opts{'profile'} eq '' );

	if ( $opts{'profile'} eq 'worse' ) {
		$prefs{'faac_qual'} = 50;
		$prefs{'naac_qual'} = '0.2';
		$prefs{'lame_qual'} = 9;
		$prefs{'x264_crf'} = 40;
		$prefs{'x264_bf'} = 0;
		$prefs{'x264_refs'} = 0;
		$prefs{'x264_preset'} = 'baseline';
		$prefs{'xvid_qscale'} = '5.5';

		if ( $opts{'target'} eq 'mobile' ) {
			$prefs{'x264_preset'} = 'baseline';
			$prefs{'width'} = 128;
			$prefs{'height'} = 96;
			$prefs{'fps'} = 15;
			$prefs{'rate'} = 22050;
		} elsif ( $opts{'target'} ne '' ) {
			die( 'Unknown target `'. $opts{'target'} .'\'.' );
		}
	} elsif ( $opts{'profile'} eq 'bad' ) {
		$prefs{'faac_qual'} = 75;
		$prefs{'naac_qual'} = '0.3';
		$prefs{'lame_qual'} = 7;
		$prefs{'x264_crf'} = 34;
		$prefs{'x264_bf'} = 2;
		$prefs{'x264_refs'} = 2;
		$prefs{'xvid_qscale'} = '4';

		if ( $opts{'target'} eq 'mobile' ) {
			$prefs{'x264_preset'} = 'baseline';
			$prefs{'width'} = 176;
			$prefs{'height'} = 144;
			$prefs{'fps'} = 15;
			$prefs{'rate'} = 22050;
		} elsif ( $opts{'target'} ne '' ) {
			die( 'Unknown target `'. $opts{'target'} .'\'.' );
		}
		$prefs{'rate'} = 44100;
	} elsif ( $opts{'profile'} eq 'normal' ) {
		# everything is set, nothing to do here

		if ( $opts{'target'} eq 'mobile' ) {
			$prefs{'x264_preset'} = 'baseline';
			$prefs{'width'} = 320;
			$prefs{'height'} = 240;
			$prefs{'fps'} = 15;
			$prefs{'rate'} = 22050;
		} elsif ( $opts{'target'} ne '' ) {
			die( 'Unknown target `'. $opts{'target'} .'\'.' );
		}
	} elsif ( $opts{'profile'} eq 'good' ) {
		$prefs{'faac_qual'} = 125;
		$prefs{'naac_qual'} = '0.5';
		$prefs{'lame_qual'} = 4;
		$prefs{'x264_crf'} = 23;
		$prefs{'x264_bf'} = 7;
		$prefs{'x264_refs'} = 6;
		$prefs{'xvid_qscale'} = '3.25';
		$prefs{'rate'} = 48000;

		if ( $opts{'target'} eq 'mobile' ) {
			$prefs{'x264_preset'} = 'baseline';
			$prefs{'width'} = 480;
			$prefs{'height'} = 360;
			$prefs{'fps'} = 15;
			$prefs{'rate'} = 44100;
		} elsif ( $opts{'target'} ne '' ) {
			die( 'Unknown target `'. $opts{'target'} .'\'.' );
		}
	} elsif ( $opts{'profile'} eq 'best' ) {
		$prefs{'faac_qual'} = 150;
		$prefs{'naac_qual'} = '0.6';
		$prefs{'lame_qual'} = 3;
		$prefs{'x264_crf'} = 18;
		$prefs{'x264_bf'} = 7;
		$prefs{'x264_refs'} = 6;
		$prefs{'xvid_qscale'} = '2.75';
		$prefs{'rate'} = 48000;

		# quite useless, but anyway ...
		if ( $opts{'target'} eq 'mobile' ) {
			die( 'You don\'t have such a good phone. :P' );
		} elsif ( $opts{'target'} ne '' ) {
			die( 'Unknown target `'. $opts{'target'} .'\'.' );
		}
	} else {
		die( 'Unknown profile `'. $opts{'profile'} .'\'.' );
	}
}

print "Done.\n\n";

if ( $tmpdir ne '' ) {
	$tmpdir =~ s/\/$//;
	$tmpdir .= '/';
} else {
	$tmpdir = dirname( $destination ) .'/';
}

# before we start converting,
# we need a pattern we can pad up file names
# for some programs not supporting spaces *argghh*
for ( my $i = 0, my @chars = ( 'A'..'Z', 'a'..'z', '0'..'9' ); $i < 8; $i++ ) {
	$pattern .= $chars[rand @chars];
}

print "Converting \"". $source ."\" to \"". $destination ."\" ...\n\n";
$cmd = sprintf( 'touch "%s"', $destination );
printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
(system( $cmd ) == 0)
	or die( 'Invalid destination file.' );

# First we need to extract audio streams
print "Extracting audio streams ...\n";
for ( my $i = 0, my $a = 0, my $force = 0; $i < @audio; $i += 5, $a++, $force = 0 ) {
	$force = 1 if ( $opts{'forcea'} != 0 );
	$force = 1 if ( $audio[$i+1] !~ m/^(ac3|aac|mp3)$/ );
	$force = 1 if (( $prefs{'rate'} > 0 ) && ( $prefs{'rate'} < $audio[$i+2] ));
	$force = 1 if (( $audio[$i+1] eq 'mp3' ) &&
		           (( $opts{'target'} eq 'mobile' ) || ( $format =~ m/^(m4a|mov|3gp|3g2)$/ )));
	$force = 1 if (( $audio[$i+1] eq 'ac3' ) && ( $format =~ m/^(m4a|mov|3gp|3g2)$/ ));

	if ( $force == 0 ) {
		$audio[$i+4] = $tmpdir . $pattern .'.'. $a .'.m4a'
			if ( $audio[$i+1] =~ m/^(ac3|aac)$/ );
		$audio[$i+4] = $tmpdir . $pattern .'.'. $a .'.mp3'
			if ( $audio[$i+1] eq 'mp3' );

		$cmd = sprintf( 'ffmpeg -i "%s" -map %s -acodec copy -vn -y "%s"',
			$source, $audio[$i], $audio[$i+4] );
		printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
		$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
		if (system( $cmd ) != 0) {
			warn( 'Couldn\'t extract audio stream '. $i .' from source file.' );
			$force = 1;
			unlink $audio[$i+4];
		} elsif ( $opts{'nogain'} == 0 ) {
			if ( $audio[$i+1] =~ m/^(aac|mp3)$/ ) {
				$cmd = sprintf( 'aacgain -r -k "%s"', $audio[$i+4] )
					if ( $audio[$i+1] eq 'aac' );
				$cmd = sprintf( 'mp3gain -k -r "%s"', $audio[$i+4] )
					if ( $audio[$i+1] eq 'mp3' );

				printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
				$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
				if (system( $cmd ) != 0) {
					warn( 'Couldn\'t normalize audio stream.' );
					$force = 1;
					unlink $audio[$i+4];
				}
			} else {
				warn( 'Couldn\'t normalize audio stream.' );
			}
		}
	}

	# if we are forced to re-encode
	if ( $force != 0 ) {
		print "Need to re-encode audio stream ...\n";

		if ((( $codecs{'naac'} != 0 ) || ( $codecs{'faac'} != 0 )) && ( $opts{'acodec'} =~ m/^.aac$/ )) {
			$audio[$i+4] = $tmpdir . $pattern .'.'. $a .'.m4a';

			if (( $codecs{'naac'} != 0 ) && ( $opts{'acodec'} eq 'naac' )) {
				print "Using Nero AAC codec.\n";

				$cmd = sprintf( 'ffmpeg -i "%s" -map %s -vn -acodec pcm_s16le',
					$source, $audio[$i] );
				$cmd .= sprintf( ' -ar %u', $prefs{'rate'} )
					if (( $prefs{'rate'} > 0 ) && ( $prefs{'rate'} < $audio[$i+2] ));
				$cmd .= sprintf( '  -y "%s.wav"', $audio[$i+4] );
				printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
				$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
				(system( $cmd ) == 0)
					or die( 'Couldn\'t extract audio stream '. $i .' from source file.' );

				$cmd = sprintf( 'neroAacEnc -q %f -lc -if "%s.wav" -of "%s"',
					$prefs{'naac_qual'}, $audio[$i+4], $audio[$i+4] );
				printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
				$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
				(system( $cmd ) == 0)
					or die( 'Couldn\'t extract audio stream '. $i .' from source file.' );
				unlink $audio[$i+4] .'.wav';
			} elsif (( $codecs{'faac'} != 0 ) && ( $opts{'acodec'} eq 'faac' )) {
				print "Using FAAC codec.\n";

				$cmd = sprintf( 'ffmpeg -i "%s" -map %s -vn -acodec libfaac',
					$source, $audio[$i] );
				$cmd .= sprintf( ' -ar %u', $prefs{'rate'} )
					if (( $prefs{'rate'} > 0 ) && ( $prefs{'rate'} < $audio[$i+2] ));
				$cmd .= sprintf( ' -aq %f -y "%s"',
					$prefs{'faac_qual'}, $audio[$i+4] );
				printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
				$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
				(system( $cmd ) == 0)
					or die( 'Couldn\'t extract audio stream '. $i .' from source file.' );
			}

			if ( $opts{'nogain'} == 0 ) {
				$cmd = sprintf( 'aacgain -r -k "%s"', $audio[$i+4] );
				printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
				$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
				(system( $cmd ) == 0)
					or warn( 'Couldn\'t normalize audio stream.' );
			}
		} elsif (( $codecs{'lame'} != 0 ) && ( $opts{'acodec'} eq 'lame' )) {
			print "Using plain old lame codec.\n";
			$audio[$i+4] = $tmpdir . $pattern .'.'. $a .'.mp3';

			$cmd = sprintf( 'ffmpeg -i "%s" -map %s -vn -acodec libmp3lame',
				$source, $audio[$i] );
			$cmd .= sprintf( ' -ar %u', $prefs{'rate'} )
				if (( $prefs{'rate'} > 0 ) && ( $prefs{'rate'} < $audio[$i+2] ));
			$cmd .= sprintf( ' -aq %f -y "%s"',
				$prefs{'lame_qual'}, $audio[$i+4] );
			printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
			$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
			(system( $cmd ) == 0)
				or die( 'Couldn\'t extract audio stream '. $i .' from source file.' );

			if ( $opts{'nogain'} == 0 ) {
				$cmd = sprintf( 'mp3gain -k -r "%s"', $audio[$i+4] );
				printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
				$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
				(system( $cmd ) == 0)
					or warn( 'Couldn\'t normalize audio stream.' );
			}
		} else {
			die( 'There is no audio codec.' );
		}
	}
}
print "Done.\n\n";

# Then we need to extract video streams
print "Extracting video streams ...\n";
for ( my $i = 0, my $force = 0, my $a = 0; $i < @video; $i += 5, $a++, $force = 0 ) {
	my $w, my $h;
	my $size_params = '';
	$force = 1 if ( $opts{'forcev'} != 0 );
	$force = 1 if ( $prefs{'fps'} > 0 );
	$force = 1 if ( $opts{'profile'} ne '' );
	$force = 1 if ( $prefs{'x264_preset'} ne 'hq' );

	# first, try to analyze resolution
	if ( $video[$i+3] =~ m/([0-9]+)x([0-9]+)/ ) {
		$w = $1;
		$h = $2;

		if (( $prefs{'width'} > 0 ) || ( $prefs{'height'} > 0 )) {
			my $r = $w/$h, my $p1, my $p2;

			if (( $prefs{'width'} > 0 ) && ( $prefs{'height'} > 0 )) {
				my $r2 = $prefs{'width'} / $prefs{'height'};

				# if the desired resolution is wider then the video
				if ( $r2 > $r ) {
					$h = floor( $prefs{'height'} / 2 ) * 2;
					$w = floor( $prefs{'height'} * $r / 2 ) * 2;
					# pads left and right
					$p1 = floor( ceil( ($prefs{'width'} - $w) / 2 ) / 2 ) * 2;
					$p2 = ceil( ceil( ($prefs{'width'} - $w) / 2 ) / 2 ) * 2;
					$size_params .= ' -s '. $w .'x'. $h .' -padleft '. $p1 .' -padright '. $p2;
				} else {
					$w = floor( $prefs{'width'} / 2 ) * 2;
					$h = floor( $prefs{'width'} / $r / 2 ) * 2;
					# pads above and below
					$p1 = floor( ceil( ($prefs{'height'} - $h) / 2 ) / 2 ) * 2;
					$p2 = ceil( ceil( ($prefs{'height'} - $h) / 2 ) / 2 ) * 2;
					$size_params .= ' -s '. $w .'x'. $h .' -padtop '. $p1 .' -padbottom '. $p2;
				}
			} elsif ( $prefs{'width'} > 0 ) {
				$w = floor( $prefs{'width'} / 2 ) * 2;
				$h = floor( $prefs{'width'} / $r / 2 ) * 2;
				$size_params .= ' -s '. $w .'x'. $h;
			} elsif ( $prefs{'height'} > 0 ) {
				$h = floor( $prefs{'height'} / 2 ) * 2;
				$w = floor( $prefs{'height'} * $r / 2 ) * 2;
				$size_params .= ' -s '. $w .'x'. $h;
			}
			$force = 1;
		}

		$size_params .= sprintf( ' -aspect %.4f', $w/$h );
	}

	# it doesn't make any sense to re-encode mp4!!!
	$video[$i+4] = $tmpdir . $pattern .'.'. $a .'.mp4';
	if ((( $video[$i+1] eq 'mpeg4' ) || ( $video[$i+1] eq 'h264' )) && ( $force == 0 )) {
		$cmd = sprintf( 'ffmpeg -i "%s" -map %s -an -vcodec copy -y "%s" -copyts',
			$source, $video[$i], $video[$i+4] );
		printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
		$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
		if (system( $cmd ) != 0) {
			warn( 'Couldn\'t extract video stream '. $i .' from source file.' );
			$force = 1;
			unlink $video[$i+4];
		}
	} else {
		$force = 1;
	}

	if ($force != 0) {
		print "Need to re-encode video stream ...\n";

		if (( $codecs{'x264'} != 0 ) && ( $opts{'vcodec'} eq 'x264' )) {
			print "Using H 264 codec.\n";
			$cmd = sprintf( 'ffmpeg -i "%s" -map %s%s',
				$source, $video[$i], $size_params );
			$cmd .= sprintf( ' -r %f', $prefs{'fps'} ) if ( $prefs{'fps'} > 0 );
			$cmd .= sprintf( ' -an -vcodec libx264 -crf %f -bf %u '
				.'-refs %u -trellis 2 -me_method umh -me_range 24 '
				.'-vpre %s -threads 0 -y "%s" -copyts',
				$prefs{'x264_crf'}, $prefs{'x264_bf'}, $prefs{'x264_refs'},
				$prefs{'x264_preset'}, $video[$i+4] );
		} elsif (( $codecs{'xvid'} != 0 ) && ( $opts{'vcodec'} eq 'xvid' )) {
			print "Using Xvid codec.\n";
			$cmd = sprintf( 'ffmpeg -i "%s" -map %s%s',
				$source, $video[$i], $size_params );
			$cmd .= sprintf( ' -r %f', $prefs{'fps'} ) if ( $prefs{'fps'} > 0 );
			$cmd .= sprintf( ' -an -vcodec libxvid -qscale %uk -threads 0 '
				.'-y "%s" -copyts',
				$prefs{'xvid_qscale'}, $video[$i+4] );
		} else {
			die( 'There is no video codec.' );
		}

		printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
		$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
		(system( $cmd ) == 0)
			or die( 'Couldn\'t extract video stream '. $i .' from source file.' );
	}
}
print "Done.\n\n";

# preparing the final command
print "Putting together all streams ...\n";
$cmd = 'ffmpeg';
for (my $i = 0; $i < @video; $i += 5 ) {
	$cmd .= sprintf( ' -i "%s"', $video[$i+4] );
}
for (my $i = 0; $i < @audio; $i += 5 ) {
	$cmd .= sprintf( ' -i "%s"', $audio[$i+4] );
}
for (my $i = 0, my $a = 0; $i < @video + @audio; $i += 5, $a++ ) {
	$cmd .= sprintf( ' -map %u.0', $a );

	# synchronize with the first audio stream
	$cmd .= sprintf( ':%u.0', @video/5 ) if (( $i < @video ) && ( @audio > 0 ));
}

$cmd .= ' -vcodec copy -vsync 1' if ( @video > 0 );
$cmd .= ' -acodec copy' if ( @audio > 0 );
$cmd .= sprintf( ' -y "%s"', $destination );
for ( my $i = 5; $i < @video; $i += 5 ) {
	$cmd .= ' -vcodec copy -vsync 1 -newvideo';
}
for ( my $i = 5; $i < @audio; $i += 5 ) {
	$cmd .= ' -acodec copy -newaudio';
}
printf "Executing ``%s''.\n", $cmd if ( $verbose > 0 );
$cmd .= ' >/dev/null 2>&1' if ( $verbose <= 1 );
(system( $cmd ) == 0)
	or die( 'Couldn\'t join streams.' );
print "Done.\n\n";

# cleaning up
for ( my $i = 0; $i < @video; $i += 5 ) {
	unlink $video[$i+4];
}
for ( my $i = 0; $i < @audio; $i += 5 ) {
	unlink $audio[$i+4];
}

__DATA__
USAGE: conv2mp4pl [(-h|-help)]	 [(-v|-vv)] [-i] [(-y|-n)] [-t DIRECTORY]
         [-f|-fa|-fv] [-ng] [-p PROFILE] [-t TARGET] SOURCE [DESTINATION]

  SOURCE must be a file accessable from the local file system.
  DESTINATION can be a file which is then created or a directory.
    In the second case, a file with the same name as the source file but
    with the destination format's extension is created.

OPTIONS

  -h, -help
    A help text is displayed.
  -v, -verbose
    Verbose mode. Commands are printed to STDOUT before they are executed.
  -vv, -debug
    Debug mode. Same as -v, but command output is also printed.
  -i, -info
    Information mode. Only information about the source given is printed.
  -y, -overwrite
    Existing destination files are overwritten. Use with caution!
  -n, -skip
    Already existing destination files are not converted.
  -d, -tmpdir
    This option specifies a directory where temporary files are stored.
  -f, -force
    Force conversion of all audio and video streams.
  -fa, -force-audio
    Force conversion of all audio streams.
  -fv, -force-video
    Force conversion of all video streams.
  -ng, -nogain
    Skip normalization of audio streams.
  -p, -profile
    Specifies a quality profile. Available profiles are: worse, bad,
    normal, good, best.
  -t, -target
    Specifies a target device. Available targets are: phone.

EXAMPLES

  Convert a video file to mp4.
    conv2mp4pl /home/you/videos/example.avi

    In this case a file named /home/you/videos/example.mp4 is created.

  Convert a file to mp4, but don't copy streams.
    conv2mp4pl -f /home/you/videos/example.avi

    Same like above, but don't copy streams as they are.

  Convert a video for your mobile.
    conv2mp4pl -p bad -t mobile /home/you/videos/example.avi
      /media/mobile/videos/

    This creates a file named /media/mobile/videos/example.mp4 with the
    desired quality level.

  Convert many files at once.
    find /home/you/videos/old -name *.avi -exec conv2mp4pl -n "{}"
      /home/you/videos/new \;

    This command looks for avi files in the /home/you/videos/old directory
    and starts an instance of conv2mp4pl for each of them.
Anhang herunterladen

Skript, um Videodateien in das mp4-Format zu konvertieren.

Diese Revision wurde am 15. Januar 2014 10:10 von ubuntuusers erstellt.