summaryrefslogtreecommitdiff
path: root/get.pl
blob: f72addc150916849d74149c9fd08ce1d9ebd1ef1 (plain)
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
#!/usr/bin/perl

$SIG{TERM} = $SIG{INT} = sub { exit 1 };

sub sanitize ($) {
  my $s = lc shift;
  $s =~ s/\.[^.]+$//g;
  $s =~ s/\W/_/g;
  $s =~ s/\s+/_/g;
  if (length($s) == 0) {
    print "fn is empty";
    exit(1);
  }
  $s =~ s/_+/_/g;
  return $s;
}

my ($s) = @ARGV;
 
if (not defined $s || $s !~ /^http/) {
  die "usage: ./get.pl http://...\n";
}

mkdir("sequences/");
chdir("sequences/");

my $fn, $new_fn, $video_fn;

if ($s =~ /(mov|mp4|webm|avi|mpe?g)$/i) {
  $s =~ s/^\s+//;
  $s =~ s/\s+$//;
  my $fn = `basename $s`;
  $clean_fn = $fn;
  $clean_fn =~ s/-/_/g;
  $clean_fn =~ s/^\s//;
  $clean_fn =~ s/\s$//;
  $clean_fn =~ s/\s/_/g;
  $clean_fn =~ s/_+/_/g;
  system('rm', $fn);
  system('rm', $clean_fn);
  print "downloading $clean_fn\n";
  system('wget', '-O', $clean_fn, $s);
  $video_fn = $clean_fn;
}
else {
  print "youtube-dl $s\n";
  my $yt = `youtube-dl -o "%(title)s.%(ext)s" $s`;
  print $yt;
  my @partz = split("\n", $yt);
  foreach $part (@partz) {
    print "$part\n";
    if ($part =~ /\[download\] Destination\: ([^)]+)$/) {
      $video_fn = $1;
      print ">>> $video_fn\n";
    }
    if ($part =~ /\[download\] (.*) has already been downloaded and merged/) {
      $video_fn = $1;
      print ">>> $video_fn\n";
    }
    if ($part =~ /\[ffmpeg\] Merging formats into "(.*)"/) {
      $video_fn = $1;
      print ">>> $video_fn\n";
    }
  }
}

if ($video_fn) {
  print "Video filename: $video_fn\n";
  my $tag = sanitize($video_fn);
  print "creating dataset: $tag\n";
  my @partz = split(/\./, $video_fn);
  my $ext = $partz[$#partz];
  my $new_fn = $tag . '.' . $ext;
  system('mv', $video_fn, $new_fn);
  print "new video filename: $video_fn\n";
  mkdir($tag);
  system('ffmpeg', '-i', $new_fn, "-filter:v", "scale=1024x512", "-vframes", 90000, $tag . '/frame_%05d.png');
  print "created dataset: $tag\n";
}

END {
  chdir('..');
}