-
Notifications
You must be signed in to change notification settings - Fork 2
/
converter.rs
358 lines (312 loc) · 11.7 KB
/
converter.rs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
extern crate regex;
use std::convert::Into;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::str;
use crate::lib::{Error, Result};
use mysql::prelude::Queryable;
use mysql::PooledConn;
use mysql::Statement;
macro_rules! regex(
($s:expr) => (regex::Regex::new($s).unwrap());
);
/// Converter containing all necessary methods to merge, extract and convert audio with/from video files
pub struct Converter<'a> {
ffmpeg_dir: PathBuf,
mp3_quality: &'a i16,
}
/// Struct containing file information needed for progress calculation
struct FileInfo {
duration: f32, // seconds
frames: f32,
}
impl<'a> Converter<'a> {
pub fn new(ffmpeg_dir: &'a str, mp3_quality: &'a i16) -> Converter<'a> {
debug!("ffmpeg dir: {}", ffmpeg_dir);
Converter {
ffmpeg_dir: PathBuf::from(ffmpeg_dir),
mp3_quality: mp3_quality,
}
}
/// Run a self-test checking for ffmpeg binaries
/// Returns true on success
pub fn startup_test(&self) -> bool {
info!("Testing converter settings");
let mut result = self.test_cmd("ffmpeg");
match result {
Ok(v) => {
if v {
result = self.test_cmd("ffprobe");
}
}
_ => {}
}
match result {
Ok(v) => v,
Err(e) => {
error!("Error on converter self test: {:?}", e);
false
}
}
}
/// Calls the provided binary of ffmpeg, expecting a '[binary] version] response on stdout
/// Returns true on success
fn test_cmd(&self, command: &str) -> Result<bool> {
let mut cmd = self.create_ffmpeg_base(command);
cmd.arg("-version");
match cmd.spawn() {
Err(why) => {
error!("Error on converter test for {}: {}", command, why);
return Ok(false);
}
Ok(mut process) => {
let mut stdout_buffer = BufReader::new(process.stdout.take().unwrap());
let mut stdout: String = String::new();
stdout_buffer.read_to_string(&mut stdout)?;
let mut stderr_buffer = BufReader::new(process.stderr.take().unwrap());
let mut stderr: String = String::new();
stderr_buffer.read_to_string(&mut stderr)?;
process.wait()?;
trace!("{} stdout: {}", command, stdout);
if !stderr.is_empty() {
warn!("{} test stderr: {}", command, stderr);
return Ok(false);
}
Ok(stdout.contains(&format!("{} version", command)))
}
}
}
/// Merge audo & video files to one
/// As ffmpeg uses \r for progress updates, we'll have to read untill this delimiter
/// ffmpeg prints only to stderr
pub fn merge_files(
&self,
qid: &u64,
video_file: &Path,
audio_file: &Path,
output_file: &Path,
conn: &mut PooledConn,
) -> Result<()> {
let file_info = self.get_file_info(video_file)?;
trace!("Total frames: {}", file_info.frames);
let mut child = self.run_merge_cmd(audio_file, video_file, output_file)?;
trace!("started merge process");
let mut stdout = BufReader::new(child.stderr.take().unwrap());
let statement = self.prepare_progress_updater(conn);
let re = regex!(r"frame=\s*(\d+)");
let mut buf = vec![];
buf.reserve(128);
let mut cur_frame: f32;
loop {
match stdout.read_until(b'\r', &mut buf) {
Ok(0) => break,
Ok(_) => {}
Err(why) => {
error!("couldn't read cmd stdout: {}", why);
panic!();
}
}
{
let line = str::from_utf8(&buf).unwrap();
debug!("ffmpeg: {}", line);
if let Some(cap) = re.captures(&line) {
debug!("frame: {}", cap.get(1).unwrap().as_str());
cur_frame = cap.get(1).unwrap().as_str().parse::<f32>().unwrap();
self.update_progress(
conn,
&statement,
format!("{:.2}", (cur_frame / file_info.frames) * 100.0),
qid,
)?;
}
}
buf.clear();
}
child.wait()?;
Ok(())
}
/// Extract audio from video files
/// If set audio will be converted to mp3 on the fly
pub fn extract_audio(
&self,
qid: &u64,
video_file: &Path,
output_file: &Path,
convert_mp3: bool,
conn: &mut PooledConn,
) -> Result<()> {
let file_info = self.get_file_info(video_file)?;
debug!("duration: {}", file_info.duration);
let mut child;
if convert_mp3 {
child = self.run_audio_extract_to_mp3(video_file, output_file)?;
} else {
child = self.run_audio_extract(video_file, output_file)?;
}
let mut stdout = BufReader::new(child.stderr.take().unwrap());
let statement = self.prepare_progress_updater(conn);
let re = regex!(r"time=(\d+):(\d+):(\d+.?\d*)");
let mut buf = vec![];
buf.reserve(128);
loop {
match stdout.read_until(b'\r', &mut buf) {
Ok(0) => break,
Ok(_) => {}
Err(why) => {
error!("couldn't read cmd stdout: {}", why);
panic!();
}
}
{
let line = str::from_utf8(&buf).unwrap();
debug!("ffmpeg: {}", line);
if let Some(cap) = re.captures(&line) {
let mut seconds: i32 =
cap.get(2).unwrap().as_str().parse::<i32>().unwrap() * 60; // minutes
seconds += cap.get(1).unwrap().as_str().parse::<i32>().unwrap() * 60 * 60; // hours
let seconds: f32 =
seconds as f32 + cap.get(3).unwrap().as_str().parse::<f32>().unwrap();
self.update_progress(
conn,
&statement,
format!("{:.2}", (seconds / file_info.duration) * 100.0),
qid,
)?;
}
}
buf.clear();
}
child.wait()?;
Ok(())
}
/// Retrive file information
fn get_file_info(&self, video_file: &Path) -> Result<FileInfo> {
let stdout = self.run_file_probe(video_file)?;
let regex_duration = regex!(r"duration=(\d+\.?\d*)");
let regex_fps = regex!(r"r_frame_rate=(\d+)/(\d+)");
if let Some(cap) = regex_duration.captures(&stdout) {
let mut file_info = FileInfo {
duration: -1.0,
frames: -1.0,
};
file_info.duration = cap.get(1).unwrap().as_str().parse::<f32>().unwrap();
if let Some(cap) = regex_fps.captures(&stdout) {
let x: f32 = cap.get(1).unwrap().as_str().parse::<f32>().unwrap();
let y: f32 = cap.get(2).unwrap().as_str().parse::<f32>().unwrap();
file_info.frames = (x / y) * file_info.duration;
}
Ok(file_info)
} else {
Err(Error::FFMPEGError(format!(
"Couldn't get max frames: {}",
stdout
)))
}
}
/// Runs a file probe and returns its output, used in progress calculation
fn run_file_probe(&self, video_file: &Path) -> Result<String> {
let mut command = self.create_ffmpeg_base("ffprobe");
command.args(&["-select_streams", "0"]);
command.args(&["-show_entries", "format=duration:stream=r_frame_rate"]);
command.args(&["-of", "default=noprint_wrappers=1"]);
command.arg(video_file);
match command.spawn() {
Err(why) => Err(why.into()),
Ok(mut process) => {
let mut stdout_buffer = BufReader::new(process.stdout.take().unwrap());
let mut stdout: String = String::new();
stdout_buffer.read_to_string(&mut stdout)?;
let mut stderr_buffer = BufReader::new(process.stderr.take().unwrap());
let mut stderr: String = String::new();
stderr_buffer.read_to_string(&mut stderr)?;
process.wait()?;
debug!("ffprobe: {}", stdout);
debug!("ffprobe err: {}", stderr);
Ok(stdout)
}
}
}
/// Merges an audio & an video file together.
/// ffmpeg uses \r as \n
fn run_merge_cmd(
&self,
audio_file: &Path,
video_file: &Path,
output_file: &Path,
) -> Result<Child> {
let mut command = self.create_ffmpeg_base("ffmpeg");
command.args(&["-threads", "0"]);
command.arg("-i");
command.arg(video_file);
command.arg("-i");
command.arg(audio_file);
command.args(&["-map", "0"]);
command.args(&["-map", "1"]);
command.args(&["-codec", "copy"]);
command.arg("-shortest");
command.arg(output_file);
//-stats -threads 0 -i "{}" -i "{}" -map 0 -map 1 -codec copy -shortest "{}"
debug!("Cmd: {:?}", command);
match command.spawn() {
Err(why) => Err(why.into()),
Ok(process) => Ok(process),
}
}
///Create a ffmpeg instance with the audio extract cmd
fn run_audio_extract(&self, video_file: &Path, output_file: &Path) -> Result<Child> {
let mut command = self.create_ffmpeg_base("ffmpeg");
command.args(&["-threads", "0"]);
command.arg("-i");
command.arg(video_file);
command.args(&["-vn", "-acodec", "copy"]);
command.arg(output_file);
match command.spawn() {
Err(why) => Err(why.into()),
Ok(process) => Ok(process),
}
}
fn run_audio_extract_to_mp3(&self, video_file: &Path, output_file: &Path) -> Result<Child> {
let mut command = self.create_ffmpeg_base("ffmpeg");
command.args(&["-threads", "0"]);
command.arg("-i");
command.arg(video_file);
command.args(&["-codec:a", "libmp3lame"]);
command.args(&["-qscale:a", &self.mp3_quality.to_string()]);
command.arg(output_file);
match command.spawn() {
Err(why) => Err(why.into()),
Ok(process) => Ok(process),
}
}
/// Create FFMPEG basic command
/// executable is the called ffmpeg binary
fn create_ffmpeg_base(&self, executable: &str) -> Command {
let mut cmd = Command::new(self.ffmpeg_dir.join(executable));
cmd.args(&["-v", "info"]); // below we're not getting any frame updates, rendering every progress update useless
cmd.stdin(Stdio::null());
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
cmd
}
// MyPooledConn does only live when MyOpts is alive -> lifetime needs to be declared
fn prepare_progress_updater(&'a self, conn: &'a mut PooledConn) -> Statement {
// no livetime needed: struct livetime used
conn.prep("UPDATE querydetails SET progress = ? WHERE qid = ?")
.unwrap()
}
///updater called from the stdout progress
fn update_progress(
&self,
conn: &'a mut PooledConn,
stmt: &Statement,
progress: String,
qid: &u64,
) -> Result<()> {
trace!("updating progress {}", progress);
conn.exec_drop(stmt, (&progress, qid))?;
Ok(())
}
}