Function init

Source
pub fn init() -> Result<(), Error>
Examples found in repository?
examples/chapters.rs (line 6)
5fn main() {
6    ffmpeg::init().unwrap();
7
8    match ffmpeg::format::input(&env::args().nth(1).expect("missing input file name")) {
9        Ok(ictx) => {
10            println!("Nb chapters: {}", ictx.nb_chapters());
11
12            for chapter in ictx.chapters() {
13                println!("chapter id {}:", chapter.id());
14                println!("\ttime_base: {}", chapter.time_base());
15                println!("\tstart: {}", chapter.start());
16                println!("\tend: {}", chapter.end());
17
18                for (k, v) in chapter.metadata().iter() {
19                    println!("\t{}: {}", k, v);
20                }
21            }
22
23            let mut octx =
24                ffmpeg::format::output(&"test.mkv".to_owned()).expect("Couldn't open test file");
25
26            for chapter in ictx.chapters() {
27                let title = match chapter.metadata().get("title") {
28                    Some(title) => String::from(title),
29                    None => String::new(),
30                };
31
32                match octx.add_chapter(
33                    chapter.id(),
34                    chapter.time_base(),
35                    chapter.start(),
36                    chapter.end(),
37                    &title,
38                ) {
39                    Ok(chapter) => println!("Added chapter with id {} to output", chapter.id()),
40                    Err(error) => {
41                        println!("Error adding chapter with id: {} - {}", chapter.id(), error)
42                    }
43                }
44            }
45
46            println!("\nOuput: nb chapters: {}", octx.nb_chapters());
47            for chapter in octx.chapters() {
48                println!("chapter id {}:", chapter.id());
49                println!("\ttime_base: {}", chapter.time_base());
50                println!("\tstart: {}", chapter.start());
51                println!("\tend: {}", chapter.end());
52                for (k, v) in chapter.metadata().iter() {
53                    println!("\t{}: {}", k, v);
54                }
55            }
56        }
57
58        Err(error) => println!("error: {}", error),
59    }
60}
More examples
Hide additional examples
examples/transcode-audio.rs (line 137)
136fn main() {
137    ffmpeg::init().unwrap();
138
139    let input = env::args().nth(1).expect("missing input");
140    let output = env::args().nth(2).expect("missing output");
141    let filter = env::args().nth(3).unwrap_or_else(|| "anull".to_owned());
142    let seek = env::args().nth(4).and_then(|s| s.parse::<i64>().ok());
143
144    let mut ictx = format::input(&input).unwrap();
145    let mut octx = format::output(&output).unwrap();
146    let mut transcoder = transcoder(&mut ictx, &mut octx, &output, &filter).unwrap();
147
148    if let Some(position) = seek {
149        // If the position was given in seconds, rescale it to ffmpegs base timebase.
150        let position = position.rescale((1, 1), rescale::TIME_BASE);
151        // If this seek was embedded in the transcoding loop, a call of `flush()`
152        // for every opened buffer after the successful seek would be advisable.
153        ictx.seek(position, ..position).unwrap();
154    }
155
156    octx.set_metadata(ictx.metadata().to_owned());
157    octx.write_header().unwrap();
158
159    let in_time_base = transcoder.decoder.time_base();
160    let out_time_base = octx.stream(0).unwrap().time_base();
161
162    let mut decoded = frame::Audio::empty();
163    let mut encoded = ffmpeg::Packet::empty();
164
165    for (stream, mut packet) in ictx.packets() {
166        if stream.index() == transcoder.stream {
167            packet.rescale_ts(stream.time_base(), in_time_base);
168
169            if let Ok(true) = transcoder.decoder.decode(&packet, &mut decoded) {
170                let timestamp = decoded.timestamp();
171                decoded.set_pts(timestamp);
172
173                transcoder
174                    .filter
175                    .get("in")
176                    .unwrap()
177                    .source()
178                    .add(&decoded)
179                    .unwrap();
180
181                while let Ok(..) = transcoder
182                    .filter
183                    .get("out")
184                    .unwrap()
185                    .sink()
186                    .frame(&mut decoded)
187                {
188                    if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
189                        encoded.set_stream(0);
190                        encoded.rescale_ts(in_time_base, out_time_base);
191                        encoded.write_interleaved(&mut octx).unwrap();
192                    }
193                }
194            }
195        }
196    }
197
198    transcoder
199        .filter
200        .get("in")
201        .unwrap()
202        .source()
203        .flush()
204        .unwrap();
205
206    while let Ok(..) = transcoder
207        .filter
208        .get("out")
209        .unwrap()
210        .sink()
211        .frame(&mut decoded)
212    {
213        if let Ok(true) = transcoder.encoder.encode(&decoded, &mut encoded) {
214            encoded.set_stream(0);
215            encoded.rescale_ts(in_time_base, out_time_base);
216            encoded.write_interleaved(&mut octx).unwrap();
217        }
218    }
219
220    if let Ok(true) = transcoder.encoder.flush(&mut encoded) {
221        encoded.set_stream(0);
222        encoded.rescale_ts(in_time_base, out_time_base);
223        encoded.write_interleaved(&mut octx).unwrap();
224    }
225
226    octx.write_trailer().unwrap();
227}
examples/codec-info.rs (line 6)
5fn main() {
6    ffmpeg::init().unwrap();
7
8    for arg in env::args().skip(1) {
9        if let Some(codec) = ffmpeg::decoder::find_by_name(&arg) {
10            println!("type: decoder");
11            println!("\t id: {:?}", codec.id());
12            println!("\t name: {}", codec.name());
13            println!("\t description: {}", codec.description());
14            println!("\t medium: {:?}", codec.medium());
15            println!("\t capabilities: {:?}", codec.capabilities());
16
17            if let Some(profiles) = codec.profiles() {
18                println!("\t profiles: {:?}", profiles.collect::<Vec<_>>());
19            } else {
20                println!("\t profiles: none");
21            }
22
23            if let Ok(video) = codec.video() {
24                if let Some(rates) = video.rates() {
25                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
26                } else {
27                    println!("\t rates: any");
28                }
29
30                if let Some(formats) = video.formats() {
31                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
32                } else {
33                    println!("\t formats: any");
34                }
35            }
36
37            if let Ok(audio) = codec.audio() {
38                if let Some(rates) = audio.rates() {
39                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
40                } else {
41                    println!("\t rates: any");
42                }
43
44                if let Some(formats) = audio.formats() {
45                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
46                } else {
47                    println!("\t formats: any");
48                }
49
50                if let Some(layouts) = audio.channel_layouts() {
51                    println!("\t channel_layouts: {:?}", layouts.collect::<Vec<_>>());
52                } else {
53                    println!("\t channel_layouts: any");
54                }
55            }
56
57            println!("\t max_lowres: {:?}", codec.max_lowres());
58        }
59
60        if let Some(codec) = ffmpeg::encoder::find_by_name(&arg) {
61            println!();
62            println!("type: encoder");
63            println!("\t id: {:?}", codec.id());
64            println!("\t name: {}", codec.name());
65            println!("\t description: {}", codec.description());
66            println!("\t medium: {:?}", codec.medium());
67            println!("\t capabilities: {:?}", codec.capabilities());
68
69            if let Some(profiles) = codec.profiles() {
70                println!("\t profiles: {:?}", profiles.collect::<Vec<_>>());
71            }
72
73            if let Ok(video) = codec.video() {
74                if let Some(rates) = video.rates() {
75                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
76                } else {
77                    println!("\t rates: any");
78                }
79
80                if let Some(formats) = video.formats() {
81                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
82                } else {
83                    println!("\t formats: any");
84                }
85            }
86
87            if let Ok(audio) = codec.audio() {
88                if let Some(rates) = audio.rates() {
89                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
90                } else {
91                    println!("\t rates: any");
92                }
93
94                if let Some(formats) = audio.formats() {
95                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
96                } else {
97                    println!("\t formats: any");
98                }
99
100                if let Some(layouts) = audio.channel_layouts() {
101                    println!("\t channel_layouts: {:?}", layouts.collect::<Vec<_>>());
102                } else {
103                    println!("\t channel_layouts: any");
104                }
105            }
106
107            println!("\t max_lowres: {:?}", codec.max_lowres());
108        }
109    }
110}
examples/metadata.rs (line 6)
5fn main() {
6    ffmpeg::init().unwrap();
7
8    match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9        Ok(context) => {
10            for (k, v) in context.metadata().iter() {
11                println!("{}: {}", k, v);
12            }
13
14            if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15                println!("Best video stream index: {}", stream.index());
16            }
17
18            if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19                println!("Best audio stream index: {}", stream.index());
20            }
21
22            if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23                println!("Best subtitle stream index: {}", stream.index());
24            }
25
26            println!(
27                "duration (seconds): {:.2}",
28                context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29            );
30
31            for stream in context.streams() {
32                println!("stream index {}:", stream.index());
33                println!("\ttime_base: {}", stream.time_base());
34                println!("\tstart_time: {}", stream.start_time());
35                println!("\tduration (stream timebase): {}", stream.duration());
36                println!(
37                    "\tduration (seconds): {:.2}",
38                    stream.duration() as f64 * f64::from(stream.time_base())
39                );
40                println!("\tframes: {}", stream.frames());
41                println!("\tdisposition: {:?}", stream.disposition());
42                println!("\tdiscard: {:?}", stream.discard());
43                println!("\trate: {}", stream.rate());
44
45                let codec = stream.codec();
46                println!("\tmedium: {:?}", codec.medium());
47                println!("\tid: {:?}", codec.id());
48
49                if codec.medium() == ffmpeg::media::Type::Video {
50                    if let Ok(video) = codec.decoder().video() {
51                        println!("\tbit_rate: {}", video.bit_rate());
52                        println!("\tmax_rate: {}", video.max_bit_rate());
53                        println!("\tdelay: {}", video.delay());
54                        println!("\tvideo.width: {}", video.width());
55                        println!("\tvideo.height: {}", video.height());
56                        println!("\tvideo.format: {:?}", video.format());
57                        println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58                        println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59                        println!("\tvideo.color_space: {:?}", video.color_space());
60                        println!("\tvideo.color_range: {:?}", video.color_range());
61                        println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62                        println!(
63                            "\tvideo.color_transfer_characteristic: {:?}",
64                            video.color_transfer_characteristic()
65                        );
66                        println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67                        println!("\tvideo.references: {}", video.references());
68                        println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69                    }
70                } else if codec.medium() == ffmpeg::media::Type::Audio {
71                    if let Ok(audio) = codec.decoder().audio() {
72                        println!("\tbit_rate: {}", audio.bit_rate());
73                        println!("\tmax_rate: {}", audio.max_bit_rate());
74                        println!("\tdelay: {}", audio.delay());
75                        println!("\taudio.rate: {}", audio.rate());
76                        println!("\taudio.channels: {}", audio.channels());
77                        println!("\taudio.format: {:?}", audio.format());
78                        println!("\taudio.frames: {}", audio.frames());
79                        println!("\taudio.align: {}", audio.align());
80                        println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81                        println!("\taudio.frame_start: {:?}", audio.frame_start());
82                    }
83                }
84            }
85        }
86
87        Err(error) => println!("error: {}", error),
88    }
89}