Function init

Source
pub fn init() -> Result<(), Error>
Examples found in repository?
examples/transcode-audio.rs (line 205)
204fn main() {
205    ffmpeg_rs::init().unwrap();
206
207    let input = env::args().nth(1).expect("missing input");
208    let output = env::args().nth(2).expect("missing output");
209    let filter = env::args().nth(3).unwrap_or_else(|| "anull".to_owned());
210    let seek = env::args().nth(4).and_then(|s| s.parse::<i64>().ok());
211
212    let mut ictx = format::input(&input).unwrap();
213    let mut octx = format::output(&output).unwrap();
214    let mut transcoder = transcoder(&mut ictx, &mut octx, &output, &filter).unwrap();
215
216    if let Some(position) = seek {
217        // If the position was given in seconds, rescale it to ffmpegs base timebase.
218        let position = position.rescale((1, 1), rescale::TIME_BASE);
219        // If this seek was embedded in the transcoding loop, a call of `flush()`
220        // for every opened buffer after the successful seek would be advisable.
221        ictx.seek(position, ..position).unwrap();
222    }
223
224    octx.set_metadata(ictx.metadata().to_owned());
225    octx.write_header().unwrap();
226
227    for (stream, mut packet) in ictx.packets() {
228        if stream.index() == transcoder.stream {
229            packet.rescale_ts(stream.time_base(), transcoder.in_time_base);
230            transcoder.send_packet_to_decoder(&packet);
231            transcoder.receive_and_process_decoded_frames(&mut octx);
232        }
233    }
234
235    transcoder.send_eof_to_decoder();
236    transcoder.receive_and_process_decoded_frames(&mut octx);
237
238    transcoder.flush_filter();
239    transcoder.get_and_process_filtered_frames(&mut octx);
240
241    transcoder.send_eof_to_encoder();
242    transcoder.receive_and_process_encoded_packets(&mut octx);
243
244    octx.write_trailer().unwrap();
245}
More examples
Hide additional examples
examples/dump-frames.rs (line 11)
10fn main() -> Result<(), ffmpeg_rs::Error> {
11    ffmpeg_rs::init().unwrap();
12
13    if let Ok(mut ictx) = input(&env::args().nth(1).expect("Cannot open file.")) {
14        let input = ictx
15            .streams()
16            .best(Type::Video)
17            .ok_or(ffmpeg_rs::Error::StreamNotFound)?;
18        let video_stream_index = input.index();
19
20        let context_decoder =
21            ffmpeg_rs::codec::context::Context::from_parameters(input.parameters())?;
22        let mut decoder = context_decoder.decoder().video()?;
23
24        let mut scaler = Context::get(
25            decoder.format(),
26            decoder.width(),
27            decoder.height(),
28            Pixel::RGB24,
29            decoder.width(),
30            decoder.height(),
31            Flags::BILINEAR,
32        )?;
33
34        let mut frame_index = 0;
35
36        let mut receive_and_process_decoded_frames =
37            |decoder: &mut ffmpeg_rs::decoder::Video| -> Result<(), ffmpeg_rs::Error> {
38                let mut decoded = Video::empty();
39                while decoder.receive_frame(&mut decoded).is_ok() {
40                    let mut rgb_frame = Video::empty();
41                    scaler.run(&decoded, &mut rgb_frame)?;
42                    save_file(&rgb_frame, frame_index).unwrap();
43                    frame_index += 1;
44                }
45                Ok(())
46            };
47
48        for (stream, packet) in ictx.packets() {
49            if stream.index() == video_stream_index {
50                decoder.send_packet(&packet)?;
51                receive_and_process_decoded_frames(&mut decoder)?;
52            }
53        }
54        decoder.send_eof()?;
55        receive_and_process_decoded_frames(&mut decoder)?;
56    }
57
58    Ok(())
59}
examples/remux.rs (line 10)
6fn main() {
7    let input_file = env::args().nth(1).expect("missing input file");
8    let output_file = env::args().nth(2).expect("missing output file");
9
10    ffmpeg_rs::init().unwrap();
11    log::set_level(log::Level::Warning);
12
13    let mut ictx = format::input(&input_file).unwrap();
14    let mut octx = format::output(&output_file).unwrap();
15
16    let mut stream_mapping = vec![0; ictx.nb_streams() as _];
17    let mut ist_time_bases = vec![Rational(0, 1); ictx.nb_streams() as _];
18    let mut ost_index = 0;
19    for (ist_index, ist) in ictx.streams().enumerate() {
20        let ist_medium = ist.parameters().codec_type();
21        if ist_medium != media::Type::Audio
22            && ist_medium != media::Type::Video
23            && ist_medium != media::Type::Subtitle
24        {
25            stream_mapping[ist_index] = -1;
26            continue;
27        }
28        stream_mapping[ist_index] = ost_index;
29        ist_time_bases[ist_index] = ist.time_base();
30        ost_index += 1;
31        let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
32        ost.set_parameters(ist.parameters());
33        // We need to set codec_tag to 0 lest we run into incompatible codec tag
34        // issues when muxing into a different container format. Unfortunately
35        // there's no high level API to do this (yet).
36        unsafe {
37            (*ost.parameters().as_mut_ptr()).codec_tag = 0;
38        }
39    }
40
41    octx.set_metadata(ictx.metadata().to_owned());
42    octx.write_header().unwrap();
43
44    for (stream, mut packet) in ictx.packets() {
45        let ist_index = stream.index();
46        let ost_index = stream_mapping[ist_index];
47        if ost_index < 0 {
48            continue;
49        }
50        let ost = octx.stream(ost_index as _).unwrap();
51        packet.rescale_ts(ist_time_bases[ist_index], ost.time_base());
52        packet.set_position(-1);
53        packet.set_stream(ost_index as _);
54        packet.write_interleaved(&mut octx).unwrap();
55    }
56
57    octx.write_trailer().unwrap();
58}
examples/chapters.rs (line 5)
4fn main() {
5    ffmpeg_rs::init().unwrap();
6
7    match ffmpeg_rs::format::input(&env::args().nth(1).expect("missing input file name")) {
8        Ok(ictx) => {
9            println!("Nb chapters: {}", ictx.nb_chapters());
10
11            for chapter in ictx.chapters() {
12                println!("chapter id {}:", chapter.id());
13                println!("\ttime_base: {}", chapter.time_base());
14                println!("\tstart: {}", chapter.start());
15                println!("\tend: {}", chapter.end());
16
17                for (k, v) in chapter.metadata().iter() {
18                    println!("\t{}: {}", k, v);
19                }
20            }
21
22            let mut octx = ffmpeg_rs::format::output(&"test.mkv").expect("Couldn't open test file");
23
24            for chapter in ictx.chapters() {
25                let title = match chapter.metadata().get("title") {
26                    Some(title) => String::from(title),
27                    None => String::new(),
28                };
29
30                match octx.add_chapter(
31                    chapter.id(),
32                    chapter.time_base(),
33                    chapter.start(),
34                    chapter.end(),
35                    &title,
36                ) {
37                    Ok(chapter) => println!("Added chapter with id {} to output", chapter.id()),
38                    Err(error) => {
39                        println!("Error adding chapter with id: {} - {}", chapter.id(), error)
40                    }
41                }
42            }
43
44            println!("\nOuput: nb chapters: {}", octx.nb_chapters());
45            for chapter in octx.chapters() {
46                println!("chapter id {}:", chapter.id());
47                println!("\ttime_base: {}", chapter.time_base());
48                println!("\tstart: {}", chapter.start());
49                println!("\tend: {}", chapter.end());
50                for (k, v) in chapter.metadata().iter() {
51                    println!("\t{}: {}", k, v);
52                }
53            }
54        }
55
56        Err(error) => println!("error: {}", error),
57    }
58}
examples/codec-info.rs (line 5)
4fn main() {
5    ffmpeg_rs::init().unwrap();
6
7    for arg in env::args().skip(1) {
8        if let Some(codec) = ffmpeg_rs::decoder::find_by_name(&arg) {
9            println!("type: decoder");
10            println!("\t id: {:?}", codec.id());
11            println!("\t name: {}", codec.name());
12            println!("\t description: {}", codec.description());
13            println!("\t medium: {:?}", codec.medium());
14            println!("\t capabilities: {:?}", codec.capabilities());
15
16            if let Some(profiles) = codec.profiles() {
17                println!("\t profiles: {:?}", profiles.collect::<Vec<_>>());
18            } else {
19                println!("\t profiles: none");
20            }
21
22            if let Ok(video) = codec.video() {
23                if let Some(rates) = video.rates() {
24                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
25                } else {
26                    println!("\t rates: any");
27                }
28
29                if let Some(formats) = video.formats() {
30                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
31                } else {
32                    println!("\t formats: any");
33                }
34            }
35
36            if let Ok(audio) = codec.audio() {
37                if let Some(rates) = audio.rates() {
38                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
39                } else {
40                    println!("\t rates: any");
41                }
42
43                if let Some(formats) = audio.formats() {
44                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
45                } else {
46                    println!("\t formats: any");
47                }
48
49                if let Some(layouts) = audio.channel_layouts() {
50                    println!("\t channel_layouts: {:?}", layouts.collect::<Vec<_>>());
51                } else {
52                    println!("\t channel_layouts: any");
53                }
54            }
55
56            println!("\t max_lowres: {:?}", codec.max_lowres());
57        }
58
59        if let Some(codec) = ffmpeg_rs::encoder::find_by_name(&arg) {
60            println!();
61            println!("type: encoder");
62            println!("\t id: {:?}", codec.id());
63            println!("\t name: {}", codec.name());
64            println!("\t description: {}", codec.description());
65            println!("\t medium: {:?}", codec.medium());
66            println!("\t capabilities: {:?}", codec.capabilities());
67
68            if let Some(profiles) = codec.profiles() {
69                println!("\t profiles: {:?}", profiles.collect::<Vec<_>>());
70            }
71
72            if let Ok(video) = codec.video() {
73                if let Some(rates) = video.rates() {
74                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
75                } else {
76                    println!("\t rates: any");
77                }
78
79                if let Some(formats) = video.formats() {
80                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
81                } else {
82                    println!("\t formats: any");
83                }
84            }
85
86            if let Ok(audio) = codec.audio() {
87                if let Some(rates) = audio.rates() {
88                    println!("\t rates: {:?}", rates.collect::<Vec<_>>());
89                } else {
90                    println!("\t rates: any");
91                }
92
93                if let Some(formats) = audio.formats() {
94                    println!("\t formats: {:?}", formats.collect::<Vec<_>>());
95                } else {
96                    println!("\t formats: any");
97                }
98
99                if let Some(layouts) = audio.channel_layouts() {
100                    println!("\t channel_layouts: {:?}", layouts.collect::<Vec<_>>());
101                } else {
102                    println!("\t channel_layouts: any");
103                }
104            }
105
106            println!("\t max_lowres: {:?}", codec.max_lowres());
107        }
108    }
109}
examples/transcode-x264.rs (line 176)
164fn main() {
165    let input_file = env::args().nth(1).expect("missing input file");
166    let output_file = env::args().nth(2).expect("missing output file");
167    let x264_opts = parse_opts(
168        env::args()
169            .nth(3)
170            .unwrap_or_else(|| DEFAULT_X264_OPTS.to_string()),
171    )
172    .expect("invalid x264 options string");
173
174    eprintln!("x264 options: {:?}", x264_opts);
175
176    ffmpeg_rs::init().unwrap();
177    log::set_level(log::Level::Info);
178
179    let mut ictx = format::input(&input_file).unwrap();
180    let mut octx = format::output(&output_file).unwrap();
181
182    format::context::input::dump(&ictx, 0, Some(&input_file));
183
184    let best_video_stream_index = ictx
185        .streams()
186        .best(media::Type::Video)
187        .map(|stream| stream.index());
188    let mut stream_mapping: Vec<isize> = vec![0; ictx.nb_streams() as _];
189    let mut ist_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
190    let mut ost_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
191    let mut transcoders = HashMap::new();
192    let mut ost_index = 0;
193    for (ist_index, ist) in ictx.streams().enumerate() {
194        let ist_medium = ist.parameters().codec_type();
195        if ist_medium != media::Type::Audio
196            && ist_medium != media::Type::Video
197            && ist_medium != media::Type::Subtitle
198        {
199            stream_mapping[ist_index] = -1;
200            continue;
201        }
202        stream_mapping[ist_index] = ost_index;
203        ist_time_bases[ist_index] = ist.time_base();
204        if ist_medium == media::Type::Video {
205            // Initialize transcoder for video stream.
206            transcoders.insert(
207                ist_index,
208                Transcoder::new(
209                    &ist,
210                    &mut octx,
211                    ost_index as _,
212                    x264_opts.to_owned(),
213                    Some(ist_index) == best_video_stream_index,
214                )
215                .unwrap(),
216            );
217        } else {
218            // Set up for stream copy for non-video stream.
219            let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
220            ost.set_parameters(ist.parameters());
221            // We need to set codec_tag to 0 lest we run into incompatible codec tag
222            // issues when muxing into a different container format. Unfortunately
223            // there's no high level API to do this (yet).
224            unsafe {
225                (*ost.parameters().as_mut_ptr()).codec_tag = 0;
226            }
227        }
228        ost_index += 1;
229    }
230
231    octx.set_metadata(ictx.metadata().to_owned());
232    format::context::output::dump(&octx, 0, Some(&output_file));
233    octx.write_header().unwrap();
234
235    for (ost_index, _) in octx.streams().enumerate() {
236        ost_time_bases[ost_index] = octx.stream(ost_index as _).unwrap().time_base();
237    }
238
239    for (stream, mut packet) in ictx.packets() {
240        let ist_index = stream.index();
241        let ost_index = stream_mapping[ist_index];
242        if ost_index < 0 {
243            continue;
244        }
245        let ost_time_base = ost_time_bases[ost_index as usize];
246        match transcoders.get_mut(&ist_index) {
247            Some(transcoder) => {
248                packet.rescale_ts(stream.time_base(), transcoder.decoder.time_base());
249                transcoder.send_packet_to_decoder(&packet);
250                transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
251            }
252            None => {
253                // Do stream copy on non-video streams.
254                packet.rescale_ts(ist_time_bases[ist_index], ost_time_base);
255                packet.set_position(-1);
256                packet.set_stream(ost_index as _);
257                packet.write_interleaved(&mut octx).unwrap();
258            }
259        }
260    }
261
262    // Flush encoders and decoders.
263    for (ost_index, transcoder) in transcoders.iter_mut() {
264        let ost_time_base = ost_time_bases[*ost_index];
265        transcoder.send_eof_to_decoder();
266        transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
267        transcoder.send_eof_to_encoder();
268        transcoder.receive_and_process_encoded_packets(&mut octx, ost_time_base);
269    }
270
271    octx.write_trailer().unwrap();
272}