forked from emk/subtitles-rs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
video.rs
356 lines (323 loc) · 10.9 KB
/
video.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
//! Tools for working with video files.
use num::rational::Ratio;
use regex::Regex;
use rustc_serialize::{Decodable, Decoder, json};
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::result;
use std::str::{FromStr, from_utf8};
use errors::*;
use lang::Lang;
use time::Period;
/// Information about an MP3 track (optional).
#[derive(Debug, Default)]
#[allow(missing_docs)]
pub struct Id3Metadata {
pub genre: Option<String>,
pub artist: Option<String>,
pub album: Option<String>,
pub track_number: Option<(usize, usize)>,
pub track_name: Option<String>,
pub lyrics: Option<String>,
}
impl Id3Metadata {
fn add_args(&self, cmd: &mut Command) {
if let Some(ref genre) = self.genre {
cmd.arg("-metadata").arg(format!("genre={}", genre));
}
if let Some(ref artist) = self.artist {
cmd.arg("-metadata").arg(format!("artist={}", artist));
}
if let Some(ref album) = self.album {
cmd.arg("-metadata").arg(format!("album={}", album));
}
if let Some((track, total)) = self.track_number {
cmd.arg("-metadata").arg(format!("track={}/{}", track, total));
}
if let Some(ref track_name) = self.track_name {
cmd.arg("-metadata").arg(format!("title={}", track_name));
}
if let Some(ref lyrics) = self.lyrics {
cmd.arg("-metadata").arg(format!("lyrics={}", lyrics));
}
}
}
/// Individual streams inside a video are labelled with a codec type.
#[derive(Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum CodecType {
Audio,
Video,
Subtitle,
Other(String),
}
impl Decodable for CodecType {
fn decode<D: Decoder>(d: &mut D) -> result::Result<Self, D::Error> {
match &try!(d.read_str())[..] {
"audio" => Ok(CodecType::Audio),
"video" => Ok(CodecType::Video),
"subtitle" => Ok(CodecType::Subtitle),
s => Ok(CodecType::Other(s.to_owned())),
}
}
}
/// A wrapper around `Ratio` with custom serialization support.
#[derive(Debug)]
pub struct Fraction(Ratio<u32>);
impl Fraction {
fn decode_parts<D>(d: &mut D) -> result::Result<(u32, u32), D::Error>
where D: Decoder
{
let s = try!(d.read_str());
let re = Regex::new(r"^(\d+)/(\d+)$").unwrap();
let cap = try!(re.captures(&s).ok_or_else(|| {
d.error(&format!("Expected fraction: {}", &s))
}));
Ok((FromStr::from_str(cap.at(1).unwrap()).unwrap(),
FromStr::from_str(cap.at(2).unwrap()).unwrap()))
}
}
impl Decodable for Fraction {
fn decode<D: Decoder>(d: &mut D) -> result::Result<Self, D::Error> {
let (num, denom) = try!(Fraction::decode_parts(d));
if denom == 0 {
Err(d.error("Found fraction with a denominator of 0"))
} else {
Ok(Fraction(Ratio::new(num, denom)))
}
}
}
/// An individual content stream within a video.
#[derive(Debug, RustcDecodable)]
#[allow(missing_docs, dead_code)]
pub struct Stream {
pub index: usize,
pub codec_name: Option<String>,
pub codec_long_name: Option<String>,
pub codec_type: CodecType,
pub codec_time_base: Option<Fraction>,
pub codec_tag_string: String,
pub codec_tag: String,
pub profile: Option<String>,
pub width: Option<usize>,
pub height: Option<usize>,
//has_b_frames
//sample_aspect_ratio
//display_aspect_ratio
pub pix_fmt: Option<String>,
pub sample_rate: Option<f64>,
pub channels: Option<usize>,
pub bits_per_sample: Option<u32>,
//avg_frame_rate
pub time_base: Fraction,
pub start_time: f64,
//duration
pub tags: Option<BTreeMap<String, String>>,
}
impl Stream {
/// Return the language associated with this stream, if we can figure
/// it out.
pub fn language(&self) -> Option<Lang> {
self.tags.as_ref()
.and_then(|tags| tags.get("language"))
.and_then(|lang| Lang::iso639(lang).ok())
}
}
#[test]
fn test_stream_decode() {
let json = "
{
\"index\" : 2,
\"codec_name\" : \"aac\",
\"codec_long_name\" : \"AAC (Advanced Audio Coding)\",
\"codec_type\" : \"audio\",
\"codec_time_base\" : \"1/48000\",
\"codec_tag_string\" : \"[0][0][0][0]\",
\"codec_tag\" : \"0x0000\",
\"sample_rate\" : \"48000.000000\",
\"channels\" : 2,
\"bits_per_sample\" : 0,
\"avg_frame_rate\" : \"0/0\",
\"time_base\" : \"1/1000\",
\"start_time\" : \"0.000000\",
\"duration\" : \"N/A\",
\"tags\" : {
\"language\" : \"eng\"
}
}
";
let stream: Stream = json::decode(json).unwrap();
assert_eq!(CodecType::Audio, stream.codec_type);
assert_eq!(Some(Lang::iso639("en").unwrap()),
stream.language())
}
/// What kind of data do we want to extract, and from what position in the
/// video clip?
pub enum ExtractionSpec {
/// Extract an image at the specified time.
Image(f32),
/// Extract an audio clip covering the specified stream and period.
Audio(Option<usize>, Period, Id3Metadata),
}
impl ExtractionSpec {
/// The earliest time at which we might need to extract data.
fn earliest_time(&self) -> f32 {
match self {
&ExtractionSpec::Image(time) => time,
&ExtractionSpec::Audio(_, period, _) => period.begin(),
}
}
/// Can we combine this extraction with others in a giant batch
/// request?
fn can_be_batched(&self) -> bool {
match self {
// Batch processing of images requires decoding the whole
// video, but we can do a "fast seek" and extract one image
// extremely quickly.
&ExtractionSpec::Image(_) => false,
_ => true,
}
}
/// Figure out what ffmpeg args we would need to extract the requested
/// data. Assume that the "fast seek" feature has been used to start
/// decoding at `time_base`.
fn add_args(&self, cmd: &mut Command, time_base: f32) {
match self {
&ExtractionSpec::Image(time) => {
let scale_filter =
format!("scale=iw*min(1\\,min({}/iw\\,{}/ih)):-1",
240, 160);
cmd.arg("-ss").arg(format!("{}", time - time_base))
.arg("-vframes").arg("1")
.arg("-filter_complex").arg(&scale_filter)
.arg("-f").arg("image2");
}
&ExtractionSpec::Audio(stream, period, ref metadata) => {
if let Some(sid) = stream {
cmd.arg("-map").arg(format!("0:{}", sid));
}
metadata.add_args(cmd);
cmd.arg("-ss").arg(format!("{}", period.begin() - time_base))
.arg("-t").arg(format!("{}", period.duration()));
}
}
}
}
/// Information about what kind of data we want to extract.
pub struct Extraction {
/// The path to extract to.
pub path: PathBuf,
/// What kind of data to extract.
pub spec: ExtractionSpec,
}
impl Extraction {
/// Add the necessary args to `cmd` to perform this extraction.
fn add_args(&self, cmd: &mut Command, time_base: f32) {
self.spec.add_args(cmd, time_base);
cmd.arg(self.path.clone());
}
}
/// Metadata associated with a video.
#[derive(Debug, RustcDecodable)]
struct Metadata {
streams: Vec<Stream>,
}
/// Represents a video file on disk.
#[derive(Debug)]
pub struct Video {
path: PathBuf,
metadata: Metadata,
}
impl Video {
/// Create a new video file, given a path.
pub fn new(path: &Path) -> Result<Video> {
// Ensure we have an actual file name before doing anything else.
try!(path.file_name().ok_or_else(|| {
err_str(format!("Video path does not have a filename: {}",
path.to_string_lossy()))
}));
// Run our probe command.
let cmd = Command::new("ffprobe")
.arg("-v").arg("quiet")
.arg("-show_streams")
.arg("-of").arg("json")
.arg(path)
.output();
let output = try!(cmd);
let stdout = try!(from_utf8(&output.stdout));
debug!("Video metadata: {}", stdout);
let metadata = try!(json::decode(stdout));
Ok(Video { path: path.to_owned(), metadata: metadata })
}
/// Get just the file name of this video file.
pub fn file_name(&self) -> &OsStr {
self.path.file_name().unwrap()
}
/// Get just the file stem of this video file, stripped of any
/// extensions.
pub fn file_stem(&self) -> &OsStr {
self.path.file_stem().unwrap()
}
/// List all the tracks in a video file.
pub fn streams(&self) -> &[Stream] {
&self.metadata.streams
}
/// Choose the best audio for the specified language.
pub fn audio_for(&self, lang: Lang) -> Option<usize> {
self.streams().iter().position(|s| {
s.codec_type == CodecType::Audio &&
s.language() == Some(lang)
})
}
/// Create an extraction command using the specified `time_base`. This
/// allows us to start extractions at any arbitrary point in the video
/// rapidly.
fn extract_command(&self, time_base: f32) -> Command {
let mut cmd = Command::new("ffmpeg");
cmd.arg("-ss").arg(format!("{}", time_base));
cmd.arg("-i").arg(&self.path);
cmd
}
/// Perform a single extraction.
fn extract_one(&self, extraction: &Extraction) -> Result<()> {
let time_base = extraction.spec.earliest_time();
let mut cmd = self.extract_command(time_base);
extraction.add_args(&mut cmd, time_base);
try!(cmd.output());
Ok(())
}
/// Perform a batch extraction. We assume that the extractions are
/// sorted in temporal order.
fn extract_batch(&self, extractions: &[&Extraction]) -> Result<()> {
// Bail early if we have nothing to extract
if extractions.is_empty() { return Ok(()); }
let time_base = extractions[0].spec.earliest_time();
// Build and run our batch extraction command.
let mut cmd = self.extract_command(time_base);
for e in extractions {
assert!(e.spec.can_be_batched());
e.add_args(&mut cmd, time_base);
}
try!(cmd.output());
Ok(())
}
/// Perform a list of extractions as efficiently as possible. We use a
/// batch interface to avoid making too many passes through the file.
/// We assume that the extractions are sorted in temporal order.
pub fn extract(&self, extractions: &[Extraction]) -> Result<()> {
let mut batch: Vec<&Extraction> = vec!();
for e in extractions {
if e.spec.can_be_batched() {
batch.push(e);
} else {
try!(self.extract_one(e));
}
}
for chunk in batch.chunks(20) {
try!(self.extract_batch(chunk));
}
Ok(())
}
}