pub struct Stream { /* private fields */ }Implementations§
Source§impl Stream
impl Stream
Sourcepub fn new(
format_ctx: &mut FormatContext,
codec: Option<&CodecContext>,
) -> Option<Stream>
pub fn new( format_ctx: &mut FormatContext, codec: Option<&CodecContext>, ) -> Option<Stream>
Examples found in repository?
examples/generate_black_square_512x512.rs (line 16)
4fn main() {
5 let filename = "bk.png";
6 let width = 512;
7 let height = 512;
8
9 let mut format_ctx =
10 FormatContext::new("image2", filename, None).expect("Failed to create FormatContext");
11
12 let mut codec = CodecContext::from_encoder_id(low_level::AVCodecID_AV_CODEC_ID_PNG)
13 .expect("Failed to create CodecContext");
14
15 let mut stream =
16 Stream::new(&mut format_ctx, Some(&*codec)).expect("Failed to create a stream");
17
18 codec
19 .set_size(width, height)
20 .set_framerate(1)
21 .set_pixel_format(low_level::AVPixelFormat_AV_PIX_FMT_RGB24)
22 .set_bitrate(400_000)
23 .set_gop_size(1)
24 .set_max_b_frames(1);
25
26 if (format_ctx.get_output_format().flags & low_level::AVFMT_GLOBALHEADER as i32) != 0 {
27 let flags = codec.get_flags() | low_level::AV_CODEC_FLAG_GLOBAL_HEADER as i32;
28
29 codec.set_flags(flags);
30 }
31
32 match codec.open(None) {
33 Err(err) => panic!("Error opening codec: {}", err),
34 Ok(()) => {}
35 };
36
37 codec.fill_stream_parameters(&mut stream);
38
39 match format_ctx.open(filename, AVIO_FLAG_WRITE as i32) {
40 Err(err) => panic!("Error opening file! {err}"),
41 Ok(()) => {}
42 };
43
44 format_ctx
45 .write_header(None)
46 .expect("Failed to write a header!");
47
48 let mut frame = Frame::from_size_and_pixfmt(width, height, AVPixelFormat_AV_PIX_FMT_RGB24)
49 .expect("Failed to make a frame!");
50
51 let data = frame.data_plane_mut(0).expect("Failed to get plane!");
52
53 for y in 0usize..height as usize {
54 for x in 0usize..width as usize {
55 let coord = y * (width as usize * 3usize) + (x * 3);
56
57 data[coord + 0] = 0xff;
58 data[coord + 1] = 0xff;
59 data[coord + 2] = 0xff;
60 }
61 }
62
63 codec.send_frame(&frame);
64
65 let mut packet = Packet::new();
66
67 codec.receive_packet(&mut packet);
68
69 format_ctx.write_frame(&mut packet);
70
71 packet.clear();
72
73 format_ctx.write_trailer();
74
75 println!("Hello, world!");
76}Sourcepub fn codec_parameters(&self) -> CodecParameters
pub fn codec_parameters(&self) -> CodecParameters
Examples found in repository?
examples/dump_info.rs (line 24)
10fn main() {
11 let url = if let Some(url) = std::env::args().skip(1).next() {
12 url
13 } else {
14 eprintln!("No input file!");
15 std::process::exit(1);
16 };
17
18 let mut format_context = FormatContext::open_input(&url).expect("Failed to open file!");
19
20 format_context.find_stream_info();
21
22 for i in format_context.streams() {
23 let index = i.index();
24 let parameters = i.codec_parameters();
25 let name = parameters.codec_name();
26 let duration_ms = (i.duration_sec() * 1000.0) as i64;
27
28 let (hr, min, sec, msec) = {
29 const HOUR: i64 = 3600 * 1000;
30
31 (
32 duration_ms / HOUR,
33 (duration_ms % HOUR) / (60 * 1000),
34 (duration_ms % (60 * 1000)) / 1000,
35 duration_ms % 1000,
36 )
37 };
38
39 let s_type = if parameters.is_audio() {
40 "audio"
41 } else if parameters.is_video() {
42 "video"
43 } else {
44 "other"
45 };
46
47 println!("Stream #{index}: {s_type}, {name}, ({hr:02}:{min:02}:{sec:02}:{msec:03})");
48
49 if parameters.is_video() {
50 let (width, height) = parameters.size();
51 let bps = parameters.bitrate();
52
53 println!(" |- size: {width} x {height}; {bps} bps");
54 } else if parameters.is_audio() {
55 let bps = parameters.bitrate();
56
57 println!(" |- {bps} bps");
58
59 }
60 }
61}More examples
examples/decode_frame.rs (line 29)
11fn main() {
12 let url = if let Some(url) = std::env::args().skip(1).next() {
13 url
14 } else {
15 eprintln!("No input file!");
16 std::process::exit(1);
17 };
18
19 let mut format_context = FormatContext::open_input(&url).expect("Failed to open file!");
20
21 format_context.find_stream_info();
22
23 for i in format_context.streams() {
24 println!("{i:#?}");
25 }
26
27 let video_stream = format_context
28 .streams()
29 .filter(|x| x.codec_parameters().is_video())
30 .next()
31 .expect("Failed to find a video stream");
32
33 println!("CodecParams idx: {}", video_stream.index());
34 println!(
35 "CodecParams format: {}",
36 video_stream.codec_parameters().format()
37 );
38
39 let mut codec = CodecContext::from_decoder_id(video_stream.codec_parameters().codec_id())
40 .expect("Failed to create CodecContext");
41
42 codec.fill_from_parameters(&video_stream.codec_parameters());
43
44 codec.open(None).unwrap();
45
46 println!("Pixel format: {}", codec.get_pixel_format());
47
48 format_context.seek_msec(video_stream.index(), 4 * 60 * 1000);
49
50 let mut packet = Packet::new();
51
52 while format_context.read_frame(&mut packet) >= 0 {
53 println!("{}", packet.stream_index());
54
55 if packet.stream_index() == video_stream.index() {
56 codec.send_packet(&packet);
57
58 let mut frame = Frame::empty().expect("Failed to allocate a frame");
59
60 while codec.receive_frame(&mut frame) >= 0 {
61 println!("{}", frame.format());
62 {
63 let (width, height) = video_stream.codec_parameters().size();
64 println!("{width} x {height}");
65
66 let mut new_frame =
67 Frame::from_size_and_pixfmt(width, height, AVPixelFormat_AV_PIX_FMT_RGB24)
68 .unwrap();
69
70 let sws = Sws::new(
71 width,
72 height,
73 video_stream.codec_parameters().format(),
74 width,
75 height,
76 AVPixelFormat_AV_PIX_FMT_RGB24,
77 SWS_BILINEAR as _,
78 );
79
80 println!("{:?} {:?}", frame.linesize(), new_frame.linesize());
81
82 sws.scale(&frame, &mut new_frame);
83
84 let mut file = std::fs::OpenOptions::new()
85 .create(true)
86 .truncate(true)
87 .write(true)
88 .open("data.bin")
89 .unwrap();
90
91 file.write(new_frame.data_plane(0).unwrap()).unwrap();
92 }
93 }
94
95 break;
96 }
97 }
98
99 println!("Exit!");
100}pub fn id(&self) -> i32
Sourcepub fn index(&self) -> i32
pub fn index(&self) -> i32
Examples found in repository?
examples/dump_info.rs (line 23)
10fn main() {
11 let url = if let Some(url) = std::env::args().skip(1).next() {
12 url
13 } else {
14 eprintln!("No input file!");
15 std::process::exit(1);
16 };
17
18 let mut format_context = FormatContext::open_input(&url).expect("Failed to open file!");
19
20 format_context.find_stream_info();
21
22 for i in format_context.streams() {
23 let index = i.index();
24 let parameters = i.codec_parameters();
25 let name = parameters.codec_name();
26 let duration_ms = (i.duration_sec() * 1000.0) as i64;
27
28 let (hr, min, sec, msec) = {
29 const HOUR: i64 = 3600 * 1000;
30
31 (
32 duration_ms / HOUR,
33 (duration_ms % HOUR) / (60 * 1000),
34 (duration_ms % (60 * 1000)) / 1000,
35 duration_ms % 1000,
36 )
37 };
38
39 let s_type = if parameters.is_audio() {
40 "audio"
41 } else if parameters.is_video() {
42 "video"
43 } else {
44 "other"
45 };
46
47 println!("Stream #{index}: {s_type}, {name}, ({hr:02}:{min:02}:{sec:02}:{msec:03})");
48
49 if parameters.is_video() {
50 let (width, height) = parameters.size();
51 let bps = parameters.bitrate();
52
53 println!(" |- size: {width} x {height}; {bps} bps");
54 } else if parameters.is_audio() {
55 let bps = parameters.bitrate();
56
57 println!(" |- {bps} bps");
58
59 }
60 }
61}More examples
examples/decode_frame.rs (line 33)
11fn main() {
12 let url = if let Some(url) = std::env::args().skip(1).next() {
13 url
14 } else {
15 eprintln!("No input file!");
16 std::process::exit(1);
17 };
18
19 let mut format_context = FormatContext::open_input(&url).expect("Failed to open file!");
20
21 format_context.find_stream_info();
22
23 for i in format_context.streams() {
24 println!("{i:#?}");
25 }
26
27 let video_stream = format_context
28 .streams()
29 .filter(|x| x.codec_parameters().is_video())
30 .next()
31 .expect("Failed to find a video stream");
32
33 println!("CodecParams idx: {}", video_stream.index());
34 println!(
35 "CodecParams format: {}",
36 video_stream.codec_parameters().format()
37 );
38
39 let mut codec = CodecContext::from_decoder_id(video_stream.codec_parameters().codec_id())
40 .expect("Failed to create CodecContext");
41
42 codec.fill_from_parameters(&video_stream.codec_parameters());
43
44 codec.open(None).unwrap();
45
46 println!("Pixel format: {}", codec.get_pixel_format());
47
48 format_context.seek_msec(video_stream.index(), 4 * 60 * 1000);
49
50 let mut packet = Packet::new();
51
52 while format_context.read_frame(&mut packet) >= 0 {
53 println!("{}", packet.stream_index());
54
55 if packet.stream_index() == video_stream.index() {
56 codec.send_packet(&packet);
57
58 let mut frame = Frame::empty().expect("Failed to allocate a frame");
59
60 while codec.receive_frame(&mut frame) >= 0 {
61 println!("{}", frame.format());
62 {
63 let (width, height) = video_stream.codec_parameters().size();
64 println!("{width} x {height}");
65
66 let mut new_frame =
67 Frame::from_size_and_pixfmt(width, height, AVPixelFormat_AV_PIX_FMT_RGB24)
68 .unwrap();
69
70 let sws = Sws::new(
71 width,
72 height,
73 video_stream.codec_parameters().format(),
74 width,
75 height,
76 AVPixelFormat_AV_PIX_FMT_RGB24,
77 SWS_BILINEAR as _,
78 );
79
80 println!("{:?} {:?}", frame.linesize(), new_frame.linesize());
81
82 sws.scale(&frame, &mut new_frame);
83
84 let mut file = std::fs::OpenOptions::new()
85 .create(true)
86 .truncate(true)
87 .write(true)
88 .open("data.bin")
89 .unwrap();
90
91 file.write(new_frame.data_plane(0).unwrap()).unwrap();
92 }
93 }
94
95 break;
96 }
97 }
98
99 println!("Exit!");
100}pub fn time_base(&self) -> AVRational
pub fn duration(&self) -> i64
Sourcepub fn duration_sec(&self) -> f64
pub fn duration_sec(&self) -> f64
Examples found in repository?
examples/dump_info.rs (line 26)
10fn main() {
11 let url = if let Some(url) = std::env::args().skip(1).next() {
12 url
13 } else {
14 eprintln!("No input file!");
15 std::process::exit(1);
16 };
17
18 let mut format_context = FormatContext::open_input(&url).expect("Failed to open file!");
19
20 format_context.find_stream_info();
21
22 for i in format_context.streams() {
23 let index = i.index();
24 let parameters = i.codec_parameters();
25 let name = parameters.codec_name();
26 let duration_ms = (i.duration_sec() * 1000.0) as i64;
27
28 let (hr, min, sec, msec) = {
29 const HOUR: i64 = 3600 * 1000;
30
31 (
32 duration_ms / HOUR,
33 (duration_ms % HOUR) / (60 * 1000),
34 (duration_ms % (60 * 1000)) / 1000,
35 duration_ms % 1000,
36 )
37 };
38
39 let s_type = if parameters.is_audio() {
40 "audio"
41 } else if parameters.is_video() {
42 "video"
43 } else {
44 "other"
45 };
46
47 println!("Stream #{index}: {s_type}, {name}, ({hr:02}:{min:02}:{sec:02}:{msec:03})");
48
49 if parameters.is_video() {
50 let (width, height) = parameters.size();
51 let bps = parameters.bitrate();
52
53 println!(" |- size: {width} x {height}; {bps} bps");
54 } else if parameters.is_audio() {
55 let bps = parameters.bitrate();
56
57 println!(" |- {bps} bps");
58
59 }
60 }
61}pub unsafe fn raw(&self) -> &AVStream
pub unsafe fn raw_mut(&mut self) -> &mut AVStream
Trait Implementations§
Auto Trait Implementations§
impl !Send for Stream
impl !Sync for Stream
impl Freeze for Stream
impl RefUnwindSafe for Stream
impl Unpin for Stream
impl UnsafeUnpin for Stream
impl UnwindSafe for Stream
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more