collect_videos.pl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/perl
  2. # Collect and move video files into a specific directory, by scanning a given a directory (and its subdirectories) for video files.
  3. # Requires `exiftool`.
  4. use 5.036;
  5. use File::Find qw(find);
  6. use File::Copy qw(move);
  7. use File::Path qw(make_path);
  8. use File::Basename qw(basename);
  9. use File::Spec::Functions qw(catfile curdir rel2abs);
  10. sub is_video ($file) {
  11. my $res = `exiftool \Q$file\E`;
  12. $? == 0 or return;
  13. defined($res) or return;
  14. $res =~ m{^MIME\s+Type\s*:\s*video/}mi;
  15. }
  16. sub collect_video ($file, $directory) {
  17. my $dest = catfile($directory, basename($file));
  18. if (-e $dest) {
  19. warn "File <<$dest>> already exists...\n";
  20. return;
  21. }
  22. move($file, $dest);
  23. }
  24. my @dirs = @ARGV;
  25. @dirs || die "usage: perl $0 [directory | files]\n";
  26. my $directory = rel2abs("Videos"); # directory where to move the videos
  27. if (not -d $directory) {
  28. make_path($directory)
  29. or die "Can't create directory <<$directory>>: $!";
  30. }
  31. if (not -d $directory) {
  32. die "<<$directory>> is not a directory!";
  33. }
  34. find(
  35. {
  36. wanted => sub {
  37. if (-f $_ and is_video($_)) {
  38. say ":: Moving video: $_";
  39. collect_video($_, $directory);
  40. }
  41. },
  42. },
  43. @dirs
  44. );