feat: embedded subtitles, resumed downloads, subtitle-only track menu
Verified in the running app and against the files on disk. Subtitles are muxed into the download as a soft SubRip track rather than written beside it. Without --write-subs, yt-dlp fetches them, embeds them and removes the WebVTT, so a download is one file — checked with ffprobe: video, audio, one mov_text track, no sidecar. WebKit exposes it, so the player lists and shows it with no fetch at all, which also means it works offline. They are converted to SubRip on the way in. YouTube's WebVTT pins every cue to the left edge and fills it with karaoke timing tags; the first embed carried both and rendered clamped to the side of the picture. SubRip carries neither. Only the plain language is embedded — "en" and "en-orig" are usually the same captions, and nothing can collapse duplicates once they are muxed in. Downloads left unfinished when the app closes are picked up on the next launch, at the quality and language they were asked for, which the downloads table now records. Verified by leaving a row "running" with no process behind it and restarting: it downloaded and completed. The player's track menu is subtitles only — the audio renditions are gone, the original-audio default still being fixed in the manifest — and tracks read "English" rather than "en". Turning subtitles off in one video now sticks for the next. The guard that applies the preference keyed on the number of text tracks alone, which does not change from one video to the next, so a choice of Off was never re-applied; it keys on the video as well. Embedding no longer depends on the on/off preference, the same trap that kept fetching switched off: a file downloaded without a subtitle track can never gain one offline. Delete all is a trash icon.
This commit is contained in:
@@ -1026,7 +1026,7 @@ async fn cache_thumbnails(state: &State<'_, AppState>) {
|
||||
pub async fn download_video(
|
||||
video_id: String,
|
||||
quality: String,
|
||||
sub_langs: String,
|
||||
sub_lang: String,
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<(), String> {
|
||||
@@ -1038,6 +1038,7 @@ pub async fn download_video(
|
||||
{
|
||||
let db = state.db.lock().await;
|
||||
db.set_download_state(&video_id, DownloadState::Queued, None)?;
|
||||
db.set_download_request(&video_id, &quality, &sub_lang)?;
|
||||
}
|
||||
let _ = app.emit(
|
||||
"download:state",
|
||||
@@ -1070,6 +1071,7 @@ pub async fn download_video(
|
||||
.join(downloader::OUTPUT_TEMPLATE)
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
let sub_langs = downloader::embed_sub_langs_for(&sub_lang);
|
||||
let mut args = downloader::build_args(&video_id, &out_template, &quality, &sub_langs);
|
||||
// Without this yt-dlp looks for ffmpeg on PATH, which a bundled app has no
|
||||
// reason to have. Merging video and audio would fail on a clean machine.
|
||||
@@ -1443,6 +1445,18 @@ pub async fn fetch_subtitles(
|
||||
Ok(read_vtt_dir(&dir).await)
|
||||
}
|
||||
|
||||
/// Downloads that were still going when the app last closed.
|
||||
///
|
||||
/// Killing the app kills yt-dlp with it, leaving rows queued or running that no
|
||||
/// process backs. The front end hands these straight back to `download_video`,
|
||||
/// so they rejoin the same queue rather than needing a second code path.
|
||||
#[tauri::command]
|
||||
pub async fn interrupted_downloads(
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<Vec<(String, String, String)>, String> {
|
||||
state.db.lock().await.interrupted_downloads()
|
||||
}
|
||||
|
||||
/// Stops everything downloading or waiting to download.
|
||||
///
|
||||
/// Kills the running processes, then marks every row the database still calls
|
||||
|
||||
@@ -92,6 +92,9 @@ impl Db {
|
||||
let _ = conn.execute("ALTER TABLE videos ADD COLUMN duration INTEGER", []);
|
||||
let _ = conn.execute("ALTER TABLE channels ADD COLUMN last_error TEXT", []);
|
||||
let _ = conn.execute("ALTER TABLE channels ADD COLUMN last_checked INTEGER", []);
|
||||
// What a download was asked for, so it can be resumed as requested.
|
||||
let _ = conn.execute("ALTER TABLE downloads ADD COLUMN quality TEXT", []);
|
||||
let _ = conn.execute("ALTER TABLE downloads ADD COLUMN sub_lang TEXT", []);
|
||||
Ok(Db { conn })
|
||||
}
|
||||
|
||||
@@ -508,6 +511,40 @@ impl Db {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The quality and subtitle language a download was started with, so an
|
||||
/// interrupted one resumes as it was asked for rather than as the settings
|
||||
/// happen to read now.
|
||||
pub fn set_download_request(
|
||||
&self,
|
||||
video_id: &str,
|
||||
quality: &str,
|
||||
sub_lang: &str,
|
||||
) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE downloads SET quality = ?2, sub_lang = ?3 WHERE video_id = ?1",
|
||||
params![video_id, quality, sub_lang],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Downloads left unfinished, as (video_id, quality, sub_lang). Killing the
|
||||
/// app leaves rows queued or running with no process behind them.
|
||||
pub fn interrupted_downloads(&self) -> Result<Vec<(String, String, String)>, String> {
|
||||
let mut stmt = self
|
||||
.conn
|
||||
.prepare(
|
||||
"SELECT video_id, COALESCE(quality, ''), COALESCE(sub_lang, '')
|
||||
FROM downloads WHERE state IN ('queued','running')",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rows = stmt
|
||||
.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))
|
||||
.map_err(|e| e.to_string())?;
|
||||
rows.collect::<Result<Vec<_>, _>>().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn set_download_state(
|
||||
&self,
|
||||
video_id: &str,
|
||||
|
||||
+49
-16
@@ -111,21 +111,28 @@ pub fn build_args(
|
||||
];
|
||||
|
||||
if !sub_langs.is_empty() {
|
||||
// Subtitles come along for offline use, including YouTube's
|
||||
// auto-generated ones. WebVTT beside the video rather than muxed in:
|
||||
// WebKit reads a <track> reliably and largely ignores subtitle streams
|
||||
// inside an MP4.
|
||||
// Muxed into the file as a soft subtitle track, not written beside it
|
||||
// and not burned into the picture. Without --write-subs, yt-dlp fetches
|
||||
// the captions, embeds them, and removes the WebVTT files, so a
|
||||
// download is one self-contained file.
|
||||
//
|
||||
// Auto-generated captions are included: on most videos they are the
|
||||
// only ones there are.
|
||||
//
|
||||
// The languages are named exactly. A wildcard like "en.*" also matches
|
||||
// every machine-translated variant YouTube offers — en-en-US, en-de and
|
||||
// dozens more — and asking for all of them earns an HTTP 429.
|
||||
args.extend([
|
||||
"--write-subs".into(),
|
||||
"--write-auto-subs".into(),
|
||||
"--embed-subs".into(),
|
||||
"--sub-format".into(),
|
||||
"vtt".into(),
|
||||
// Converted to SubRip on the way in. YouTube's WebVTT pins every
|
||||
// cue to the left edge and fills it with karaoke timing tags;
|
||||
// SubRip carries neither, so the embedded track is plain centred
|
||||
// text rather than something clinging to the side of the picture.
|
||||
"--convert-subs".into(),
|
||||
"vtt".into(),
|
||||
"srt".into(),
|
||||
"--sub-langs".into(),
|
||||
sub_langs.to_string(),
|
||||
]);
|
||||
@@ -161,10 +168,12 @@ pub fn subs_only_args(video_id: &str, out_template: &str, sub_langs: &str) -> Ve
|
||||
]
|
||||
}
|
||||
|
||||
/// The subtitle languages to request for a preference, or empty for none.
|
||||
/// The subtitle languages to fetch for a preference, or empty for none.
|
||||
///
|
||||
/// Only the language itself and YouTube's "-orig" variant; anything broader
|
||||
/// pulls in machine translations by the dozen.
|
||||
/// The language itself and YouTube's "-orig" variant, which is the original
|
||||
/// rather than a machine translation. Anything broader pulls in translations by
|
||||
/// the dozen. The two are usually byte-identical, so whoever reads them back
|
||||
/// collapses duplicates.
|
||||
pub fn sub_langs_for(pref: &str) -> String {
|
||||
if pref.is_empty() || pref == "off" {
|
||||
String::new()
|
||||
@@ -173,6 +182,18 @@ pub fn sub_langs_for(pref: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// The languages to embed in a download: just the one.
|
||||
///
|
||||
/// Nothing downstream can collapse duplicates once they are muxed in, and
|
||||
/// asking for "en,en-orig" gives a file with the same captions on two tracks.
|
||||
pub fn embed_sub_langs_for(pref: &str) -> String {
|
||||
if pref.is_empty() || pref == "off" {
|
||||
String::new()
|
||||
} else {
|
||||
pref.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// yt-dlp reports the final path via `--print after_move:`, which is more
|
||||
/// reliable than guessing the extension after a merge.
|
||||
pub fn parse_final_path(line: &str) -> Option<String> {
|
||||
@@ -270,24 +291,36 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtitles_are_requested_including_auto_generated() {
|
||||
let args = build_args("abc", "/tmp/o.%(ext)s", "best", "en,en-orig");
|
||||
assert!(args.contains(&"--write-subs".to_string()));
|
||||
fn subtitles_are_embedded_not_written_beside_the_video() {
|
||||
let args = build_args("abc", "/tmp/o.%(ext)s", "best", "en");
|
||||
assert!(args.contains(&"--embed-subs".to_string()));
|
||||
// The auto-generated track is the only one many videos have.
|
||||
assert!(args.contains(&"--write-auto-subs".to_string()));
|
||||
assert!(args.contains(&"en,en-orig".to_string()));
|
||||
// WebVTT, because that is what a <track> element can load.
|
||||
assert!(args.contains(&"vtt".to_string()));
|
||||
assert!(args.contains(&"en".to_string()));
|
||||
// Without --write-subs, yt-dlp removes the WebVTT files after
|
||||
// embedding, leaving one self-contained file.
|
||||
assert!(!args.contains(&"--write-subs".to_string()));
|
||||
// SubRip, so YouTube's edge-pinned cue positioning does not come with.
|
||||
assert!(args.contains(&"srt".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_preference_means_no_subtitle_requests_at_all() {
|
||||
let args = build_args("abc", "/tmp/o.%(ext)s", "best", "");
|
||||
assert!(!args.contains(&"--write-subs".to_string()));
|
||||
assert!(!args.contains(&"--embed-subs".to_string()));
|
||||
assert!(!args.contains(&"--write-auto-subs".to_string()));
|
||||
assert!(!args.contains(&"--sub-langs".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedding_asks_for_one_language_and_fetching_asks_for_both() {
|
||||
// Two identical tracks cannot be collapsed once they are muxed in.
|
||||
assert_eq!(embed_sub_langs_for("en"), "en");
|
||||
assert_eq!(embed_sub_langs_for("off"), "");
|
||||
// Reading them back can collapse duplicates, so breadth is free there.
|
||||
assert_eq!(sub_langs_for("en"), "en,en-orig");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_subtitle_failure_must_not_abort_the_video() {
|
||||
// yt-dlp aborts the whole job on the first error without this.
|
||||
|
||||
@@ -158,6 +158,7 @@ fn build_menu(app: &tauri::AppHandle) -> tauri::Result<Menu<tauri::Wry>> {
|
||||
commands::download_video,
|
||||
commands::cancel_download,
|
||||
commands::cancel_all_downloads,
|
||||
commands::interrupted_downloads,
|
||||
commands::delete_download,
|
||||
commands::delete_all_downloads,
|
||||
commands::list_subtitles,
|
||||
|
||||
Reference in New Issue
Block a user