Skip to content

Commit

Permalink
Merge pull request #78 from madiele/main
Browse files Browse the repository at this point in the history
v1.0.2
  • Loading branch information
madiele authored Jun 13, 2023
2 parents 2415e74 + ef051c8 commit 8313258
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Cargo.lock
.dev.env
.env
devcontainerInstall.log
.vscode/settings.json
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ redis = { version = "0.23", features = ["tokio-comp"] }
feed-rs = "1.3.0"
md5 = "0.7.0"
mime = "0.3.17"
cached = { version = "0.43.0", features = ["redis_tokio"] }
cached = { version = "0.44.0", features = ["redis_tokio"] }
iso8601-duration = "0.2.0"
chrono = "0.4.24"

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.68 as builder
FROM rust:1.70 as builder

RUN cd /tmp && USER=root cargo new --bin vod2pod
WORKDIR /tmp/vod2pod
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!make
SHELL = /bin/bash
define write-env =
touch .env
grep -q '^TRANSCODE=' .env || echo 'TRANSCODE=true' >> .env
Expand All @@ -25,17 +26,19 @@ install-ubuntu-deps:
sudo apt install -y ffmpeg python3-pip redis
echo installing rust + cargo
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "${HOME}/.cargo/env"
cargo install cargo-watch
pip3 install yt-dlp --yes
pip3 install yt-dlp

install-fedora-deps:
$(write-env)
sudo dnf update
sudo dnf install -y ffmpeg python3-pip redis
echo installing rust + cargo
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "${HOME}/.cargo/env"
cargo install cargo-watch
pip3 install yt-dlp --yes
pip3 install yt-dlp

start-deps:
@if which docker-compose >/dev/null; then \
Expand Down
11 changes: 11 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Security Policy

## Supported Versions
| Version | Supported |
| ------- | ------------------ |
| 1.x.x | :white_check_mark: |
| 0.x.x | :x: |

## Reporting a Vulnerability

If you come across a vulnerability, please use the Security tab and report it privately. I will do my best to fix it.
73 changes: 73 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use log::warn;
use serde::Serialize;

pub fn conf() -> impl Conf {
EnvConf {}
}
Expand All @@ -19,6 +22,7 @@ pub enum ConfName {
TranscodingEnabled,
SubfolderPath,
ValidUrlDomains,
AudioCodec,
}

struct EnvConf { }
Expand Down Expand Up @@ -57,6 +61,75 @@ impl Conf for EnvConf {
Ok(folder)
},
ConfName::ValidUrlDomains => Ok(std::env::var("VALID_URL_DOMAINS").unwrap_or_else(|_| "https://*.youtube.com/,https://youtube.com/,https://youtu.be/,https://*.youtu.be/,https://*.twitch.tv/,https://twitch.tv/,https://*.googlevideo.com/,https://*.cloudfront.net/".to_string())),
ConfName::AudioCodec => Ok(std::env::var("AUDIO_CODEC").map(|c| {
match c.as_str() {
"MP3" => c,
"OPUS" => c,
"OGG" => "OGG_VORBIS".to_string(),
"VORBIS" => "OGG_VORBIS".to_string(),
"OGG_VORBIS" => c,
_ => {
warn!("Unrecognized codec \"{c}\". Defaulting to MP3.");
"MP3".to_string()
}
}
}).unwrap_or_else(|_| "MP3".to_string())),
}
}
}

#[derive(Serialize,Clone, Copy)]
pub enum AudioCodec {
MP3,
Opus,
OGGVorbis,
}

impl AudioCodec {
pub fn get_ffmpeg_codec_str(&self) -> &'static str {
match self {
AudioCodec::MP3 => "libmp3lame",
AudioCodec::Opus => {
warn!("seeking is not supported with OPUS codec ");
"libopus"
},
AudioCodec::OGGVorbis => {
warn!("seeking is not supported with OGG_VORBIS codec ... ");
"libvorbis"
},
}
}

pub fn get_extension_str(&self) -> &'static str {
match self {
AudioCodec::MP3 => "mp3",
AudioCodec::Opus => "webm",
AudioCodec::OGGVorbis => "webm",
}
}

pub fn get_mime_type_str(&self) -> &'static str {
match self {
AudioCodec::MP3 => "audio/mpeg",
AudioCodec::Opus => "audio/webm",
AudioCodec::OGGVorbis => "audio/webm",
}
}
}

impl From<String> for AudioCodec {
fn from(value: String) -> Self {
match value.as_str() {
"MP3" => AudioCodec::MP3,
"OPUS" => AudioCodec::Opus,
"OGG_VORBIS" => AudioCodec::OGGVorbis,
_ => AudioCodec::MP3,
}
}
}

impl Default for AudioCodec {
fn default() -> Self {
Self::MP3
}
}
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;
use simple_logger::SimpleLogger;
use std::{collections::HashMap, time::Instant};
use vod2pod_rss::{
transcoder::{ Transcoder, FfmpegParameters, FFMPEGAudioCodec }, rss_transcodizer::RssTranscodizer, url_convert, configs::{Conf, conf, ConfName},
transcoder::{ Transcoder, FfmpegParameters }, rss_transcodizer::RssTranscodizer, url_convert, configs::{Conf, conf, ConfName },
};

