From e37b0f53052b6afdeafa8430b815b236073eb825 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 15 Sep 2026 10:54:54 -0600 Subject: [PATCH] Transcode: audio files, and Remove video to turn a video into an audio file video_codec 'none' (REMOVE_VIDEO) drops the video stream (-vn, audio required); an audio-only source is audio-only output whatever the video target says, and a video target on it is refused. Audio output gets an audio container (m4a/ogg/mp3) checked against the codec it will hold, and a video container asked for audio output is replaced by the codec's natural one. faststart only for mp4/m4a. Verification learned the audio-only shape and ffprobe's container names (mkv reports matroska). The modal offers 'Remove video (audio only)', switches to audio containers and follows the audio codec, reads Extract Audio, and opens for audio files (no video codec select). Co-Authored-By: Claude Fable 5.1 --- app/src/components/Vars.js | 17 ++ app/src/components/VideoEditMenu.js | 101 ++++++-- app/src/components/VideoEditMenu.test.js | 78 +++++- modules/videos/test/test_transcode_audio.py | 265 ++++++++++++++++++++ modules/videos/transcode.py | 137 ++++++++-- modules/videos/video/api.py | 8 +- 6 files changed, 547 insertions(+), 59 deletions(-) create mode 100644 modules/videos/test/test_transcode_audio.py diff --git a/app/src/components/Vars.js b/app/src/components/Vars.js index 60cac8b0..01631236 100644 --- a/app/src/components/Vars.js +++ b/app/src/components/Vars.js @@ -161,9 +161,12 @@ export const downloadAudioCodecOptions = [ // Codecs WROLPi's ffmpeg step can produce. Mirrors TRANSCODE_VIDEO_TARGETS / TRANSCODE_AUDIO_TARGETS / // TRANSCODE_CONTAINERS in modules/videos/transcode.py; the API rejects anything else. export const TRANSCODE_COPY = 'copy'; +// REMOVE_VIDEO in modules/videos/transcode.py: drop the video stream, the result is an audio file. +export const TRANSCODE_REMOVE_VIDEO = 'none'; export const transcodeVideoCodecOptions = [ {value: TRANSCODE_COPY, label: 'Keep (copy)'}, {value: 'h264', label: 'h264 (avc1)'}, + {value: TRANSCODE_REMOVE_VIDEO, label: 'Remove video (audio only)'}, ]; export const transcodeAudioCodecOptions = [ {value: TRANSCODE_COPY, label: 'Keep (copy)'}, @@ -171,10 +174,24 @@ export const transcodeAudioCodecOptions = [ {value: 'opus', label: 'opus'}, {value: 'mp3', label: 'mp3'}, ]; +// Containers for output with a video stream ... export const transcodeContainerOptions = [ {value: 'mp4', label: 'mp4'}, {value: 'mkv', label: 'mkv'}, ]; +// ... and for audio-only output. AUDIO_CONTAINER_CODECS in transcode.py says what each holds. +export const transcodeAudioContainerOptions = [ + {value: 'm4a', label: 'm4a (aac)'}, + {value: 'ogg', label: 'ogg (opus)'}, + {value: 'mp3', label: 'mp3'}, +]; + +/** The audio container a player expects for a codec (default_audio_container in transcode.py). */ +export function audioContainerForCodec(codec) { + if (codec === 'mp3') return 'mp3'; + if (['opus', 'vorbis', 'flac'].includes(codec)) return 'ogg'; + return 'm4a'; +} export const Downloaders = { Archive: 'archive', diff --git a/app/src/components/VideoEditMenu.js b/app/src/components/VideoEditMenu.js index 23812cfd..bcfaa1dc 100644 --- a/app/src/components/VideoEditMenu.js +++ b/app/src/components/VideoEditMenu.js @@ -5,8 +5,11 @@ import { Button, Confirm, Group, Icon, Menu, Modal, Progress, Select, Stack, Status, Text, Textarea, TextInput, toast, } from './ui'; import { + audioContainerForCodec, TRANSCODE_COPY, + TRANSCODE_REMOVE_VIDEO, transcodeAudioCodecOptions, + transcodeAudioContainerOptions, transcodeContainerOptions, transcodeVideoCodecOptions, } from './Vars'; @@ -41,10 +44,25 @@ export function preferredTarget(preferences, options) { return TRANSCODE_COPY; } -/** The container of a video file, when it is one ffmpeg can write for us. */ +/** The container of a video (or audio) file, when it is one ffmpeg can write for us. */ export function currentContainer(video) { const suffix = String(video?.video_path || '').split('.').pop().toLowerCase(); - return transcodeContainerOptions.some(i => i.value === suffix) ? suffix : null; + const known = [...transcodeContainerOptions, ...transcodeAudioContainerOptions]; + return known.some(i => i.value === suffix) ? suffix : null; +} + +/** + * The container to offer for the chosen codecs: audio-only output gets the natural container of + * the audio codec that will be in it (the target, or the source's when copied); output with video + * keeps the file's own container when possible. + */ +export function containerFor({audioOutput, audioCodec, video}) { + if (audioOutput) { + const codec = audioCodec !== TRANSCODE_COPY ? audioCodec : currentCodecs(video).audio[0]; + return audioContainerForCodec(codec); + } + const current = currentContainer(video); + return transcodeContainerOptions.some(i => i.value === current) ? current : 'mp4'; } const statusKind = (status) => { @@ -175,7 +193,10 @@ export function JobProgress({job, onCancel}) { * * The selects start at the user's preferred codecs from the Videos settings. */ -export function TranscodeModal({open, onClose, fileGroupId, video, job, jobId, onQueued, onCancelJob}) { +export function TranscodeModal({ + open, onClose, fileGroupId, video, audioOnly = false, job, jobId, onQueued, + onCancelJob, + }) { const wrolModeEnabled = useWROLMode(); const [videoCodec, setVideoCodec] = useState(TRANSCODE_COPY); const [audioCodec, setAudioCodec] = useState(TRANSCODE_COPY); @@ -184,24 +205,34 @@ export function TranscodeModal({open, onClose, fileGroupId, video, job, jobId, o const [submitting, setSubmitting] = useState(false); const current = currentCodecs(video); + const removeVideo = videoCodec === TRANSCODE_REMOVE_VIDEO; + // An audio file has no video stream to keep or remove; removing the video makes an audio file. + const audioOutput = audioOnly || removeVideo; useEffect(() => { if (!open) { return; } setDefaultsLoaded(false); - setContainer(currentContainer(video) || 'mp4'); let stale = false; const load = async () => { + let nextVideo = TRANSCODE_COPY; + let nextAudio = TRANSCODE_COPY; try { const defaults = await fetchVideoDownloadDefaults(); if (stale) return; - setVideoCodec(preferredTarget(defaults?.video_codecs, transcodeVideoCodecOptions)); - setAudioCodec(preferredTarget(defaults?.audio_codecs, transcodeAudioCodecOptions)); + nextVideo = audioOnly ? TRANSCODE_COPY + : preferredTarget(defaults?.video_codecs, transcodeVideoCodecOptions); + nextAudio = preferredTarget(defaults?.audio_codecs, transcodeAudioCodecOptions); } catch (e) { console.error(e); } finally { - if (!stale) setDefaultsLoaded(true); + if (!stale) { + setVideoCodec(nextVideo); + setAudioCodec(nextAudio); + setContainer(containerFor({audioOutput: audioOnly, audioCodec: nextAudio, video})); + setDefaultsLoaded(true); + } } }; load(); @@ -210,8 +241,20 @@ export function TranscodeModal({open, onClose, fileGroupId, video, job, jobId, o }; }, [open]); - const remuxOnly = videoCodec === TRANSCODE_COPY && audioCodec === TRANSCODE_COPY; + const changeVideoCodec = (value) => { + const next = value || TRANSCODE_COPY; + setVideoCodec(next); + setContainer(containerFor({audioOutput: audioOnly || next === TRANSCODE_REMOVE_VIDEO, audioCodec, video})); + }; + const changeAudioCodec = (value) => { + const next = value || TRANSCODE_COPY; + setAudioCodec(next); + setContainer(containerFor({audioOutput, audioCodec: next, video})); + }; + + const remuxOnly = !removeVideo && videoCodec === TRANSCODE_COPY && audioCodec === TRANSCODE_COPY; const sameContainer = container === currentContainer(video); + const containerOptions = audioOutput ? transcodeAudioContainerOptions : transcodeContainerOptions; const handleStart = async () => { setSubmitting(true); @@ -234,44 +277,54 @@ export function TranscodeModal({open, onClose, fileGroupId, video, job, jobId, o // A queued or finished Job replaces the form until the parent forgets it. const showJob = !!jobId; + let hint = null; + if (defaultsLoaded && removeVideo) { + hint = `The video stream is removed; the result is an audio file (.${container}). ` + + (audioCodec === TRANSCODE_COPY ? 'The audio is copied without re-encoding.' : `The audio is converted to ${audioCodec}.`); + } else if (defaultsLoaded && remuxOnly) { + hint = sameContainer + ? 'Both streams are kept: the file is rewritten as-is with fast start (moves the index to the front for quicker playback start).' + : `Both streams are kept: only the container changes to ${container} (a quick remux, no quality loss).`; + } + const buttonLabel = removeVideo ? 'Extract Audio' : remuxOnly ? 'Remux' : 'Transcode'; + return - Transcode Video + {audioOnly ? 'Transcode Audio' : 'Transcode Video'} - Current codecs: video {describe(current.video)}, audio {describe(current.audio)}. + {audioOnly + ? `Current codec: audio ${describe(current.audio)}. ` + : `Current codecs: video ${describe(current.video)}, audio ${describe(current.audio)}. `} Streams set to Keep are copied without re-encoding. Transcoding is slow and slightly reduces quality; the original file is replaced when it finishes. {!showJob && <> - setAudioCodec(value || TRANSCODE_COPY)} + onChange={changeAudioCodec} disabled={!defaultsLoaded} allowDeselect={false} />