-
-
Save dogamak/8068448e285803c40e845866e72aa507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ^^^ ffmpeg::init() is called in an other function | |
let mut context = ffmpeg::format::input(&path)?; | |
let duration; | |
let mut video; | |
let stream_index; | |
for stream in context.streams() { | |
println!("Stream #{}", stream.index()); | |
println!(" Duration: {}", stream.duration()); | |
println!(" Frames: {}", stream.frames()); | |
} | |
{ | |
let stream = context.streams().best(ffmpeg::media::Type::Video).unwrap(); | |
stream_index = stream.index(); | |
duration = stream.duration(); | |
println!("Frames: {}", stream.frames()); | |
video = stream.codec().decoder().video().unwrap(); | |
} | |
let tmp_dir = TempDir::new("sidekick_thumbnails")?; | |
let args = format!("width={}:height={}:pix_fmt={}:time_base={}:sar={}", | |
video.width(), | |
video.height(), | |
"yuv420p", | |
video.time_base(), | |
video.aspect_ratio()); | |
let mut filter = filter::Graph::new(); | |
filter.add(&filter::find("buffer").unwrap(), "in", &args); | |
filter.add(&filter::find("buffersink").unwrap(), "out", ""); | |
filter.output("in", 0)? | |
.input("out", 0)? | |
.parse("thumbnail=100"); | |
filter.validate()?; | |
let mut decoded = ffmpeg::frame::Video::empty(); | |
for i in 0..number { | |
let offset = (((duration as f64)*((i as f64)+0.5))/(number as f64)) as i64; | |
context.seek(offset, ..offset); | |
println!("Seeking to {}", offset); | |
let mut packets = 0; | |
for (stream, mut packet) in context.packets() { | |
if stream.index() == stream_index { | |
packets += 1; | |
println!("{}: {}", stream.index(), packets); | |
if let Ok(_) = video.decode(&packet, &mut decoded) { | |
println!("{}x{}", decoded.width(), decoded.height()); | |
filter.get("in").unwrap().source().add(&decoded); | |
} | |
if packets == 100 { | |
break; | |
} | |
} | |
} | |
} | |
filter.get("in").unwrap().source().flush(); | |
let mut out = 0; | |
while let Ok(_) = filter.get("out").unwrap().sink().frame(&mut decoded) { | |
println!("> {}", out); | |
out += 1; | |
let buf = image::ImageBuffer::<image::Rgb<u8>,&[u8]>::from_raw(decoded.width(), decoded.height(), decoded.data(0)).unwrap(); | |
buf.save(Path::new("/tmp/").join(format!("thumb{}.png", out))).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment