forked from dswinton/Batch-H.264-Converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-h264-converter
executable file
·179 lines (133 loc) · 4.79 KB
/
batch-h264-converter
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
#!/usr/bin/php
<?php
/*
Batch H.264 Converter
Copyright 2016 David Swinton
Licensed under GNU GPL v3
*/
function logLine($msg){
echo $msg."\n";
syslog(LOG_INFO,'H.264 Converter: '.$msg);
};
logLine('Batch H.264 Converter Started');
require_once('config.php');
require_once('lib-mediainfo.php');
$dirPath = $argv[1];
if(!$dirPath){
logLine('Error - directory not specified.');
exit;
};
logLine('Directory is: '.$dirPath);
$fileTypes = array('mkv','avi','mp4','divx','m4v','mov','mpg','wmv');
function notify($title,$msg){
global $config;
if($config['pushBulletEmail']){
$postFields = array(
"email" => $config['pushBulletEmail'],
"type" => "note",
"title" => $title,
"body" => $msg,
"sender_name" => "H.264 Converter"
);
$curl = curl_init('https://api.pushbullet.com/v2/pushes');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$config['pushBulletApiKey']]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
return curl_exec($curl);
};
};
logLine('Scanning Directory...');
$dirListing = scandir($dirPath);
logLine('Scan complete. Starting conversion run...');
foreach($dirListing as $d){
$doConvert = false;
$matchStr = strtolower(substr($d,strlen($d)-3,3));
if(in_array($matchStr,$fileTypes)){
$dPath = ($dirPath.$d);
logLine('=== '.$dPath.' ===');
$mediaInfo = new mediaInfo($dPath);
$mediaInfo = json_decode(json_encode($mediaInfo),true);
//print_r($mediaInfo);
//Decision points as to whether we will convert
//Check video format
if($mediaInfo['arrGeneral']['Format'] <> "MPEG-4"){
logLine('ISSUE: Container is not MPEG4 ('.$mediaInfo['arrGeneral']['Format'].')');
$doConvert = true;
};
//Check video codec
if($mediaInfo['arrVideo']['Format'] <> "AVC"){
logLine('ISSUE: Video codec is not AVC/H.264 ('.$mediaInfo['arrVideo']['Format'].')');
$doConvert = true;
};
//Check audio codec
$audio = $mediaInfo['arrAudio'];
reset($audio);
$firstAudioKey = key($audio);
if($mediaInfo['arrAudio'][$firstAudioKey]['Format'] <> "AAC"){
logLine('ISSUE: Audio codec is not AAC ('.$mediaInfo['arrAudio'][$firstAudioKey]['Format'].')');
$doConvert = true;
};
//Need to check what it looks like with multiple tracks / languages
if(count($mediaInfo['arrAudio']) > 1){
logLine('NOPE: More than one audio track');
$doConvert = false;
print_r($mediaInfo);
exit;
};
//Deal with packaging subtitles
if(count($mediaInfo['arrText'])){
//TODO: Remove non-english subtitles
//Currently subtitles will be included as MOV_TEXT so you can turn them on and off
//Probably will never get written as there's little benifit to removing others
};
//Do it
if($doConvert){
$sourceFile = $mediaInfo['arrGeneral']['Complete name'];
$src = pathinfo($sourceFile);
$hash = md5($sourceFile);
$workingFile = $config['dir']['scratch'].$hash.'.mp4';
$destFile = $src['dirname'] . '/' . $src['filename'] . '.mp4';
logLine('Source: '.$sourceFile);
logLine('Scratch: '.$workingFile);
logLine('Dest: '.$destFile);
$timeStart = time();
$ffCmd = 'ffmpeg -i "'.$sourceFile.'" -c:v libx264 -crf '.$config['videoQuality'];
//H.264 quality setting - suggested by @jmckee on the Plex forums
if($config['h264Level']){
$ffCmd .= ' -level '.$config['h264Level'].' ';
};
//Add totally customised arguments to the FFMPEG command
if($config['customFfmpegArgs']){
$ffCmd .= ' '.$config['customFfmpegArgs'].' ';
};
$ffCmd .= ' -preset '.$config['speed'].' -c:a aac -q:a '.$config['audioQuality'].' -c:s mov_text -movflags +faststart -strict -2 "'.$workingFile.'"';
logLine('Running command: '.$ffCmd);
if(!$config['test']){exec($ffCmd);};
if($config['keepInJunkyard']){
logLine('Moving the original source to the junkyard...');
$junkFile = $config['dir']['junkyard'] . $src['basename'];
$junkCmd = 'mv "'.$sourceFile. '" "'.$junkFile.'"';
logLine($junkCmd);
if(!$config['test']){exec($junkCmd);};
}else{
logLine('Deleting original...');
$junkFile = $config['dir']['junkyard'] . $src['basename'];
$rmCmd = 'rm "'.$sourceFile. '"';
logLine($rmCmd);
if(!$config['test']){exec($rmCmd);};
};
logLine('Moving converted file into place of original...');
$moveCmd = 'mv "'.$workingFile. '" "'.$destFile.'"';
logLine($moveCmd);
if(!$config['test']){exec($moveCmd);};
$timeTaken = time() - $timeStart;
notify('Conversion finished',$src['filename'].' ['.round($timeTaken/60).' minutes]');
}else{
logLine('Not converting this file');
};
};
};
logLine('Run complete!');
?>