Skip to content

Commit

Permalink
add hit post-processing via ffmpeg to convert to mp3 and cut off the …
Browse files Browse the repository at this point in the history
…beginning according to the offset
  • Loading branch information
Timtam committed Mar 5, 2024
1 parent af15bbb commit b6a696a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ FROM debian:bookworm-slim
# prepare the OS

RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
apt-get -y install --no-install-recommends libssl-dev ca-certificates && \
apt-get -y install --no-install-recommends libssl-dev ca-certificates ffmpeg && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Expand Down
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
dotenvy = "0.15.7"
ffmpeg-cli = "0.1.0"
hex = "0.4.3"
names = "0.14.0"
regex = "1.10.3"
Expand Down
35 changes: 33 additions & 2 deletions server/src/hits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use ffmpeg_cli::{FfmpegBuilder, File, Parameter};
use regex::Regex;
use rocket::{
fairing::{Fairing, Info, Kind},
Orbit, Rocket,
};
use rusty_ytdl::{Video, VideoOptions, VideoQuality, VideoSearchOptions};
use std::{env, fs::create_dir_all, path::Path};
use std::{
env,
fs::{create_dir_all, remove_file},
path::Path,
process::Stdio,
};

include!(concat!(env!("OUT_DIR"), "/hits.rs"));

Expand Down Expand Up @@ -63,9 +69,34 @@ impl Fairing for HitsterDownloader {
);

video
.download(format!("{}/{}.mp4", download_dir.as_str(), id))
.download(format!("{}/{}.opus", download_dir.as_str(), id))
.await
.unwrap();

println!("Post-processing opus to mp3...");

let in_file = format!("{}/{}.opus", download_dir.as_str(), id);
let out_file = format!("{}/{}.mp3", download_dir.as_str(), id);
let offset = format!("{}", hit.playback_offset);

let builder = FfmpegBuilder::new()
.stderr(Stdio::piped())
.option(Parameter::Single("nostdin"))
.option(Parameter::Single("y"))
.input(File::new(in_file.as_str()))
.output(
File::new(out_file.as_str())
.option(Parameter::KeyValue("ss", offset.as_str()))
.option(Parameter::Single("vn"))
.option(Parameter::Single("sn"))
.option(Parameter::Single("dn")),
);

let ffmpeg = builder.run().await.unwrap();

ffmpeg.process.wait_with_output().unwrap();

remove_file(in_file.as_str()).unwrap();
}
}
}
Expand Down

0 comments on commit b6a696a

Please sign in to comment.