Skip to main content

rff_cli/
ffmpeg.rs

1//! The `ffmpeg` binary's logic: parse args, print the usual banners/listings,
2//! and dispatch transcode jobs into the engine.
3
4use std::process::ExitCode;
5
6use rff::Engine;
7
8use crate::args::{self, Action};
9
10/// Entry point used by `src/bin/ffmpeg.rs`. `args` is everything after argv[0].
11pub fn run(args: Vec<String>) -> ExitCode {
12    let cli = match args::parse(&args) {
13        Ok(cli) => cli,
14        Err(err) => {
15            eprintln!("Error parsing options: {err}");
16            return ExitCode::FAILURE;
17        }
18    };
19
20    // FFmpeg prints its banner to stderr unless -hide_banner.
21    if !cli.hide_banner {
22        eprint!("{}", banner());
23    }
24    for warning in &cli.warnings {
25        eprintln!("[warning] {warning}");
26    }
27
28    let engine = Engine::new();
29
30    match cli.action {
31        Action::Version => {
32            print_version();
33            ExitCode::SUCCESS
34        }
35        Action::Help => {
36            print_help();
37            ExitCode::SUCCESS
38        }
39        Action::ListCodecs => {
40            print_codecs(&engine);
41            ExitCode::SUCCESS
42        }
43        Action::ListFormats => {
44            print_formats(&engine);
45            ExitCode::SUCCESS
46        }
47        Action::Transcode(spec) => {
48            if spec.inputs.is_empty() && spec.outputs.is_empty() {
49                print_usage();
50                return ExitCode::FAILURE;
51            }
52            match rff::transcode::run(&engine, &spec) {
53                Ok(report) => {
54                    for warning in &report.warnings {
55                        eprintln!("[warning] {warning}");
56                    }
57                    eprintln!(
58                        "done — {} packet(s) written, {} frame(s) decoded",
59                        report.packets_written, report.frames_decoded
60                    );
61                    ExitCode::SUCCESS
62                }
63                Err(err) => {
64                    eprintln!("[error] {err}");
65                    ExitCode::FAILURE
66                }
67            }
68        }
69    }
70}
71
72/// The startup banner. Deliberately NOT branded as "FFmpeg" — see the trademark
73/// note in the README.
74pub fn banner() -> String {
75    format!(
76        "remade_ffmpeg_rs {ver} (ffmpeg-compatible CLI) — Remade With Rust, by Mata Network\n\
77         Not affiliated with or endorsed by the FFmpeg project. \"FFmpeg\" is a trademark of Fabrice Bellard.\n",
78        ver = rff::VERSION,
79    )
80}
81
82/// `-version` output (to stdout).
83pub fn print_version() {
84    println!("remade_ffmpeg_rs version {}", rff::VERSION);
85    println!("A clean-room, permissively-licensed media toolkit written in Rust.");
86    println!("Apache-2.0. https://github.com/Remade-With-Rust/remade_ffmpeg_rs");
87}
88
89/// Minimal `-h` text.
90fn print_help() {
91    println!("{}", banner());
92    println!("Usage: ffmpeg [options] [-i input]... [output options] output\n");
93    println!("Common options:");
94    println!("  -i FILE            add an input file (repeatable)");
95    println!("  -f FMT             force container format for the next input/output");
96    println!("  -c:v CODEC         video codec for the output (e.g. h264, copy)");
97    println!("  -c:a CODEC         audio codec for the output (e.g. opus, copy)");
98    println!("  -b:v / -b:a RATE   target bitrate (e.g. 2M, 128k)");
99    println!("  -preset P          encoder speed/quality (h264: fast|medium|slow)");
100    println!("  -profile P         encoder profile (h264: baseline|main|high)");
101    println!("  -g N               keyframe interval in frames");
102    println!("  -vf FILTERS        video filter graph (e.g. scale=640:480, crop=W:H:X:Y)");
103    println!("  -pix_fmt FMT       encoder pixel format (e.g. yuv420p, yuv444p, yuv420p10le)");
104    println!("  -map SPEC          select streams: INPUT[:v|:a|:N] (repeatable)");
105    println!("  -vn / -an / -sn    drop video / audio / subtitle streams from the output");
106    println!("  -y / -n            overwrite / never overwrite the output");
107    println!("  -codecs            list supported codecs");
108    println!("  -formats           list supported container formats");
109    println!("  -hide_banner       suppress the startup banner");
110    println!("  -version           print version and exit");
111}
112
113/// One-line usage hint, printed when invoked with nothing to do.
114fn print_usage() {
115    eprintln!("Usage: ffmpeg [options] [-i input]... output   (try `ffmpeg -h`)");
116}
117
118/// `ffmpeg -codecs`: list every registered codec with its capabilities.
119fn print_codecs(engine: &Engine) {
120    println!("Codecs:");
121    println!(" D. = decoding supported");
122    println!(" .E = encoding supported");
123    println!(" -----");
124    let mut codecs: Vec<_> = engine.codecs.iter().collect();
125    codecs.sort_by_key(|c| c.name);
126    for codec in codecs {
127        let d = if codec.can_decode() { 'D' } else { '.' };
128        let e = if codec.can_encode() { 'E' } else { '.' };
129        println!(
130            " {d}{e} {:<9} {:<8} {}",
131            codec.media_type.to_string(),
132            codec.name,
133            codec.long_name
134        );
135    }
136}
137
138/// `ffmpeg -formats`: list every registered container format.
139fn print_formats(engine: &Engine) {
140    println!("File formats:");
141    println!(" D. = demuxing supported");
142    println!(" .E = muxing supported");
143    println!(" -----");
144    let mut formats: Vec<_> = engine.formats.iter().collect();
145    formats.sort_by_key(|f| f.name);
146    for format in formats {
147        let d = if format.can_demux() { 'D' } else { '.' };
148        let e = if format.can_mux() { 'E' } else { '.' };
149        println!(" {d}{e} {:<8} {}", format.name, format.long_name);
150    }
151}