dump_framed/dump_framed.rs
1/*
2 * Copyright (c) 2014 Stefano Sabatini
3 * Copyright (c) 2020 Varphone Wong
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24/**
25 * @file
26 * libavformat AVFormatContext API example.
27 *
28 * Make libavformat demuxer read all packets and
29 * write to another file with a little frame size header.
30 * @example dump_framed.rs
31 */
32use ffav_sys::*;
33use std::convert::TryInto;
34use std::env;
35use std::fs::OpenOptions;
36use std::io::prelude::*;
37
38#[allow(unused_assignments)]
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}