-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ffmpeg to grab video thumbnails.
- Loading branch information
Showing
8 changed files
with
116 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
let | ||
# put this on stable once Cargo 1.7 hits | ||
pkgs = import (fetchTarball("channel:nixpkgs-unstable")) {}; | ||
in pkgs.mkShell { | ||
buildInputs = with pkgs; [ cargo rustc rust-analyzer bacon clippy openssl pkg-config ]; | ||
{ pkgs ? import <nixpkgs> {} }: | ||
|
||
pkgs.mkShell { | ||
buildInputs = with pkgs; [ cargo ffmpeg rustc rust-analyzer bacon clippy openssl pkg-config ]; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{fs, path::Path, process::Command}; | ||
|
||
pub fn get_video_duration(path: &Path) -> anyhow::Result<f32> { | ||
let mut command = Command::new("ffprobe"); | ||
|
||
command.args([ | ||
"-loglevel", | ||
"error", | ||
"-of", | ||
"csv=p=0", | ||
"-show_entries", | ||
"format=duration", | ||
]); | ||
|
||
command.arg(path); | ||
|
||
let output = command.output()?; | ||
let output = String::from_utf8(output.stdout)?; | ||
|
||
Ok(output.trim().parse()?) | ||
} | ||
|
||
pub fn get_video_thumbnail(path: &Path) -> anyhow::Result<Vec<u8>> { | ||
let duration = get_video_duration(path)?; | ||
let tmpfile = tempfile::Builder::new().suffix(".jpg").tempfile()?; | ||
|
||
let mut command = Command::new("ffmpeg"); | ||
|
||
command.arg("-y"); | ||
command.args(["-loglevel", "error"]); | ||
command.arg("-ss"); | ||
command.arg((duration / 2.0).to_string()); | ||
command.arg("-i"); | ||
command.arg(path); | ||
command.args(["-frames:v", "1", "-update", "true"]); | ||
command.arg(tmpfile.path()); | ||
|
||
if !command.status()?.success() { | ||
anyhow::bail!("could not create thumbnail"); | ||
} | ||
|
||
Ok(fs::read(tmpfile.path())?) | ||
} |
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