use std::path::Path; use std::process; use std::{thread, time}; use log::{info}; use crate::config::Config; use crate::util::io; use super::repository::Repository; pub struct Transcoder { config: Config, repository: Repository, } impl Transcoder { pub fn new(config: Config, repository: Repository) -> Transcoder { return Transcoder{ config: config, repository: repository, } } pub fn start(self) { info!("Starting transcoder..."); loop { // search for files in ingest let ingest_files = self.repository.search_ingest(); // check if we found any files to transcode if ingest_files.len() < 1 { info!("There were no files found in ingest to transcode; skipping run."); } else { for i in ingest_files { let ingest_path = Path::new(&self.repository.ingest_dir).join(&i); if io::is_file_locked(&ingest_path.to_str().unwrap()) { info!("{} is currently open in another program; skipping it for this run.", &i); continue; } // copy the file to the archive self.repository.archive_file(&i); // perform the video transcode self.transcode(&i); // remove the source file self.repository.cleanup_file(&i); } } // put the loop to sleep for X minutes let sleep_minutes = time::Duration::from_secs((self.config.transcoder.interval * 60).into()); thread::sleep(sleep_minutes); } } fn transcode(&self, file: &str) { let ingest_file = Path::new(&self.repository.ingest_dir).join(file); let output_file = Path::new(&self.repository.output_dir).join(file); let cmd_output = process::Command::new("/usr/bin/ffmpeg") .arg("-i") .arg(&*ingest_file.to_string_lossy()) .arg("-y") .arg("-f") .arg(&self.config.transcoder.video_format) .arg("-c:v") .arg(&self.config.transcoder.video_codec) .arg("-s") .arg(&self.config.transcoder.video_resolution) .arg("-r") .arg(format!("{}", self.config.transcoder.video_framerate)) .arg("-vf") .arg(format!("format={}", &self.config.transcoder.video_color)) .arg("-profile:v").arg(&self.config.transcoder.video_profile) .arg("-c:a") .arg(&self.config.transcoder.audio_codec) .arg(&*output_file.to_string_lossy()) .output() .expect("Failed to execute command"); assert!(cmd_output.status.success()); let results_raw = &String::from_utf8_lossy(&cmd_output.stderr); info!("{}", results_raw); } }