#[actix_web::main]
Expand Down Expand Up @@ -201,10 +201,11 @@ async fn transcode_to_mp3(

let seek_secs = ((start_bytes as f32) / (total_streamable_bytes as f32)) * (duration_secs as f32);
debug!("choosen seek_time: {seek_secs}");
let codec = conf().get(ConfName::AudioCodec).unwrap().into();
let ffmpeg_paramenters = FfmpegParameters {
seek_time: seek_secs,
url: stream_url.clone(),
audio_codec: FFMPEGAudioCodec::Libmp3lame,
audio_codec: codec,
bitrate_kbit: bitrate,
max_rate_kbit: bitrate * 30,
expected_bytes_count: expected_bytes,
Expand All @@ -219,7 +220,7 @@ async fn transcode_to_mp3(

response_builder.insert_header(("Accept-Ranges", "bytes"))
.insert_header(("Content-Range", format!("bytes {start_bytes}-{end_bytes}/{total_streamable_bytes}")))
.content_type("audio/mpeg")
.content_type(codec.get_mime_type_str())
.no_chunking((expected_bytes).try_into().unwrap())
.streaming(stream)
}
Expand Down
6 changes: 4 additions & 2 deletions src/rss_transcodizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tokio::process::Command;

use std::str;

use crate::configs::{conf, ConfName, Conf};
use crate::configs::{conf, ConfName, Conf, AudioCodec};

#[cfg_attr(not(test),
io_cached(
Expand Down Expand Up @@ -448,13 +448,15 @@ async fn convert_item(params: ConvertItemsParams) -> Option<Item> {
.parse()
.expect("MP3_BITRATE must be a number");
let generation_uuid = uuid::Uuid::new_v4().to_string();
let codec: AudioCodec = conf().get(ConfName::AudioCodec).unwrap().into();
let ext = format!(".{}", codec.get_extension_str());
transcode_service_url
.query_pairs_mut()
.append_pair("bitrate", bitrate.to_string().as_str())
.append_pair("uuid", generation_uuid.as_str())
.append_pair("duration", duration_secs.to_string().as_str())
.append_pair("url", media_url.as_str())
.append_pair("ext", ".mp3"); //this should allways be last, some players refuse to play urls not ending in .mp3
.append_pair("ext", ext.as_str()); //this should allways be last, some players refuse to play urls not ending in .mp3

let enclosure = Enclosure {
length: (bitrate * 1024 * duration_secs).to_string(),
Expand Down
33 changes: 7 additions & 26 deletions src/transcoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,13 @@ use tokio::sync::mpsc::Receiver;
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::channel;

use crate::configs::{conf, ConfName, Conf};

#[derive(Serialize)]
pub enum FFMPEGAudioCodec {
Libmp3lame,
}

impl FFMPEGAudioCodec {
fn as_str(&self) -> &'static str {
match self {
FFMPEGAudioCodec::Libmp3lame => "libmp3lame",
}
}
}

impl Default for FFMPEGAudioCodec {
fn default() -> Self {
Self::Libmp3lame
}
}
use crate::configs::{conf, ConfName, Conf, AudioCodec};

#[derive(Serialize)]
pub struct FfmpegParameters {
pub seek_time: f32,
pub url: Url,
pub audio_codec: FFMPEGAudioCodec,
pub audio_codec: AudioCodec,
pub bitrate_kbit: usize,
pub max_rate_kbit: usize,
pub expected_bytes_count: usize,
Expand Down Expand Up @@ -101,7 +82,7 @@ impl Transcoder {
Self::get_ffmpeg_command(&FfmpegParameters {
seek_time: ffmpeg_paramenters.seek_time,
url: get_youtube_stream_url(&ffmpeg_paramenters.url).await?,
audio_codec: FFMPEGAudioCodec::Libmp3lame,
audio_codec: ffmpeg_paramenters.audio_codec.to_owned(),
bitrate_kbit: ffmpeg_paramenters.bitrate_kbit,
max_rate_kbit: ffmpeg_paramenters.max_rate_kbit,
expected_bytes_count: ffmpeg_paramenters.expected_bytes_count
Expand All @@ -121,9 +102,9 @@ impl Transcoder {
command_ref
.args(["-ss", ffmpeg_paramenters.seek_time.to_string().as_str()])
.args(["-i", ffmpeg_paramenters.url.as_str()])
.args(["-acodec", ffmpeg_paramenters.audio_codec.as_str()])
.args(["-acodec", ffmpeg_paramenters.audio_codec.get_ffmpeg_codec_str()])
.args(["-ab", format!("{}k", ffmpeg_paramenters.bitrate_kbit).as_str()])
.args(["-f", "mp3"])
.args(["-f", ffmpeg_paramenters.audio_codec.get_extension_str()])
.args(["-bufsize", (ffmpeg_paramenters.bitrate_kbit * 30).to_string().as_str()])
.args(["-maxrate", format!("{}k", ffmpeg_paramenters.max_rate_kbit).as_str()])
.args(["-timeout", "300"])
Expand Down Expand Up @@ -293,7 +274,7 @@ mod test {
seek_time: 30.0,
url: stream_url,
max_rate_kbit: 64,
audio_codec: FFMPEGAudioCodec::Libmp3lame,
audio_codec: AudioCodec::MP3,
bitrate_kbit: 3,
expected_bytes_count: 999,
};
Expand All @@ -320,7 +301,7 @@ mod test {
Some("-acodec") => {
let value = args.next().unwrap().to_str().unwrap();
info!("-acodec {}", value);
assert_eq!(value, params.audio_codec.as_str());
assert_eq!(value, params.audio_codec.get_ffmpeg_codec_str());
}
Some("-ab") => {
let value = args.next().unwrap().to_str().unwrap();
Expand Down

0 comments on commit 8313258

Please sign in to comment.