courier
Clone
https://git.tahinli.com/Tahinli/courier.git
0 watching·0 forks·Rust·AGPL-3.0·5.3 MiB·6d9eeb9·Tahinli·21 days ago·3 commits
| 1 | #![forbid(unsafe_code)] |
| 2 | use std::collections::HashMap; |
| 3 | use std::io::Write; |
| 4 | use std::process::{Command, Stdio}; |
| 5 | |
| 6 | const SR: usize = 44100; |
| 7 | const FPS: f32 = 30.0; |
| 8 | |
| 9 | fn decode(path: &str) -> Vec<f32> { |
| 10 | let out = Command::new("ffmpeg").args(["-v", "error", "-i", path, "-f", "f32le", "-ac", "2", "-ar", "44100", "-"]).output().expect("ffmpeg"); |
| 11 | out.stdout.chunks_exact(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect() |
| 12 | } |
| 13 | |
| 14 | fn main() { |
| 15 | let args: Vec<String> = std::env::args().collect(); |
| 16 | let (video, root, out) = (&args[1], &args[2], &args[3]); |
| 17 | let cues = std::fs::read_to_string(format!("{video}.cues")).expect("cues"); |
| 18 | let mut layers: Vec<String> = Vec::new(); |
| 19 | let mut vols: Vec<(f32, Vec<f32>)> = Vec::new(); |
| 20 | let mut shots: Vec<(f32, String, f32)> = Vec::new(); |
| 21 | for line in cues.lines() { |
| 22 | let p: Vec<&str> = line.split(' ').collect(); |
| 23 | match p[0] { |
| 24 | "H" => layers = p[1..].iter().map(|s| s.to_string()).collect(), |
| 25 | "L" => vols.push((p[1].parse().unwrap(), p[2..].iter().map(|v| v.parse().unwrap()).collect())), |
| 26 | "S" => shots.push((p[1].parse().unwrap(), p[2].to_string(), p[3].parse().unwrap())), |
| 27 | _ => {} |
| 28 | } |
| 29 | } |
| 30 | let t0 = 5.0 / FPS; |
| 31 | let dur = vols.last().map(|v| v.0).unwrap_or(0.0) - t0; |
| 32 | let n = (dur.max(0.0) * SR as f32) as usize; |
| 33 | let mut buf = vec![0.0f32; n * 2]; |
| 34 | let mut cache: HashMap<String, Vec<f32>> = HashMap::new(); |
| 35 | let mut load = |p: &str| -> Vec<f32> { cache.entry(p.to_string()).or_insert_with(|| decode(&format!("{root}/{p}"))).clone() }; |
| 36 | for (li, path) in layers.iter().enumerate() { |
| 37 | let data = load(path); |
| 38 | if data.len() < 2 { |
| 39 | continue; |
| 40 | } |
| 41 | let frames = data.len() / 2; |
| 42 | for k in 0..n { |
| 43 | let t = t0 + k as f32 / SR as f32; |
| 44 | let fi = t * FPS; |
| 45 | let i = (fi as usize).min(vols.len() - 1); |
| 46 | let j = (i + 1).min(vols.len() - 1); |
| 47 | let f = fi.fract(); |
| 48 | let v = vols[i].1[li] * (1.0 - f) + vols[j].1[li] * f; |
| 49 | if v <= 0.0005 { |
| 50 | continue; |
| 51 | } |
| 52 | let s = k % frames; |
| 53 | buf[k * 2] += data[s * 2] * v; |
| 54 | buf[k * 2 + 1] += data[s * 2 + 1] * v; |
| 55 | } |
| 56 | } |
| 57 | for (t, path, vol) in &shots { |
| 58 | let data = load(path); |
| 59 | let start = ((t - t0).max(0.0) * SR as f32) as usize; |
| 60 | for (i, x) in data.iter().enumerate() { |
| 61 | let k = start * 2 + i; |
| 62 | if k >= buf.len() { |
| 63 | break; |
| 64 | } |
| 65 | buf[k] += x * vol; |
| 66 | } |
| 67 | } |
| 68 | let bytes: Vec<u8> = buf.iter().flat_map(|x| (x * 0.9).tanh().to_le_bytes()).collect(); |
| 69 | let mut child = Command::new("ffmpeg") |
| 70 | .args(["-y", "-v", "error", "-i", video, "-f", "f32le", "-ar", "44100", "-ac", "2", "-i", "-", "-map", "0:v", "-map", "1:a", "-c:v", "copy", "-c:a", "aac", "-b:a", "160k", "-shortest", out]) |
| 71 | .stdin(Stdio::piped()) |
| 72 | .spawn() |
| 73 | .expect("ffmpeg"); |
| 74 | child.stdin.take().unwrap().write_all(&bytes).unwrap(); |
| 75 | child.wait().unwrap(); |
| 76 | } |