from jpg to png

This commit is contained in:
Bel LaPointe
2023-12-27 21:42:53 -05:00
parent 46ca6064c3
commit f3b960c2f5
3 changed files with 405 additions and 8 deletions

View File

@@ -2,10 +2,11 @@ use std::str::FromStr;
use core::cmp::Ordering;
use std::fs;
use std::path::Path;
use std::io::Write;
pub fn screenshotify(input: &String) -> Result<Vec<String>, String> {
let output_d = format!("{}.d", input);
let ext = "jpg".to_string();
let ext = "png".to_string();
let mut result = vec![];
let _ = ify(input, |content_span| {
let output = format!("{}/{}.{}", output_d, result.len(), ext);
@@ -67,6 +68,21 @@ pub fn clip(output: &String, input: &String, content_span: ContentSpan) -> Resul
}
pub fn screenshot(output: &String, input: &String, ts: f32) -> Result<(), String> {
match screenshot_png(input, ts) {
Ok(png) => {
match std::fs::File::create(output) {
Ok(mut f) => {
f.write_all(&png).unwrap();
Ok(())
},
Err(msg) => Err(format!("failed to open {} for writing: {}", output, msg)),
}
},
Err(msg) => Err(msg),
}
}
pub fn screenshot_png(input: &String, ts: f32) -> Result<Vec<u8>, String> {
match std::process::Command::new("ffmpeg")
.args([
"-y",
@@ -74,10 +90,12 @@ pub fn screenshot(output: &String, input: &String, ts: f32) -> Result<(), String
"-i", input,
"-frames:v", "1",
"-q:v", "2",
output,
"-c:v", "png",
"-f", "image2pipe",
"-",
])
.output() {
Ok(_) => Ok(()),
Ok(output) => Ok(output.stdout),
Err(msg) => Err(format!("failed to ffmpeg screenshot {}: {}", input, msg)),
}
}
@@ -178,15 +196,19 @@ impl Inspection {
fn duration(&self) -> f32 {
let ts = self.lines.iter()
.filter(|x| (*x).contains("Duration: "))
.filter(|x| (*x).contains("start: "))
.filter(|x| (*x).contains("bitrate: "))
.nth(0)
.expect("did not find duration from ffmpeg")
.split(",").nth(0).unwrap()
.split(": ").nth(1).unwrap();
let pieces: Vec<_> = ts.split(":")
.map(|x| f32::from_str(x).unwrap())
.map(|x| f32::from_str(x))
.filter(|x| x.is_ok())
.map(|x| x.unwrap())
.collect();
if pieces.len() == 0 {
return 0.0;
}
assert_eq!(3, pieces.len());
let hours = pieces[0] * 60.0 * 60.0;
let minutes = pieces[1] * 60.0;
@@ -269,7 +291,7 @@ mod test_inspection {
#[test]
fn test_screenshot() {
let output = format!("{}.clipped.jpg", FILE);
let output = format!("{}.clipped.png", FILE);
screenshot(
&output,
&FILE.to_string(),
@@ -279,7 +301,7 @@ mod test_inspection {
let inspection = _inspect(&output);
assert_eq!(true, inspection.is_ok());
let inspection = inspection.unwrap();
assert_eq!(0.04, inspection.duration());
assert_eq!(0.0, inspection.duration());
}
#[test]