forked from AnsellC/Encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.php
148 lines (96 loc) · 3.5 KB
/
encode.php
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
<?php
include './vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$binPath = getenv('bin_path');
$watermarkPath = getenv('watermark');
if( !isset($argv[1]) OR !isset($argv[2]) ) {
$source = getenv('source');
$dest = getenv('dest');
} else {
$source = $argv[1];
$dest = $argv[2];
}
echo $source;
$adapter = new Local($source);
$filesystem = new Filesystem($adapter);
$animes = $filesystem->listContents('./');
echo "Found ". count($animes). " anime...\n";
$i = 0;
$total_videos = 0;
foreach($animes AS $anime) {
echo "Processing: \033[0;32m".$anime['path']."\033[0m\n";
$files = $filesystem->listContents($anime['path']);
$animes[$i]['videos'] = $files;
$total_videos += count($files);
$s = 'ffmpeg -i "'. $source .'/'. $files[0]['path'] .'" 2>&1 &';
unset($out);
exec($s, $out);
foreach($out AS $line) {
if (preg_match('/Stream #0/', $line))
echo $line ."\n";
}
echo "Dub encode?(y/n) [default n]: ";
$dub = strtolower(trim(fgets(STDIN)));
if($dub == 'y') {
$animes[$i]['encode_path'] = $anime['path'] .' DUB';
} else {
$animes[$i]['encode_path'] = $anime['path'];
}
echo "Manual or Auto?(a/m) [default m]: ";
$choice = trim(fgets(STDIN));
if ($choice != strtolower('a')) {
echo "SELECT VIDEO: ";
$animes[$i]['video_stream'] = strtolower(trim(fgets(STDIN)));
echo "SELECT AUDIO: ";
$animes[$i]['audio_stream'] = strtolower(trim(fgets(STDIN)));
echo "Burn subs?(y/n) [default y]: ";
$animes[$i]['subs'] = strtolower(trim(fgets(STDIN)));
} else {
//if auto add subs
$animes[$i]['subs'] = 'y';
}
/*if($animes[$i]['subs'] == 'y') {
echo "Subtitle Type?(ass/dvd) [default ass]: ";
}*/
echo "\n\n\n";
$i++;
}
flush();
$i = 1;
$t = 1;
foreach($animes AS $anime) {
$x = 1;
if(!file_exists($dest . '/'. $anime['encode_path'])) {
mkdir($dest . '/'. $anime['encode_path']);
}
foreach($anime['videos'] AS $video) {
$video_path = $source .'/'. $video['path'];
$out_path = $dest .'/'. $anime['encode_path'].'/'. $video['basename'] .'.mp4';
if ( file_exists($out_path) ) {
echo "SKIPPING (file exists): \033[0;32m".$video['basename']."\033[0m\n";
continue;
}
if ($anime['subs'] == 'y')
echo "ENCODING: \033[0;32m".$video['basename']."\033[0m \033[0;31m(SUB)\033[0m {$t} of {$total_videos}\n";
else
echo "ENCODING: \033[0;32m".$video['basename']."\033[0m {$t} of {$total_videos}\n";
$cmd = 'ffmpeg -hide_banner -loglevel warning -i "'. $video_path.'"';
if( isset($anime['video_stream']) AND isset($anime['audio_stream']) ) {
$cmd .= " -map 0:{$anime['video_stream']} -map 0:{$anime['audio_stream']}";
}
$cmd .= ' -c:v libx264 -preset faster -tune animation -crf 23 -profile:v high -level 4.1 -pix_fmt yuv420p -c:a aac -b:a 192k';
if (isset($anime['subs']) AND $anime['subs'] == 'y') {
$cmd .= ' -vf "ass=\''.str_replace(":", "\:", $watermarkPath).'\', subtitles=\''.str_replace(":", "\:", $video_path).'\'"';
} else {
$cmd .= ' -vf "ass=\''.str_replace(":", "\:", $watermarkPath).'\'"';
}
$cmd.= ' "'.$out_path.'"';
$x++;
$t++;
exec($cmd);
}
$i++;
}