|
1 | | -use ffmpeg::format; |
2 | | -use mediafoundation_ffmpeg::{H264StreamMuxer, MuxerConfig}; |
3 | | -use std::path::PathBuf; |
4 | | - |
5 | | -/// Example of using H264StreamMuxer with an existing FFmpeg output context |
6 | | -/// This demonstrates how to integrate with MP4File or similar structures |
7 | | -fn example_with_shared_output() -> Result<(), Box<dyn std::error::Error>> { |
8 | | - // Initialize FFmpeg |
9 | | - ffmpeg::init()?; |
10 | | - |
11 | | - // Create an output context (this would normally be owned by MP4File) |
12 | | - let output_path = PathBuf::from("output.mp4"); |
13 | | - let mut output = format::output(&output_path)?; |
14 | | - |
15 | | - // Configure the H264 muxer |
16 | | - let config = MuxerConfig { |
17 | | - width: 1920, |
18 | | - height: 1080, |
19 | | - fps: 30, |
20 | | - bitrate: 5_000_000, // 5 Mbps |
21 | | - }; |
22 | | - |
23 | | - // Add the H264 stream and create the muxer |
24 | | - // Note: We need to add the stream before writing the header |
25 | | - let mut h264_muxer = H264StreamMuxer::add_stream(&mut output, config)?; |
26 | | - |
27 | | - // You might also have other streams (like audio) added to the same output |
28 | | - // ... add audio stream here if needed ... |
29 | | - |
30 | | - // Write the header after all streams are added |
31 | | - output.write_header()?; |
32 | | - |
33 | | - // Now you can write H264 samples from MediaFoundation |
| 1 | +fn main() { |
34 | 2 | #[cfg(windows)] |
35 | | - { |
36 | | - // Example: Write samples from MediaFoundation |
37 | | - // let sample: IMFSample = get_sample_from_media_foundation(); |
38 | | - // h264_muxer.write_sample(&sample)?; |
39 | | - } |
| 3 | + win::main(); |
| 4 | +} |
40 | 5 |
|
41 | | - // Or write raw H264 data |
42 | | - let example_h264_data = vec![0, 0, 0, 1, 0x65]; // Example keyframe NAL |
43 | | - h264_muxer.write_h264_data( |
44 | | - &example_h264_data, |
45 | | - 0, // pts in microseconds |
46 | | - 0, // dts in microseconds |
47 | | - 33333, // duration in microseconds (1/30 fps) |
48 | | - true, // is_keyframe |
49 | | - &mut output, |
50 | | - )?; |
| 6 | +#[cfg(windows)] |
| 7 | +mod win { |
| 8 | + use cap_mediafoundation_ffmpeg::{H264StreamMuxer, MuxerConfig}; |
| 9 | + use ffmpeg::format; |
| 10 | + use std::path::PathBuf; |
51 | 11 |
|
52 | | - // Finish the muxer (doesn't write trailer) |
53 | | - h264_muxer.finish()?; |
| 12 | + /// Example of using H264StreamMuxer with an existing FFmpeg output context |
| 13 | + /// This demonstrates how to integrate with MP4File or similar structures |
| 14 | + fn example_with_shared_output() -> Result<(), Box<dyn std::error::Error>> { |
| 15 | + // Initialize FFmpeg |
| 16 | + ffmpeg::init()?; |
54 | 17 |
|
55 | | - // Write the trailer (this would be done by MP4File::finish()) |
56 | | - output.write_trailer()?; |
| 18 | + // Create an output context (this would normally be owned by MP4File) |
| 19 | + let output_path = PathBuf::from("output.mp4"); |
| 20 | + let mut output = format::output(&output_path)?; |
57 | 21 |
|
58 | | - Ok(()) |
59 | | -} |
| 22 | + // Configure the H264 muxer |
| 23 | + let config = MuxerConfig { |
| 24 | + width: 1920, |
| 25 | + height: 1080, |
| 26 | + fps: 30, |
| 27 | + bitrate: 5_000_000, // 5 Mbps |
| 28 | + }; |
60 | 29 |
|
61 | | -/// Example showing how this would integrate with an MP4File-like structure |
62 | | -struct MP4FileExample { |
63 | | - output: format::context::Output, |
64 | | - h264_muxer: Option<H264StreamMuxer>, |
65 | | - is_finished: bool, |
66 | | -} |
| 30 | + // Add the H264 stream and create the muxer |
| 31 | + // Note: We need to add the stream before writing the header |
| 32 | + let mut h264_muxer = H264StreamMuxer::add_stream(&mut output, config)?; |
67 | 33 |
|
68 | | -impl MP4FileExample { |
69 | | - fn new(output_path: PathBuf, video_config: MuxerConfig) -> Result<Self, ffmpeg::Error> { |
70 | | - let mut output = format::output(&output_path)?; |
| 34 | + // You might also have other streams (like audio) added to the same output |
| 35 | + // ... add audio stream here if needed ... |
71 | 36 |
|
72 | | - // Add H264 stream and create muxer |
73 | | - let h264_muxer = H264StreamMuxer::add_stream(&mut output, video_config)?; |
| 37 | + // Write the header after all streams are added |
| 38 | + output.write_header()?; |
74 | 39 |
|
75 | | - // You could add audio streams here too |
76 | | - // ... |
| 40 | + // Now you can write H264 samples from MediaFoundation |
| 41 | + #[cfg(windows)] |
| 42 | + { |
| 43 | + // Example: Write samples from MediaFoundation |
| 44 | + // let sample: IMFSample = get_sample_from_media_foundation(); |
| 45 | + // h264_muxer.write_sample(&sample)?; |
| 46 | + } |
77 | 47 |
|
78 | | - // Write header after all streams are added |
79 | | - output.write_header()?; |
| 48 | + // Or write raw H264 data |
| 49 | + let example_h264_data = vec![0, 0, 0, 1, 0x65]; // Example keyframe NAL |
| 50 | + h264_muxer.write_h264_data( |
| 51 | + &example_h264_data, |
| 52 | + 0, // pts in microseconds |
| 53 | + 0, // dts in microseconds |
| 54 | + 33333, // duration in microseconds (1/30 fps) |
| 55 | + true, // is_keyframe |
| 56 | + &mut output, |
| 57 | + )?; |
80 | 58 |
|
81 | | - Ok(Self { |
82 | | - output, |
83 | | - h264_muxer: Some(h264_muxer), |
84 | | - is_finished: false, |
85 | | - }) |
86 | | - } |
| 59 | + // Finish the muxer (doesn't write trailer) |
| 60 | + h264_muxer.finish()?; |
| 61 | + |
| 62 | + // Write the trailer (this would be done by MP4File::finish()) |
| 63 | + output.write_trailer()?; |
87 | 64 |
|
88 | | - #[cfg(windows)] |
89 | | - fn write_sample( |
90 | | - &mut self, |
91 | | - sample: &windows::Win32::Media::MediaFoundation::IMFSample, |
92 | | - ) -> Result<(), Box<dyn std::error::Error>> { |
93 | | - if let Some(muxer) = &mut self.h264_muxer { |
94 | | - muxer.write_sample(sample, &mut self.output)?; |
95 | | - } |
96 | 65 | Ok(()) |
97 | 66 | } |
98 | 67 |
|
99 | | - fn write_h264_data( |
100 | | - &mut self, |
101 | | - data: &[u8], |
102 | | - pts: i64, |
103 | | - dts: i64, |
104 | | - duration: i64, |
105 | | - is_keyframe: bool, |
106 | | - ) -> Result<(), ffmpeg::Error> { |
107 | | - if let Some(muxer) = &mut self.h264_muxer { |
108 | | - muxer.write_h264_data(data, pts, dts, duration, is_keyframe, &mut self.output)?; |
109 | | - } |
110 | | - Ok(()) |
| 68 | + /// Example showing how this would integrate with an MP4File-like structure |
| 69 | + struct MP4FileExample { |
| 70 | + output: format::context::Output, |
| 71 | + h264_muxer: Option<H264StreamMuxer>, |
| 72 | + is_finished: bool, |
111 | 73 | } |
112 | 74 |
|
113 | | - fn finish(&mut self) -> Result<(), ffmpeg::Error> { |
114 | | - if self.is_finished { |
115 | | - return Ok(()); |
| 75 | + impl MP4FileExample { |
| 76 | + fn new(output_path: PathBuf, video_config: MuxerConfig) -> Result<Self, ffmpeg::Error> { |
| 77 | + let mut output = format::output(&output_path)?; |
| 78 | + |
| 79 | + // Add H264 stream and create muxer |
| 80 | + let h264_muxer = H264StreamMuxer::add_stream(&mut output, video_config)?; |
| 81 | + |
| 82 | + // You could add audio streams here too |
| 83 | + // ... |
| 84 | + |
| 85 | + // Write header after all streams are added |
| 86 | + output.write_header()?; |
| 87 | + |
| 88 | + Ok(Self { |
| 89 | + output, |
| 90 | + h264_muxer: Some(h264_muxer), |
| 91 | + is_finished: false, |
| 92 | + }) |
116 | 93 | } |
117 | | - self.is_finished = true; |
118 | 94 |
|
119 | | - if let Some(muxer) = &mut self.h264_muxer { |
120 | | - muxer.finish()?; |
| 95 | + #[cfg(windows)] |
| 96 | + fn write_sample( |
| 97 | + &mut self, |
| 98 | + sample: &windows::Win32::Media::MediaFoundation::IMFSample, |
| 99 | + ) -> Result<(), Box<dyn std::error::Error>> { |
| 100 | + if let Some(muxer) = &mut self.h264_muxer { |
| 101 | + muxer.write_sample(sample, &mut self.output)?; |
| 102 | + } |
| 103 | + Ok(()) |
121 | 104 | } |
122 | 105 |
|
123 | | - // Write trailer |
124 | | - self.output.write_trailer()?; |
125 | | - Ok(()) |
126 | | - } |
127 | | -} |
| 106 | + fn write_h264_data( |
| 107 | + &mut self, |
| 108 | + data: &[u8], |
| 109 | + pts: i64, |
| 110 | + dts: i64, |
| 111 | + duration: i64, |
| 112 | + is_keyframe: bool, |
| 113 | + ) -> Result<(), ffmpeg::Error> { |
| 114 | + if let Some(muxer) = &mut self.h264_muxer { |
| 115 | + muxer.write_h264_data(data, pts, dts, duration, is_keyframe, &mut self.output)?; |
| 116 | + } |
| 117 | + Ok(()) |
| 118 | + } |
128 | 119 |
|
129 | | -/// Alternative approach using the owned version for standalone use |
130 | | -fn example_with_owned_muxer() -> Result<(), Box<dyn std::error::Error>> { |
131 | | - use mediafoundation_ffmpeg::H264SampleMuxerOwned; |
132 | | - |
133 | | - // Initialize FFmpeg |
134 | | - ffmpeg::init()?; |
135 | | - |
136 | | - let config = MuxerConfig { |
137 | | - width: 1920, |
138 | | - height: 1080, |
139 | | - fps: 30, |
140 | | - bitrate: 5_000_000, |
141 | | - }; |
142 | | - |
143 | | - // Create a standalone muxer that owns its output |
144 | | - let mut muxer = H264SampleMuxerOwned::new_mp4(PathBuf::from("standalone_output.mp4"), config)?; |
145 | | - |
146 | | - // Write some H264 data |
147 | | - let example_h264_data = vec![0, 0, 0, 1, 0x65]; // Example keyframe NAL |
148 | | - muxer.write_h264_data( |
149 | | - &example_h264_data, |
150 | | - 0, // pts |
151 | | - 0, // dts |
152 | | - 33333, // duration |
153 | | - true, // is_keyframe |
154 | | - )?; |
155 | | - |
156 | | - // The muxer automatically finishes and writes trailer when dropped |
157 | | - muxer.finish()?; |
158 | | - |
159 | | - Ok(()) |
160 | | -} |
| 120 | + fn finish(&mut self) -> Result<(), ffmpeg::Error> { |
| 121 | + if self.is_finished { |
| 122 | + return Ok(()); |
| 123 | + } |
| 124 | + self.is_finished = true; |
161 | 125 |
|
162 | | -fn main() -> Result<(), Box<dyn std::error::Error>> { |
163 | | - println!("Example 1: Using H264StreamMuxer with shared output"); |
164 | | - example_with_shared_output()?; |
| 126 | + if let Some(muxer) = &mut self.h264_muxer { |
| 127 | + muxer.finish()?; |
| 128 | + } |
165 | 129 |
|
166 | | - println!("\nExample 2: Using H264SampleMuxerOwned for standalone use"); |
167 | | - example_with_owned_muxer()?; |
| 130 | + // Write trailer |
| 131 | + self.output.write_trailer()?; |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// Alternative approach using the owned version for standalone use |
| 137 | + fn example_with_owned_muxer() -> Result<(), Box<dyn std::error::Error>> { |
| 138 | + use mediafoundation_ffmpeg::H264SampleMuxerOwned; |
168 | 139 |
|
169 | | - println!("\nExample 3: Using MP4FileExample with integrated muxer"); |
170 | | - let mut mp4_file = MP4FileExample::new( |
171 | | - PathBuf::from("integrated_output.mp4"), |
172 | | - MuxerConfig { |
| 140 | + // Initialize FFmpeg |
| 141 | + ffmpeg::init()?; |
| 142 | + |
| 143 | + let config = MuxerConfig { |
173 | 144 | width: 1920, |
174 | 145 | height: 1080, |
175 | 146 | fps: 30, |
176 | 147 | bitrate: 5_000_000, |
177 | | - }, |
178 | | - )?; |
| 148 | + }; |
| 149 | + |
| 150 | + // Create a standalone muxer that owns its output |
| 151 | + let mut muxer = |
| 152 | + H264SampleMuxerOwned::new_mp4(PathBuf::from("standalone_output.mp4"), config)?; |
179 | 153 |
|
180 | | - // Write some test data |
181 | | - let example_h264_data = vec![0, 0, 0, 1, 0x65]; |
182 | | - mp4_file.write_h264_data(&example_h264_data, 0, 0, 33333, true)?; |
| 154 | + // Write some H264 data |
| 155 | + let example_h264_data = vec![0, 0, 0, 1, 0x65]; // Example keyframe NAL |
| 156 | + muxer.write_h264_data( |
| 157 | + &example_h264_data, |
| 158 | + 0, // pts |
| 159 | + 0, // dts |
| 160 | + 33333, // duration |
| 161 | + true, // is_keyframe |
| 162 | + )?; |
183 | 163 |
|
184 | | - // Finish writing |
185 | | - mp4_file.finish()?; |
| 164 | + // The muxer automatically finishes and writes trailer when dropped |
| 165 | + muxer.finish()?; |
186 | 166 |
|
187 | | - Ok(()) |
| 167 | + Ok(()) |
| 168 | + } |
| 169 | + |
| 170 | + fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 171 | + println!("Example 1: Using H264StreamMuxer with shared output"); |
| 172 | + example_with_shared_output()?; |
| 173 | + |
| 174 | + println!("\nExample 2: Using H264SampleMuxerOwned for standalone use"); |
| 175 | + example_with_owned_muxer()?; |
| 176 | + |
| 177 | + println!("\nExample 3: Using MP4FileExample with integrated muxer"); |
| 178 | + let mut mp4_file = MP4FileExample::new( |
| 179 | + PathBuf::from("integrated_output.mp4"), |
| 180 | + MuxerConfig { |
| 181 | + width: 1920, |
| 182 | + height: 1080, |
| 183 | + fps: 30, |
| 184 | + bitrate: 5_000_000, |
| 185 | + }, |
| 186 | + )?; |
| 187 | + |
| 188 | + // Write some test data |
| 189 | + let example_h264_data = vec![0, 0, 0, 1, 0x65]; |
| 190 | + mp4_file.write_h264_data(&example_h264_data, 0, 0, 33333, true)?; |
| 191 | + |
| 192 | + // Finish writing |
| 193 | + mp4_file.finish()?; |
| 194 | + |
| 195 | + Ok(()) |
| 196 | + } |
188 | 197 | } |
0 commit comments