1use std::process::ExitCode;
5
6use rff::Engine;
7
8use crate::args::{self, Action};
9
10pub 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 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
72pub 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
82pub 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
89fn 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|:d|:N] (repeatable)");
105 println!(" -map_chapters N take chapters from input N (-1: none)");
106 println!(" -vn / -an / -sn drop video / audio / subtitle streams from the output");
107 println!(" -y / -n overwrite / never overwrite the output");
108 println!(" -codecs list supported codecs");
109 println!(" -formats list supported container formats");
110 println!(" -hide_banner suppress the startup banner");
111 println!(" -version print version and exit");
112}
113
114fn print_usage() {
116 eprintln!("Usage: ffmpeg [options] [-i input]... output (try `ffmpeg -h`)");
117}
118
119fn print_codecs(engine: &Engine) {
121 println!("Codecs:");
122 println!(" D. = decoding supported");
123 println!(" .E = encoding supported");
124 println!(" -----");
125 let mut codecs: Vec<_> = engine.codecs.iter().collect();
126 codecs.sort_by_key(|c| c.name);
127 for codec in codecs {
128 let d = if codec.can_decode() { 'D' } else { '.' };
129 let e = if codec.can_encode() { 'E' } else { '.' };
130 println!(
131 " {d}{e} {:<9} {:<8} {}",
132 codec.media_type.to_string(),
133 codec.name,
134 codec.long_name
135 );
136 }
137}
138
139fn print_formats(engine: &Engine) {
141 println!("File formats:");
142 println!(" D. = demuxing supported");
143 println!(" .E = muxing supported");
144 println!(" -----");
145 let mut formats: Vec<_> = engine.formats.iter().collect();
146 formats.sort_by_key(|f| f.name);
147 for format in formats {
148 let d = if format.can_demux() { 'D' } else { '.' };
149 let e = if format.can_mux() { 'E' } else { '.' };
150 println!(" {d}{e} {:<8} {}", format.name, format.long_name);
151 }
152}