mirror of
https://github.com/alfg/mp4-rust.git
synced 2025-01-03 08:58:40 +00:00
Update main.rs.
This commit is contained in:
parent
5d11bf9c78
commit
9994a0a6a7
1 changed files with 31 additions and 19 deletions
50
src/main.rs
50
src/main.rs
|
@ -3,46 +3,52 @@ use std::io::{BufReader, Read, SeekFrom};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
// struct Box {
|
#[derive(Debug)]
|
||||||
// name: String,
|
struct Box {
|
||||||
// size: u32,
|
name: String,
|
||||||
// offset: u32,
|
size: u32,
|
||||||
// }
|
offset: u32,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
// Using BufReader.
|
// Using BufReader.
|
||||||
let mut f = File::open("tears-of-steel-2s.mp4")?;
|
let f = File::open("tears-of-steel-2s.mp4")?;
|
||||||
let filesize = f.metadata().unwrap().len();
|
let filesize = f.metadata().unwrap().len();
|
||||||
println!("{:?}", filesize);
|
|
||||||
let mut reader = BufReader::new(f);
|
let mut reader = BufReader::new(f);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
|
||||||
let mut offset = 0u64;
|
let mut offset = 0u64;
|
||||||
|
|
||||||
while offset < filesize {
|
while offset < filesize {
|
||||||
|
|
||||||
// reader.seek(SeekFrom::Current(40 + 2872360));
|
// Seek to offset.
|
||||||
reader.seek(SeekFrom::Current(offset as i64));
|
let _r = reader.seek(SeekFrom::Current(offset as i64));
|
||||||
|
|
||||||
|
// Create and read to buf.
|
||||||
let mut buf = [0u8;8];
|
let mut buf = [0u8;8];
|
||||||
let n = reader.read(&mut buf);
|
let _n = reader.read(&mut buf);
|
||||||
|
|
||||||
|
// Get size.
|
||||||
let s = buf[0..4].try_into().unwrap();
|
let s = buf[0..4].try_into().unwrap();
|
||||||
let size = u32::from_be_bytes(s);
|
let size = u32::from_be_bytes(s);
|
||||||
|
|
||||||
|
// Exit loop if size is 0.
|
||||||
|
if size == 0 { break; }
|
||||||
|
|
||||||
|
// Get box type string.
|
||||||
let t = buf[4..8].try_into().unwrap();
|
let t = buf[4..8].try_into().unwrap();
|
||||||
let typ = match std::str::from_utf8(t) {
|
let typ = match std::str::from_utf8(t) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
|
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Exit loop if size is 0.
|
// Make Box struct and add to vector.
|
||||||
if size == 0 { break; }
|
let b = Box{
|
||||||
|
name: typ.try_into().expect("asdf"),
|
||||||
// println!("{}", buf.len());
|
size: size,
|
||||||
// println!("{:?}", buf);
|
offset: offset as u32,
|
||||||
println!("{:?}", size);
|
};
|
||||||
println!("{:?}", typ);
|
v.push(b);
|
||||||
|
|
||||||
// This will find all boxes, including nested boxes.
|
// This will find all boxes, including nested boxes.
|
||||||
// let mut offset = match size {
|
// let mut offset = match size {
|
||||||
|
@ -51,10 +57,16 @@ fn main() -> std::io::Result<()> {
|
||||||
// };
|
// };
|
||||||
// assert!(offset <= size);
|
// assert!(offset <= size);
|
||||||
|
|
||||||
|
// Increment offset.
|
||||||
offset = (size - 8) as u64;
|
offset = (size - 8) as u64;
|
||||||
println!("skip {:?}\n", offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print results.
|
||||||
|
for a in v {
|
||||||
|
println!("{:?}", a);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done.
|
||||||
println!("done");
|
println!("done");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
Loading…
Reference in a new issue