any_to_3gp.pl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 10 June 2013
  5. # https://github.com/trizen
  6. #
  7. ## Convert any media file to the 3gp mobile format.
  8. #
  9. # Requires ffmpeg compiled with '--enable-libopencore_amrnb'
  10. use 5.010;
  11. use strict;
  12. use warnings;
  13. use Getopt::Std qw(getopts);
  14. use File::Path qw(make_path);
  15. use File::Spec::Functions qw(catfile);
  16. my %opt;
  17. getopts('f:o:i:h', \%opt);
  18. if ($opt{h} or not defined $opt{f}) {
  19. print <<"USAGE";
  20. usage: $0 [options]
  21. options:
  22. -f format : convert only this video formats (can be a regex)
  23. -i input dir : convert videos from this directory (default: '.')
  24. -o output dir : where to put the converted videos (default: '.')
  25. example: perl $0 -f 'mp4|webm' -i Videos/ -o 3GP_Videos/
  26. USAGE
  27. exit !$opt{h};
  28. }
  29. my $output_dir = $opt{o} // '.';
  30. my $input_dir = $opt{i} // '.';
  31. my $input_format = eval { qr{\.\K(?:$opt{f})\z}i } // die "$0: Invalid regex: $@";
  32. if (not -d $output_dir) {
  33. make_path($output_dir)
  34. or die "$0: Can't create path '$output_dir': $!\n";
  35. }
  36. opendir(my $dir_h, $input_dir)
  37. or die "$0: Can't open dir '$input_dir': $!\n";
  38. while (defined(my $file = readdir $dir_h)) {
  39. (my $output_file = $file) =~ s{$input_format}{3gp} or next;
  40. -f -s (my $input_file = catfile($input_dir, $file)) or next;
  41. system qw(ffmpeg -i), $input_file, qw(
  42. -acodec amr_nb
  43. -ar 8000
  44. -ac 1
  45. -ab 32
  46. -vcodec h263
  47. -s qcif
  48. -r 15
  49. ), catfile($output_dir, $output_file);
  50. if ($? != 0) {
  51. die "$0: ffmpeg exited with a non-zero code!\n";
  52. }
  53. }
  54. closedir($dir_h);