pub fn av_err2str(errnum: c_int) -> StringExamples found in repository?
examples/dump_framed.rs (line 104)
39fn main() {
40 unsafe {
41 let args = env::args().collect::<Vec<_>>();
42 if args.len() != 3 {
43 println!(
44 "usage: {} <input_file> <output_file>\n\
45 API example program to show how to read all packets and \
46 write to another file with a little frame size header.",
47 args[0]
48 );
49 std::process::exit(-1);
50 }
51
52 let input_filename = std::ffi::CString::new(args[1].clone()).unwrap();
53 let mut output = OpenOptions::new()
54 .write(true)
55 .create(true)
56 .open(&args[2])
57 .unwrap();
58
59 let mut ctx: *mut AVFormatContext = std::ptr::null_mut();
60 let mut ret: i32 = 0;
61
62 'outer: loop {
63 ret = avformat_open_input(
64 &mut ctx,
65 input_filename.as_ptr(),
66 std::ptr::null_mut(),
67 std::ptr::null_mut(),
68 );
69 if ret < 0 {
70 println!("Could not open input!");
71 break 'outer;
72 }
73
74 ret = avformat_find_stream_info(ctx, std::ptr::null_mut());
75 if ret < 0 {
76 println!("Could not find stream information!");
77 break 'outer;
78 }
79
80 av_dump_format(ctx, 0, input_filename.as_ptr(), 0);
81
82 let mut pkt = AVPacket::default();
83 av_init_packet(&mut pkt);
84
85 while av_read_frame(ctx, &mut pkt) >= 0 {
86 output.write(&pkt.size.to_be_bytes()).unwrap();
87 output
88 .write(std::slice::from_raw_parts(
89 pkt.data,
90 pkt.size.try_into().unwrap(),
91 ))
92 .unwrap();
93 av_free_packet(&mut pkt);
94 }
95
96 println!("Output (framed) to: '{}'", args[2]);
97
98 break 'outer;
99 }
100
101 avformat_close_input(&mut ctx);
102
103 if ret < 0 {
104 println!("Error occurred: {:?}", av_err2str(ret));
105 std::process::exit(-2);
106 }
107 }
108}More examples
examples/avio_reading.rs (line 166)
61fn main() {
62 unsafe {
63 let mut avio_ctx: *mut AVIOContext = std::ptr::null_mut();
64 let mut avio_ctx_buffer: *mut u8 = std::ptr::null_mut();
65 let avio_ctx_buffer_size: usize = 4096;
66 let mut buffer: *mut u8 = std::ptr::null_mut();
67 let mut buffer_size: usize = 0;
68 let mut fmt_ctx: *mut AVFormatContext = std::ptr::null_mut();
69 let mut ret: i32 = 0;
70 let mut bd = BufferData {
71 ptr: std::ptr::null_mut(),
72 size: 0,
73 };
74
75 let args = env::args().collect::<Vec<_>>();
76 if args.len() != 2 {
77 println!(
78 "usage: {} input_file\n\
79 API example program to show how to read from a custom buffer \
80 accessed through AVIOContext.",
81 args[0]
82 );
83 std::process::exit(-1);
84 }
85
86 let input_filename = std::ffi::CString::new(args[1].clone()).unwrap();
87
88 loop {
89 // slurp file content into buffer
90 ret = av_file_map(
91 input_filename.as_ptr(),
92 &mut buffer,
93 &mut buffer_size,
94 0,
95 std::ptr::null_mut(),
96 );
97 if ret < 0 {
98 break;
99 }
100
101 // fill opaque structure used by the AVIOContext read callback
102 bd.ptr = buffer;
103 bd.size = buffer_size;
104
105 fmt_ctx = avformat_alloc_context();
106 if fmt_ctx.is_null() {
107 ret = AVERROR(ENOMEM);
108 break;
109 }
110
111 avio_ctx_buffer = av_malloc(avio_ctx_buffer_size) as *mut u8;
112 if avio_ctx_buffer.is_null() {
113 ret = AVERROR(ENOMEM);
114 break;
115 }
116 avio_ctx = avio_alloc_context(
117 avio_ctx_buffer,
118 avio_ctx_buffer_size.try_into().unwrap(),
119 0,
120 &mut bd as *mut BufferData as *mut c_void,
121 Some(read_packet),
122 None,
123 None,
124 );
125 if avio_ctx.is_null() {
126 ret = AVERROR(ENOMEM);
127 break;
128 }
129 (*fmt_ctx).pb = avio_ctx;
130
131 ret = avformat_open_input(
132 &mut fmt_ctx,
133 std::ptr::null(),
134 std::ptr::null_mut(),
135 std::ptr::null_mut(),
136 );
137 if ret < 0 {
138 println!("Could not open input!");
139 break;
140 }
141
142 ret = avformat_find_stream_info(fmt_ctx, std::ptr::null_mut());
143 if ret < 0 {
144 println!("Could not find stream information!");
145 break;
146 }
147
148 av_dump_format(fmt_ctx, 0, input_filename.as_ptr(), 0);
149
150 break;
151 }
152
153 avformat_close_input(&mut fmt_ctx);
154
155 // note: the internal buffer could have changed, and be != avio_ctx_buffer
156 if !avio_ctx.is_null() {
157 av_freep(std::mem::transmute::<*mut *mut u8, *mut c_void>(
158 &mut (*avio_ctx).buffer,
159 ));
160 }
161 avio_context_free(&mut avio_ctx);
162
163 av_file_unmap(buffer, buffer_size);
164
165 if ret < 0 {
166 println!("Error occurred: {:?}", av_err2str(ret));
167 std::process::exit(-2);
168 }
169 }
170}examples/remuxing.rs (line 256)
57fn main() {
58 unsafe {
59 let mut ofmt_ptr: *mut AVOutputFormat = std::ptr::null_mut();
60 let mut ifmt_ctx_ptr: *mut AVFormatContext = std::ptr::null_mut();
61 let mut ofmt_ctx_ptr: *mut AVFormatContext = std::ptr::null_mut();
62 let mut pkt: AVPacket = std::mem::zeroed();
63 let mut ret;
64
65 let args = env::args().collect::<Vec<_>>();
66 if args.len() < 3 {
67 println!(
68 "usage: {} input output\n\
69 API example program to remux a media file with libavformat and libavcodec.\n\
70 The output format is guessed according to the file extension.\n",
71 args[0]
72 );
73 std::process::exit(-1);
74 }
75
76 let in_filename = CString::new(args[1].clone()).unwrap();
77 let out_filename = CString::new(args[2].clone()).unwrap();
78
79 'outer: loop {
80 ret = avformat_open_input(
81 &mut ifmt_ctx_ptr,
82 in_filename.as_ptr(),
83 std::ptr::null_mut(),
84 std::ptr::null_mut(),
85 );
86 if ret < 0 {
87 println!("Could not open input file {:?}", in_filename);
88 break 'outer;
89 }
90
91 ret = avformat_find_stream_info(ifmt_ctx_ptr, std::ptr::null_mut());
92 if ret < 0 {
93 println!("Failed to retrieve input stream information");
94 break 'outer;
95 }
96
97 av_dump_format(ifmt_ctx_ptr, 0, in_filename.as_ptr(), 0);
98
99 avformat_alloc_output_context2(
100 &mut ofmt_ctx_ptr,
101 std::ptr::null_mut(),
102 std::ptr::null_mut(),
103 out_filename.as_ptr(),
104 );
105 if ofmt_ctx_ptr.is_null() {
106 println!("Could not create output context");
107 ret = AVERROR_UNKNOWN;
108 break 'outer;
109 }
110
111 let ifmt_ctx = &mut *ifmt_ctx_ptr;
112 let ofmt_ctx = &mut *ofmt_ctx_ptr;
113
114 let in_nb_streams = ifmt_ctx.nb_streams as usize;
115 let in_streams: &[*mut AVStream] =
116 std::slice::from_raw_parts(ifmt_ctx.streams, in_nb_streams);
117
118 let mut stream_index = 0;
119 let mut stream_mapping: Vec<i32> = Vec::with_capacity(in_nb_streams);
120 stream_mapping.resize(stream_mapping.capacity(), -1);
121
122 ofmt_ptr = ofmt_ctx.oformat;
123
124 for i in 0..in_nb_streams {
125 let in_stream = &mut *in_streams[i];
126 let in_codecpar_ptr: *mut AVCodecParameters = in_stream.codecpar;
127 let in_codecpar = &mut *in_codecpar_ptr;
128
129 if in_codecpar.codec_type != AVMEDIA_TYPE_AUDIO
130 && in_codecpar.codec_type != AVMEDIA_TYPE_VIDEO
131 && in_codecpar.codec_type != AVMEDIA_TYPE_SUBTITLE
132 {
133 stream_mapping[i] = -1;
134 continue;
135 }
136
137 stream_mapping[i] = stream_index;
138 stream_index += 1;
139
140 let out_stream_ptr = avformat_new_stream(ofmt_ctx_ptr, std::ptr::null_mut());
141 if out_stream_ptr.is_null() {
142 println!("Failed allocating output stream");
143 ret = AVERROR_UNKNOWN;
144 break 'outer;
145 }
146
147 let out_stream = &mut *out_stream_ptr;
148
149 ret = avcodec_parameters_copy(out_stream.codecpar, in_codecpar_ptr);
150 if ret < 0 {
151 println!("Failed to copy codec parameters");
152 break 'outer;
153 }
154 (*out_stream.codecpar).codec_tag = 0;
155 }
156
157 av_dump_format(ofmt_ctx_ptr, 0, out_filename.as_ptr(), 1);
158
159 if ((*ofmt_ptr).flags & AVFMT_NOFILE) != AVFMT_NOFILE {
160 ret = avio_open(&mut ofmt_ctx.pb, out_filename.as_ptr(), AVIO_FLAG_WRITE);
161 if ret < 0 {
162 println!("Could not open output file {:?}", out_filename);
163 break 'outer;
164 }
165 }
166
167 ret = avformat_write_header(ofmt_ctx_ptr, std::ptr::null_mut());
168 if ret < 0 {
169 println!("Error occurred when opening output file");
170 break 'outer;
171 }
172
173 let out_streams = std::slice::from_raw_parts(
174 ofmt_ctx.streams,
175 ofmt_ctx.nb_streams.try_into().unwrap(),
176 );
177
178 let mut cur_pts: [i64; 64] = [0; 64];
179
180 'inner: loop {
181 ret = av_read_frame(ifmt_ctx_ptr, &mut pkt);
182 if ret < 0 {
183 break 'inner;
184 }
185
186 let curr_stream_index = pkt.stream_index as usize;
187 let in_stream_ptr = in_streams[curr_stream_index];
188 if curr_stream_index >= stream_mapping.len()
189 || stream_mapping[curr_stream_index] < 0
190 {
191 av_packet_unref(&mut pkt);
192 continue;
193 }
194
195 pkt.stream_index = stream_mapping[curr_stream_index];
196 let out_stream_ptr = out_streams[curr_stream_index];
197
198 let in_stream = &mut *in_stream_ptr;
199 let out_stream = &mut *out_stream_ptr;
200
201 let orig_pts = pkt.pts;
202 let orig_duration = pkt.duration;
203
204 if orig_pts == AV_NOPTS_VALUE {
205 pkt.pts = cur_pts[curr_stream_index];
206 pkt.dts = pkt.pts;
207 }
208
209 log_packet(ifmt_ctx_ptr, &pkt, "in");
210
211 /* copy packet */
212 pkt.pts = av_rescale_q_rnd(
213 pkt.pts,
214 in_stream.time_base,
215 out_stream.time_base,
216 AVRounding::new().near_inf().pass_min_max(),
217 );
218 pkt.dts = av_rescale_q_rnd(
219 pkt.dts,
220 in_stream.time_base,
221 out_stream.time_base,
222 AVRounding::new().near_inf().pass_min_max(),
223 );
224 pkt.duration =
225 av_rescale_q(pkt.duration, in_stream.time_base, out_stream.time_base);
226 pkt.pos = -1;
227 log_packet(ofmt_ctx_ptr, &pkt, "out");
228
229 ret = av_interleaved_write_frame(ofmt_ctx_ptr, &mut pkt);
230 if ret < 0 {
231 println!("Error muxing packet");
232 break 'inner;
233 }
234
235 if orig_pts == AV_NOPTS_VALUE {
236 cur_pts[curr_stream_index] += orig_duration;
237 }
238
239 av_packet_unref(&mut pkt);
240 }
241
242 av_write_trailer(ofmt_ctx_ptr);
243
244 break 'outer;
245 }
246
247 avformat_close_input(&mut ifmt_ctx_ptr);
248
249 // close output
250 if !ofmt_ctx_ptr.is_null() && ((*ofmt_ptr).flags & AVFMT_NOFILE) != AVFMT_NOFILE {
251 avio_closep(&mut (*ofmt_ctx_ptr).pb);
252 }
253 avformat_free_context(ofmt_ctx_ptr);
254
255 if ret < 0 && ret != AVERROR_EOF {
256 println!("Error occurred: {:?}", av_err2str(ret));
257 std::process::exit(-2);
258 }
259 }
260}