pub struct VideoReader {
pub mov: MovVideo,
/* private fields */
}decode only.Expand description
A video file opened for reading as ffmpeg -i <file> -f rawvideo -pix_fmt rgb24 -
would: same frames, same order, same pixels.
Fields§
§mov: MovVideoThe track’s timing as ffmpeg computes it.
Implementations§
Source§impl VideoReader
impl VideoReader
Sourcepub fn open(data: Vec<u8>) -> Result<Self, String>
pub fn open(data: Vec<u8>) -> Result<Self, String>
Err for anything outside the supported subset (codec, rotation,
fragmented files, multiple sample descriptions or edits).
Examples found in repository?
6fn main() {
7 let a: Vec<String> = std::env::args().collect();
8 if a.len() < 2 {
9 eprintln!("usage: media_banner FILE");
10 std::process::exit(2);
11 }
12 let data = std::fs::read(&a[1]).expect("read");
13 match threers_codec::ffcompat::reader::VideoReader::open(data) {
14 Ok(r) => {
15 let m = &r.mov;
16 println!(
17 "fps {} tbr {}/{} duration {:.2} start_us {} frames {}",
18 m.banner_fps,
19 m.r_frame_rate.0,
20 m.r_frame_rate.1,
21 m.banner_duration,
22 m.start_us,
23 m.pts.len()
24 );
25 }
26 Err(e) => println!("ERROR {e}"),
27 }
28}More examples
10fn main() {
11 let a: Vec<String> = std::env::args().collect();
12 if a.len() < 3 {
13 eprintln!("usage: video_rgb_pipe IN OUT.rgb");
14 std::process::exit(2);
15 }
16 let r = threers_codec::ffcompat::reader::VideoReader::open(std::fs::read(&a[1]).expect("read"))
17 .unwrap_or_else(|e| {
18 println!("ERROR {e}");
19 std::process::exit(2)
20 });
21 let frames = r.pipe(None);
22 let wanted: BTreeSet<usize> = frames.iter().copied().collect();
23 let mut got = std::collections::BTreeMap::new();
24 if let Err(e) = r.decode_rgb(&wanted, |i, rgb| {
25 got.insert(i, rgb.to_vec());
26 Ok(())
27 }) {
28 println!("ERROR {e}");
29 std::process::exit(2)
30 }
31 let out: Vec<u8> = frames.iter().flat_map(|i| got[i].clone()).collect();
32 let (w, h) = r.size();
33 if a.get(2).map(String::as_str) == Some("--compare") {
34 let reference = std::fs::read(&a[3]).expect("reference");
35 let n = out.len().min(reference.len());
36 let diff = (0..n).filter(|&i| out[i] != reference[i]).count();
37 let maxd = (0..n)
38 .map(|i| (out[i] as i32 - reference[i] as i32).abs())
39 .max()
40 .unwrap_or(0);
41 let first = (0..n).find(|&i| out[i] != reference[i]).map(|i| {
42 (
43 i / (w as usize * h as usize * 3),
44 (i / 3) % w as usize,
45 (i / 3 / w as usize) % h as usize,
46 i % 3,
47 )
48 });
49 println!("{w}x{h} frames ours {} ref {}: {diff} bytes differ (max {maxd}), first (frame, x, y, c) {first:?}", out.len() / (w as usize * h as usize * 3), reference.len() / (w as usize * h as usize * 3));
50 } else {
51 std::fs::write(&a[2], out).expect("write");
52 }
53}Sourcepub fn size(&self) -> (u32, u32)
pub fn size(&self) -> (u32, u32)
Size of the piped pictures (after autorotation).
Examples found in repository?
10fn main() {
11 let a: Vec<String> = std::env::args().collect();
12 if a.len() < 3 {
13 eprintln!("usage: video_rgb_pipe IN OUT.rgb");
14 std::process::exit(2);
15 }
16 let r = threers_codec::ffcompat::reader::VideoReader::open(std::fs::read(&a[1]).expect("read"))
17 .unwrap_or_else(|e| {
18 println!("ERROR {e}");
19 std::process::exit(2)
20 });
21 let frames = r.pipe(None);
22 let wanted: BTreeSet<usize> = frames.iter().copied().collect();
23 let mut got = std::collections::BTreeMap::new();
24 if let Err(e) = r.decode_rgb(&wanted, |i, rgb| {
25 got.insert(i, rgb.to_vec());
26 Ok(())
27 }) {
28 println!("ERROR {e}");
29 std::process::exit(2)
30 }
31 let out: Vec<u8> = frames.iter().flat_map(|i| got[i].clone()).collect();
32 let (w, h) = r.size();
33 if a.get(2).map(String::as_str) == Some("--compare") {
34 let reference = std::fs::read(&a[3]).expect("reference");
35 let n = out.len().min(reference.len());
36 let diff = (0..n).filter(|&i| out[i] != reference[i]).count();
37 let maxd = (0..n)
38 .map(|i| (out[i] as i32 - reference[i] as i32).abs())
39 .max()
40 .unwrap_or(0);
41 let first = (0..n).find(|&i| out[i] != reference[i]).map(|i| {
42 (
43 i / (w as usize * h as usize * 3),
44 (i / 3) % w as usize,
45 (i / 3 / w as usize) % h as usize,
46 i % 3,
47 )
48 });
49 println!("{w}x{h} frames ours {} ref {}: {diff} bytes differ (max {maxd}), first (frame, x, y, c) {first:?}", out.len() / (w as usize * h as usize * 3), reference.len() / (w as usize * h as usize * 3));
50 } else {
51 std::fs::write(&a[2], out).expect("write");
52 }
53}Sourcepub fn seeks_for(&self, wanted: &BTreeSet<usize>) -> bool
pub fn seeks_for(&self, wanted: &BTreeSet<usize>) -> bool
Indices (into MovVideo::pts) of the frames the CLI writes for
-ss A -i f -ss B, (A, B) in microseconds.
Where decoding can start for the samples in need (decode index ->
frame): one sync sample before the last sync sample at or before the
first needed one. The extra GOP covers open-GOP leading pictures (a
B-frame shown before its I-frame references the previous GOP); with
fewer sync samples than that, the stream start. Without this every
call decodes from the start of the stream, which makes extracting a
long video chunk by chunk quadratic.
Whether decode_rgb of wanted starts after the
stream start (diagnostics / tests).
Sourcepub fn pipe(&self, seek: Option<(i64, i64)>) -> Vec<usize>
pub fn pipe(&self, seek: Option<(i64, i64)>) -> Vec<usize>
Which frames the CLI writes to its pipe, as indices into mov.pts, for
input and output seeks -ss A -i f -ss B given as (A, B) in
microseconds.
Examples found in repository?
10fn main() {
11 let a: Vec<String> = std::env::args().collect();
12 if a.len() < 3 {
13 eprintln!("usage: video_rgb_pipe IN OUT.rgb");
14 std::process::exit(2);
15 }
16 let r = threers_codec::ffcompat::reader::VideoReader::open(std::fs::read(&a[1]).expect("read"))
17 .unwrap_or_else(|e| {
18 println!("ERROR {e}");
19 std::process::exit(2)
20 });
21 let frames = r.pipe(None);
22 let wanted: BTreeSet<usize> = frames.iter().copied().collect();
23 let mut got = std::collections::BTreeMap::new();
24 if let Err(e) = r.decode_rgb(&wanted, |i, rgb| {
25 got.insert(i, rgb.to_vec());
26 Ok(())
27 }) {
28 println!("ERROR {e}");
29 std::process::exit(2)
30 }
31 let out: Vec<u8> = frames.iter().flat_map(|i| got[i].clone()).collect();
32 let (w, h) = r.size();
33 if a.get(2).map(String::as_str) == Some("--compare") {
34 let reference = std::fs::read(&a[3]).expect("reference");
35 let n = out.len().min(reference.len());
36 let diff = (0..n).filter(|&i| out[i] != reference[i]).count();
37 let maxd = (0..n)
38 .map(|i| (out[i] as i32 - reference[i] as i32).abs())
39 .max()
40 .unwrap_or(0);
41 let first = (0..n).find(|&i| out[i] != reference[i]).map(|i| {
42 (
43 i / (w as usize * h as usize * 3),
44 (i / 3) % w as usize,
45 (i / 3 / w as usize) % h as usize,
46 i % 3,
47 )
48 });
49 println!("{w}x{h} frames ours {} ref {}: {diff} bytes differ (max {maxd}), first (frame, x, y, c) {first:?}", out.len() / (w as usize * h as usize * 3), reference.len() / (w as usize * h as usize * 3));
50 } else {
51 std::fs::write(&a[2], out).expect("write");
52 }
53}Sourcepub fn decode_rgb(
&self,
wanted: &BTreeSet<usize>,
f: impl FnMut(usize, &[u8]) -> Result<(), String>,
) -> Result<(), String>
pub fn decode_rgb( &self, wanted: &BTreeSet<usize>, f: impl FnMut(usize, &[u8]) -> Result<(), String>, ) -> Result<(), String>
Decode once, calling f(index, rgb24) for each wanted frame index
(presentation order, ascending).
Examples found in repository?
10fn main() {
11 let a: Vec<String> = std::env::args().collect();
12 if a.len() < 3 {
13 eprintln!("usage: video_rgb_pipe IN OUT.rgb");
14 std::process::exit(2);
15 }
16 let r = threers_codec::ffcompat::reader::VideoReader::open(std::fs::read(&a[1]).expect("read"))
17 .unwrap_or_else(|e| {
18 println!("ERROR {e}");
19 std::process::exit(2)
20 });
21 let frames = r.pipe(None);
22 let wanted: BTreeSet<usize> = frames.iter().copied().collect();
23 let mut got = std::collections::BTreeMap::new();
24 if let Err(e) = r.decode_rgb(&wanted, |i, rgb| {
25 got.insert(i, rgb.to_vec());
26 Ok(())
27 }) {
28 println!("ERROR {e}");
29 std::process::exit(2)
30 }
31 let out: Vec<u8> = frames.iter().flat_map(|i| got[i].clone()).collect();
32 let (w, h) = r.size();
33 if a.get(2).map(String::as_str) == Some("--compare") {
34 let reference = std::fs::read(&a[3]).expect("reference");
35 let n = out.len().min(reference.len());
36 let diff = (0..n).filter(|&i| out[i] != reference[i]).count();
37 let maxd = (0..n)
38 .map(|i| (out[i] as i32 - reference[i] as i32).abs())
39 .max()
40 .unwrap_or(0);
41 let first = (0..n).find(|&i| out[i] != reference[i]).map(|i| {
42 (
43 i / (w as usize * h as usize * 3),
44 (i / 3) % w as usize,
45 (i / 3 / w as usize) % h as usize,
46 i % 3,
47 )
48 });
49 println!("{w}x{h} frames ours {} ref {}: {diff} bytes differ (max {maxd}), first (frame, x, y, c) {first:?}", out.len() / (w as usize * h as usize * 3), reference.len() / (w as usize * h as usize * 3));
50 } else {
51 std::fs::write(&a[2], out).expect("write");
52 }
53}Auto Trait Implementations§
impl Forget for VideoReader
impl Freeze for VideoReader
impl Move for VideoReader
impl RefUnwindSafe for VideoReader
impl Send for VideoReader
impl Sync for VideoReader
impl Unpin for VideoReader
impl UnsafeUnpin for VideoReader
impl UnwindSafe for VideoReader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more