-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffbatch.cmd
219 lines (193 loc) · 6.99 KB
/
ffbatch.cmd
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
rem /* --- mp4 encoder ---
@rem This batch file calls itself as javascript -- see js part below
@rem Set the correct path of ffmpeg executable in line 25.
@echo off
rem ------------------------------
rem Drop file or folder to script
rem ------------------------------
rem Recodes files to the same directory with .mp4 extension if not exists
rem Skips existing file
rem runs "c:\progs\Video Encoders\FFmpeg\64-bit\ffmpeg.exe" -i %1 -vcodec mpeg4 -b:v 32M -y %1.mp4
rem returns only when finished
rem -o overwrite existing files
rem -b <bitrate> output bitrate in bit/s, you may use k or M postfixes, e.g. 32M (default)
rem -p <n> number of parallel processes (default 3)
rem -f <pattern> RE pattern to filter processed files
rem Filenames must not contain spaces!
cscript //E:jscript "%~dp0\ffbatch.cmd" %1 %2 %3
pause
exit
rem Javascript part */ = 0;
// Global settings
var commandbase = "\"c:\\progs\\Video Encoders\\FFmpeg\\64-bit\\ffmpeg.exe\"";
var outputextension = 'mp4'; // Ehhez még hozzá fogja fűzni a bitrate értékét is.
var exts = ['mpeg', 'mpg', 'avi', 'mkv', 'mov', 'mxf'];
var ffoptions = '-vtag xvid -vf yadif=3:1,mcdeint=2:1';
var charmap = [
'á,é,í,ó,ö,ő,ú,ü,ű,ć,č,ď,ĺ,ł,š,ś,ż,ž,ą,ę,ţ,ç',
'a,e,i,o,o,o,u,u,u,c,c,d,l,l,s,s,z,z,a,e,t,c'];
// Default option values
var ps = 3; // Párhuzamos processzek maximális száma
var f_overwrite = 0;
var f_bitrate = '32M';
var pattern = '';
Array.prototype.indexOf = function(value) {
for(var i=0;i<this.length;i++) {
if(this[i]==value) return i;
}
return -1;
};
// Global objects and constants
var ForReading = 1;
var WshRunning = 0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var WshShell = new ActiveXObject("WScript.Shell");
var comspec = WshShell.ExpandEnvironmentStrings("%comspec%");
var re = null;
// Determine input file
if(WScript.Arguments.length<1) {
WScript.Echo("Using: mp4re filename options");
WScript.Echo("Options:");
WScript.Echo("\t-o overwrite existing result file");
WScript.Echo("\t-b <bitrate>\toutput bitrate in bit/s, you may use k or M postfixes,\n\t\t\te.g. 32M (default)");
WScript.Echo("\t-b <n>\tnumber of parallel processes (default 3)");
WScript.Echo("\t-f <pattern>\tRE pattern to filter processed files");
WScript.Echo("Filename may be a directory name, in this case all files in it \nwill be processed. Filenames must not contain spaces!");
WScript.Quit(1);
}
var filename = WScript.Arguments.Item(0);
// Determine options
for(var i=1;i<WScript.Arguments.length;i++) {
if(WScript.Arguments.Item(i)=='-o') f_overwrite = 1;
if(WScript.Arguments.Item(i)=='-b') f_bitrate = WScript.Arguments.Item(++i);
if(WScript.Arguments.Item(i)=='-p') ps = WScript.Arguments.Item(++i);
if(WScript.Arguments.Item(i)=='-f') pattern = WScript.Arguments.Item(++i);
}
if(pattern) re = new RegExp(pattern);
// Detect directory
if(fso.FolderExists(filename)) {
WScript.Echo(filename + " is a folder.");
var folderspec = filename;
var folder = fso.GetFolder(folderspec);
var fc = new Enumerator(folder.files);
var processes = new Array(ps);
for(;!fc.atEnd(); fc.moveNext()) {
var filename1 = fc.item().Name;
var ext = fso.GetExtensionName(filename1).toLowerCase();
// Skip unsupported extensions
if(exts.indexOf(ext)==-1) continue;
// Skip processed files
if(filename1.indexOf('.'+f_bitrate+'.')!=-1) continue;
// Skip filtered files
if(pattern && !re.test(filename1)) continue;
var path1 = folderspec+'\\'+filename1;
// Waiting for empty slot
//var p = waitForFree(processes);
var oExec = process_file(path1, f_overwrite, f_bitrate);
//processes[p] = oExec;
}
// Waiting for finish all process
//waitForFinish(processes);
WScript.Echo("Folder process completed.\n");
WScript.Quit(0);
}
// Detect one file
if(!fso.FileExists(filename)) {
WScript.Echo("Input file " + filename + " does not exist, skipping.");
WScript.Quit(1);
}
var oExec = process_file(filename, f_overwrite, f_bitrate);
// wait for complete
if(oExec) while(oExec.Status == WshRunning) WScript.Sleep(100);
/*-------------------------------------------------------------------------------*/
/*
* Visszaadja a process tömb első üres elemét.
* Ha nincs, vár egy processs befejezéséig
*/
function waitForFree(processes) {
var l = processes.length;
var s = 0;
var f = -1;
while((f = firstFree(processes))==-1) {
WScript.Sleep(100);
if((s++ % 10)==0) WScript.StdOut.Write(l);
}
return f;
}
/*
* Vár az összes process befejezéséig
*/
function waitForFinish(processes) {
var l = processes.length;
for(var i=0;i<l;i++) {
p = processes[i];
if(!p) continue;
var s = 0;
while(p.Status == WshRunning) {
WScript.Sleep(100);
if((s++ % 10)==0) WScript.StdOut.Write('.'+l);
}
}
}
/*
* Vissazadja a process tömb első üres vagy már nem futó indexét
* -1, ha nincs üres eleme
*/
function firstFree(processes) {
for(var i=0;i<processes.length;i++) {
p = processes[i];
if(!p) return i;
if(p.Status != WshRunning) {
processes[i] = false;
return i;
}
}
return -1;
}
/*
* Átkódol egy fájlt, új névvel
*
* @param filename -- kódolandó fájl neve
* @param f_overwrite -- 1=meglévő fájlt felülírja
* @param f_bitrate -- pl. 192k
* @return processz-azonosító vagy null
*/
function process_file(filename, f_overwrite, f_bitrate) {
var ext = fso.GetExtensionName(filename).toLowerCase();
var inputpath = fso.getParentFolderName(filename);
WshShell.CurrentDirectory = inputpath;
// Determine output file
var ext = fso.GetExtensionName(filename1).toLowerCase();
var filename_base = filename.replace(new RegExp("\\."+ext+"$"), "");
var filename_base = mapChars(filename_base.toLowerCase(), charmap);
var filename2 = filename_base + '.'+f_bitrate+'.'+outputextension;
if(fso.FileExists(filename2)) {
if(f_overwrite) {
WScript.Echo("Output file " + filename2 + " exist, overwriting.");
}
else {
WScript.Echo("Output file " + filename2 + " exist, skipping.");
return null;
}
}
// Running job, e.g ... -i %1 -vcodec mpeg4 -b:v 16M -y %1.16M.mp4
var command = commandbase + ' -i ' + filename + ' -vcodec mpeg4 -b:v ' + f_bitrate + ' -y ' + filename2;
WScript.Echo("\nProcessing file " + filename);
WScript.Echo(command);
var oExec = WshShell.Run(command, 4, true);
return oExec;
}
/**
* A stringben lecseréli a cseretérkép elemeit (egy menetes)
* @param s -- a bemeneti string
* @param map -- kételemű tömb, első eleme a lecserélendő elemek stringje ,-kel elválasztva, második hasonlóképpen a csereértékek
* Ha a csereértékek kevesebben vannak, hiba keletkezik
*/
function mapChars(s, map) {
var aa = charmap[0].split(/,/);
var bb = charmap[1].split(/,/);
for(var i=0;i<aa.length;i++) {
s = s.replace(aa[i], bb[i]);
}
return s;
}