Skip to content

Commit 8cb84c7

Browse files
committed
feat: add gif generation
1 parent e89a748 commit 8cb84c7

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/ffmpeg.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ fn run_command(command: &str) {
2020
println!("{}", result);
2121
}
2222

23-
// fn create_preview_gif() {}
23+
pub fn create_preview_gif(input: &str, output: &str) {
24+
let path = format!("{}/gif", output);
25+
if fs::metadata(&path).is_err() {
26+
fs::create_dir_all(&path).unwrap();
27+
}
28+
29+
let command = format!(
30+
"ffmpeg -ss 30 -t 3 -i {} -vf \"fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse\" -loop 0 {}/{}.gif", input, path, output
31+
);
32+
33+
run_command(&command);
34+
}
2435

2536
pub fn create_preview_image(input: &str, output: &str) {
2637
let path = format!("{}/lg", output);

src/main.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::ffmpeg::create_preview_image;
1+
use crate::ffmpeg::{create_preview_gif, create_preview_image};
22
use clap::{App, Arg};
33
use std::process;
44

55
enum Flags {
6-
_Gif,
6+
Gif,
77
Hls,
88
_Metadata,
99
PreviewImage,
@@ -32,7 +32,7 @@ impl ToString for Flags {
3232
impl Flags {
3333
fn as_str(&self) -> &'static str {
3434
match self {
35-
Flags::_Gif => "gif",
35+
Flags::Gif => "gif",
3636
Flags::Hls => "HLS",
3737
Flags::_Metadata => "metadata",
3838
Flags::PreviewImage => "preview_image",
@@ -85,6 +85,13 @@ fn main() {
8585
.help("Generate hls chunks")
8686
.takes_value(false),
8787
)
88+
.arg(
89+
Arg::with_name(Flags::Gif.as_str())
90+
.short('g')
91+
.long(Flags::Gif.as_str())
92+
.help("Generate gif")
93+
.takes_value(false),
94+
)
8895
.get_matches();
8996

9097
let input = matches.value_of("input").unwrap_or(Flags::Help.as_str());
@@ -114,6 +121,11 @@ fn main() {
114121
create_hls_encoding(&out.file_name, &path);
115122
}
116123

124+
let gen_gif = matches.is_present(Flags::Gif.as_str());
125+
if gen_gif {
126+
create_preview_gif(&out.file_name, &path);
127+
}
128+
117129
/*
118130
TODO:
119131
[x] Take filename as argument to process

0 commit comments

Comments
 (0)