Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slow transcoder (use mediainfo id instead of sha1) #197

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions front/packages/ui/src/player/video.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { useAtomValue, useSetAtom, useAtom } from "jotai";
import { useYoshiki } from "yoshiki";
import SubtitleOctopus from "libass-wasm";
import { playAtom, PlayMode, playModeAtom, subtitleAtom } from "./state";
import Hls, { Level } from "hls.js";
import Hls, { Level, LoadPolicy } from "hls.js";
import { useTranslation } from "react-i18next";
import { Menu } from "@kyoo/primitives";
import toVttBlob from "srt-webvtt";
Expand All @@ -54,6 +54,23 @@ let client_id = typeof window === "undefined" ? "ssr" : uuidv4();
const initHls = async (): Promise<Hls> => {
if (hls !== null) return hls;
const token = await getToken();

const loadPolicy: LoadPolicy = {
default: {
maxTimeToFirstByteMs: Infinity,
maxLoadTimeMs: 60_000,
timeoutRetry: {
maxNumRetry: 2,
retryDelayMs: 0,
maxRetryDelayMs: 0,
},
errorRetry: {
maxNumRetry: 1,
retryDelayMs: 0,
maxRetryDelayMs: 0,
},
}
};
hls = new Hls({
xhrSetup: (xhr) => {
if (token) xhr.setRequestHeader("Authorization", `Bearer: ${token}`);
Expand All @@ -62,6 +79,12 @@ const initHls = async (): Promise<Hls> => {
autoStartLoad: false,
// debug: true,
startPosition: 0,
fragLoadPolicy: loadPolicy,
keyLoadPolicy: loadPolicy,
certLoadPolicy: loadPolicy,
playlistLoadPolicy: loadPolicy,
manifestLoadPolicy: loadPolicy,
steeringManifestLoadPolicy: loadPolicy,
});
// hls.currentLevel = hls.startLevel;
return hls;
Expand Down Expand Up @@ -102,7 +125,7 @@ const Video = forwardRef<{ seek: (value: number) => void }, VideoProps>(function
useEffect(() => {
if (!ref.current || paused === ref.current.paused) return;
if (paused) ref.current?.pause();
else ref.current?.play().catch(() => {});
else ref.current?.play().catch(() => { });
}, [paused]);
useEffect(() => {
if (!ref.current || !volume) return;
Expand Down Expand Up @@ -134,7 +157,7 @@ const Video = forwardRef<{ seek: (value: number) => void }, VideoProps>(function
hls.on(Hls.Events.MANIFEST_LOADED, async () => {
try {
await ref.current?.play();
} catch {}
} catch { }
});
hls.on(Hls.Events.ERROR, (_, d) => {
if (!d.fatal || !hls?.media) return;
Expand Down Expand Up @@ -267,8 +290,8 @@ const toWebVtt = async (srtUrl: string) => {
const query = await fetch(srtUrl, {
headers: token
? {
Authorization: token,
}
Authorization: token,
}
: undefined,
});
const srt = await query.blob();
Expand Down
12 changes: 0 additions & 12 deletions transcoder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion transcoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ derive_more = "0.99.17"
reqwest = { version = "0.11.16", default_features = false, features = ["json", "rustls-tls"] }
utoipa = { version = "3", features = ["actix_extras"] }
json = "0.12.4"
sha-1 = "0.10.1"
21 changes: 13 additions & 8 deletions transcoder/src/identify.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use json::JsonValue;
use serde::Serialize;
use sha1::{Digest, Sha1};
use std::{
collections::HashMap,
fs, io,
collections::{hash_map::DefaultHasher, HashMap},
hash::{Hash, Hasher},
path::PathBuf,
process::Stdio,
str::{self, FromStr},
Expand Down Expand Up @@ -103,6 +102,7 @@ pub struct Chapter {
}

async fn extract(path: String, sha: &String, subs: &Vec<Subtitle>) {
println!("Extract subs and fonts for {}", path);
let mut cmd = Command::new("ffmpeg");
cmd.current_dir(format!("/metadata/{sha}/att/"))
.args(&["-dump_attachment:t", ""])
Expand Down Expand Up @@ -141,16 +141,21 @@ pub async fn identify(path: String) -> Result<MediaInfo, std::io::Error> {
assert!(mediainfo.status.success());
let output = json::parse(str::from_utf8(mediainfo.stdout.as_slice()).unwrap()).unwrap();

let mut file = fs::File::open(&path)?;
let mut hasher = Sha1::new();
io::copy(&mut file, &mut hasher)?;
let sha = format!("{:x}", hasher.finalize());

let general = output["media"]["track"]
.members()
.find(|x| x["@type"] == "General")
.unwrap();

let sha = general["UniqueID"]
.as_str()
.map(|x| x.to_string())
.unwrap_or_else(|| {
let mut hasher = DefaultHasher::new();
path.hash(&mut hasher);
general["File_Modified_Date"].to_string().hash(&mut hasher);
format!("{hash:x}", hash = hasher.finish())
});

let subs: Vec<Subtitle> = output["media"]["track"]
.members()
.filter(|x| x["@type"] == "Text")
Expand Down