-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsamples.rs
More file actions
143 lines (127 loc) · 5.25 KB
/
Copy pathsamples.rs
File metadata and controls
143 lines (127 loc) · 5.25 KB
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
use mp4box::{BoxValue, StructuredData, get_boxes};
use std::fs::File;
fn main() -> anyhow::Result<()> {
// Check if a file path is provided
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} <mp4_file>", args[0]);
std::process::exit(1);
}
let path = &args[1];
let mut file = File::open(path)?;
let size = file.metadata()?.len();
// Parse with decoding enabled to get structured data
let boxes = get_boxes(&mut file, size, true)?;
println!("Analyzing sample tables in: {}", path);
analyze_sample_tables(&boxes, 0);
// Also test the direct parsing example
println!("\nTesting direct parsing example:");
example_direct_parsing()?;
Ok(())
}
fn analyze_sample_tables(boxes: &[mp4box::Box], depth: usize) {
let indent = " ".repeat(depth);
for box_info in boxes {
// Look for sample table boxes
if let Some(decoded) = &box_info.decoded {
match box_info.typ.as_str() {
"stts" => {
println!("{}📊 Decoding Time-to-Sample Box (stts):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured sample timing data", indent);
// In practice, you would parse the structured data here
// For now we show it's working with structured output
}
}
"stsc" => {
println!("{}🗂️ Sample-to-Chunk Box (stsc):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured chunk mapping data", indent);
}
}
"stsz" => {
println!("{}📏 Sample Size Box (stsz):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured sample size data", indent);
}
}
"stco" => {
println!("{}📍 Chunk Offset Box (stco):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured chunk offset data", indent);
}
}
"co64" => {
println!("{}📍 64-bit Chunk Offset Box (co64):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured 64-bit chunk offset data", indent);
}
}
"stss" => {
println!("{}🎯 Sync Sample Box (stss):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured keyframe data", indent);
}
}
"ctts" => {
println!("{}⏰ Composition Time-to-Sample Box (ctts):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured composition offset data", indent);
}
}
"stsd" => {
println!("{}🎬 Sample Description Box (stsd):", indent);
if decoded.starts_with("structured:") {
println!("{} Contains structured codec information", indent);
}
}
_ => {}
}
}
// Recurse into children
if let Some(children) = &box_info.children {
analyze_sample_tables(children, depth + 1);
}
}
}
/// Example of how you would access structured data directly from the registry
fn example_direct_parsing() -> anyhow::Result<()> {
use mp4box::boxes::{BoxHeader, FourCC};
use mp4box::registry::{BoxDecoder, SttsDecoder};
use std::io::Cursor;
// Example: Create a mock STTS box data
// Note: version/flags are handled by the main parser, decoder receives only payload
let mock_stts_data = vec![
0, 0, 0, 2, // entry_count = 2
0, 0, 0, 100, // sample_count = 100
0, 0, 4, 0, // sample_delta = 1024
0, 0, 0, 1, // sample_count = 1
0, 0, 2, 0, // sample_delta = 512
];
let mut cursor = Cursor::new(mock_stts_data);
let header = BoxHeader {
typ: FourCC(*b"stts"),
uuid: None,
size: 28, // 20 bytes data + 8 bytes header
header_size: 8,
start: 0,
};
let decoder = SttsDecoder;
let result = decoder.decode(&mut cursor, &header, Some(0), Some(0))?;
match result {
BoxValue::Structured(StructuredData::DecodingTimeToSample(stts_data)) => {
println!("Parsed STTS data:");
println!(" Version: {}", stts_data.version);
println!(" Flags: {}", stts_data.flags);
println!(" Entry count: {}", stts_data.entry_count);
for (i, entry) in stts_data.entries.iter().enumerate() {
println!(
" Entry {}: {} samples, delta {}",
i, entry.sample_count, entry.sample_delta
);
}
}
_ => println!("Unexpected result type"),
}
Ok(())
}