summaryrefslogtreecommitdiff
path: root/dir-to-movie.pl
blob: ea8992e9e35fcd76fbaf7465c7a13309f4cef2dd (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
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
#!/usr/bin/perl

use Getopt::Long;
use IO::Handle;

STDERR->autoflush(1);
STDOUT->autoflush(1);

my $tag = "pix2pixhd_" . time;
my $module = "pix2pixhd";
my $path = "results";
my $endpoint = undef;
my $prefix = "ren";
GetOptions (
  "tag=s"  => \$tag,
  "module=s" => \$module,
  "path=s" => \$path,
  "endpoint=s" => \$endpoint,
  "prefix=s" => \$prefix,
)
or die("Error in command line arguments\n");

sub read_dir {
  my $path = shift;
  my $files = [];
  opendir my $dir, $path or die "Cannot open directory: $!";
  while (my $file = readdir($dir)) {
    if ($file =~ /\.png$/) {
      push @$files, $file;
    }
  }
  closedir $dir;
  return $files;
}

$tag =~ s/\....$//;

print "tag: $tag\n";
print "module: $module\n";
print "endpoint: $endpoint\n";

if (! -e "$path/$tag") {
  print "usage: $0 [tag]\n";
  exit(1);
}

chdir("$path/$tag");

my $i = 0;


my $lines = read_dir('.');
my $pwd = `pwd`;
chomp $pwd;
$pwd .= '/';
print $pwd . "\n";

mkdir('./tmp');

for my $line (sort @$lines) {
  if ($line !~ /$prefix/) {
    next;
  }
  chomp $line;
  system('ln', '-s', $pwd . $line, sprintf('./tmp/frame_%05d.png', $i));
  $i += 1;
}

if ($i == 0) {
  print "No frames found.\n";
  exit(1);
}

my $parent_dir = "../..";
my $dataset = $tag;
if ($tag =~ /\//) {
  $parent_dir .= "/..";
  $tag =~ s/\//_/g;
} else {
  $dataset = $tag;
}

my $ffmpeg = `ffmpeg -hide_banner -i ./tmp/frame_%05d.png -y -c:v libx264 -preset slow -crf 19 -vf fps=25 -pix_fmt yuv420p -s 1024x512 $parent_dir/renders/$tag.mp4`;

my @ffmpeg_lines = split("\n", $ffmpeg);
for my $line (@ffmpeg_lines) {
  print $line . "\n";
}

#print("upload disabled for now\n");
#if (2 == 1 && defined $endpoint) {
if (defined $endpoint) {
  print("upload to $endpoint\n");
  system("curl",
    "-X", "POST",
    "-F", "module=$module",
    "-F", "activity=live",
    "-F", "generated=true",
    "-F", "dataset=$tag",
    "-F", "datatype=video",
    "-F", "file=\@$parent_dir/renders/$tag.mp4",
    $endpoint
  );
}

print "rendered: $tag.mp4\n";

END {
  system('rm', '-rf', './tmp');
  chdir($parent_dir);
}