mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 11:56:18 -05:00
videos: transcode can remove the audio stream (silent video) (#737)
A video's audio stream can be dropped the same way its video stream could already be: a new REMOVE_AUDIO audio target maps only the video stream with -an, needs a video container, and is refused for audio files. Output verification insists the result has no audio stream. Removing both streams is refused by the validator, the API, resolve_output, and the modal: choosing Remove for one stream resets the other to Keep and withdraws the other's Remove option. Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
63ddad69e8
commit
15d3792b0e
7 changed files with 291 additions and 30 deletions
|
|
@ -193,8 +193,8 @@ class VideoUpdateRequest:
|
|||
|
||||
@dataclass
|
||||
class VideoTranscodeRequest:
|
||||
video_codec: Optional[str] = None # A key of TRANSCODE_VIDEO_TARGETS, or None to copy the stream.
|
||||
audio_codec: Optional[str] = None # A key of TRANSCODE_AUDIO_TARGETS, or None to copy the stream.
|
||||
video_codec: Optional[str] = None # A key of TRANSCODE_VIDEO_TARGETS, REMOVE_VIDEO, or None to copy the stream.
|
||||
audio_codec: Optional[str] = None # A key of TRANSCODE_AUDIO_TARGETS, REMOVE_AUDIO, or None to copy the stream.
|
||||
container: str = 'mp4'
|
||||
fragmented: bool = False # Fragmented mp4: quick start/seek for very long videos (mp4/m4a only).
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
"""Audio-only transcodes: removing the video stream from a video, and transcoding audio files."""
|
||||
"""Audio-only transcodes: removing the video stream from a video, and transcoding audio files.
|
||||
Also the mirror: removing the audio stream from a video (`REMOVE_AUDIO`)."""
|
||||
import json
|
||||
import pathlib
|
||||
import shutil
|
||||
|
|
@ -7,7 +8,7 @@ from unittest import mock
|
|||
import pytest
|
||||
|
||||
from modules.videos.transcode import transcode_video_file, validate_transcode_request, verify_transcode_output, \
|
||||
REMOVE_VIDEO, TRANSCODE_AUDIO_CONTAINERS, default_audio_container, transcode_video_job
|
||||
REMOVE_VIDEO, REMOVE_AUDIO, TRANSCODE_AUDIO_CONTAINERS, default_audio_container, transcode_video_job
|
||||
from wrolpi import jobs
|
||||
from wrolpi.cmd import CommandResult
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
|
|
@ -263,3 +264,160 @@ async def test_transcode_api_remove_video_needs_audio_container(async_client, te
|
|||
'container': 'm4a'}))
|
||||
assert response.status == 200, response.json
|
||||
jobs.cancel_job(response.json['job_id'])
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ remove audio
|
||||
|
||||
def test_validate_remove_audio():
|
||||
"""Removing the audio needs a video container; both streams cannot be removed."""
|
||||
validate_transcode_request(None, REMOVE_AUDIO, 'mp4')
|
||||
validate_transcode_request('h264', REMOVE_AUDIO, 'mkv')
|
||||
validate_transcode_request(None, REMOVE_AUDIO, 'mp4', fragmented=True)
|
||||
with pytest.raises(ValueError, match='video container'):
|
||||
validate_transcode_request(None, REMOVE_AUDIO, 'm4a')
|
||||
with pytest.raises(ValueError, match='video container'):
|
||||
validate_transcode_request(None, REMOVE_AUDIO, 'mp3')
|
||||
with pytest.raises(ValueError, match='both'):
|
||||
validate_transcode_request(REMOVE_VIDEO, REMOVE_AUDIO, 'mp4')
|
||||
with pytest.raises(ValueError, match='both'):
|
||||
validate_transcode_request(REMOVE_VIDEO, REMOVE_AUDIO, 'm4a')
|
||||
|
||||
|
||||
def test_verify_remove_audio_output():
|
||||
"""The output must have the video stream and must not have an audio stream."""
|
||||
source = make_probe('vp9', 'opus', 100.0)
|
||||
verify_transcode_output(source, make_probe('h264', None, 100.0), 'h264', REMOVE_AUDIO, 'mp4')
|
||||
verify_transcode_output(source, make_probe('vp9', None, 100.0), None, REMOVE_AUDIO, 'mp4')
|
||||
with pytest.raises(RuntimeError, match='still has an audio stream'):
|
||||
verify_transcode_output(source, make_probe('vp9', 'opus', 100.0), None, REMOVE_AUDIO, 'mp4')
|
||||
with pytest.raises(RuntimeError, match='no video stream'):
|
||||
verify_transcode_output(source, make_probe(None, None, 100.0), None, REMOVE_AUDIO, 'mp4')
|
||||
# A silent source can have its (absent) audio removed; the result is unchanged.
|
||||
verify_transcode_output(make_probe('vp9', None, 100.0), make_probe('vp9', None, 100.0), None, REMOVE_AUDIO, 'mp4')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_audio(test_directory, async_client):
|
||||
"""Removing the audio: the video is mapped, `-an` drops the audio, no `-c:a`, the output keeps a
|
||||
video container and the video stream is copied unless a target is given."""
|
||||
video_path = test_directory / 'movie.webm'
|
||||
video_path.write_bytes(b'fake video data')
|
||||
probes = FakeProbes(make_probe('vp9', 'opus', 10.0, 'matroska,webm'), make_probe('vp9', None, 10.0, 'matroska,webm'))
|
||||
|
||||
with mock.patch('modules.videos.transcode.probe_for_verify', side_effect=probes), \
|
||||
mock.patch('modules.videos.transcode.run_command', side_effect=async_fake_ffmpeg) as mock_run:
|
||||
result = await transcode_video_file(video_path, target_vcodec=None, target_acodec=REMOVE_AUDIO,
|
||||
container='mkv')
|
||||
|
||||
assert result == test_directory / 'movie.mkv' and result.is_file()
|
||||
assert not video_path.exists()
|
||||
cmd = mock_run.call_args_list[0][0][0]
|
||||
assert '-an' in cmd
|
||||
assert '-vn' not in cmd
|
||||
assert '0:a:0?' not in cmd and '0:a:0' not in cmd
|
||||
assert cmd[cmd.index('-map') + 1] == '0:V:0'
|
||||
assert '-c:a' not in cmd
|
||||
assert cmd[cmd.index('-c:v') + 1] == 'copy'
|
||||
assert str(cmd[-1]).endswith('movie.transcode.mkv')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_audio_with_video_target(test_directory, async_client):
|
||||
video_path = test_directory / 'movie.webm'
|
||||
video_path.write_bytes(b'fake video data')
|
||||
probes = FakeProbes(make_probe('vp9', 'opus', 10.0, 'matroska,webm'), make_probe('h264', None, 10.0))
|
||||
|
||||
with mock.patch('modules.videos.transcode.probe_for_verify', side_effect=probes), \
|
||||
mock.patch('modules.videos.transcode.run_command', side_effect=async_fake_ffmpeg) as mock_run:
|
||||
result = await transcode_video_file(video_path, target_vcodec='h264', target_acodec=REMOVE_AUDIO,
|
||||
container='mp4')
|
||||
|
||||
assert result == test_directory / 'movie.mp4'
|
||||
cmd = mock_run.call_args_list[0][0][0]
|
||||
assert '-an' in cmd
|
||||
assert cmd[cmd.index('-c:v') + 1] == 'libx264'
|
||||
assert '-c:a' not in cmd
|
||||
assert '+faststart' in cmd
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_audio_from_audio_source_is_refused(test_directory, async_client):
|
||||
"""An audio file with its audio removed is nothing; ffmpeg is never run."""
|
||||
audio_path = test_directory / 'song.mp3'
|
||||
audio_path.write_bytes(b'fake audio data')
|
||||
probes = FakeProbes(make_probe(None, 'mp3', 10.0, 'mp3'), make_probe(None, 'mp3', 10.0, 'mp3'))
|
||||
|
||||
with mock.patch('modules.videos.transcode.probe_for_verify', side_effect=probes), \
|
||||
mock.patch('modules.videos.transcode.run_command', side_effect=async_fake_ffmpeg) as mock_run:
|
||||
with pytest.raises(RuntimeError, match='no video stream'):
|
||||
await transcode_video_file(audio_path, target_acodec=REMOVE_AUDIO, container='mp4')
|
||||
mock_run.assert_not_called()
|
||||
assert audio_path.is_file()
|
||||
|
||||
|
||||
def test_resolve_output_refuses_removing_both():
|
||||
"""Even without `validate_transcode_request`, ffmpeg is never asked for nothing."""
|
||||
from modules.videos.transcode import resolve_output
|
||||
with pytest.raises(RuntimeError, match='both'):
|
||||
resolve_output(make_probe('h264', 'aac'), REMOVE_VIDEO, REMOVE_AUDIO, 'mp4')
|
||||
with pytest.raises(RuntimeError, match='both'):
|
||||
resolve_output(make_probe(None, 'aac'), REMOVE_VIDEO, REMOVE_AUDIO, 'mp3')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_audio_job(test_session, test_directory, async_client, video_factory):
|
||||
"""The Job removes the audio in place: same FileGroup, still a video."""
|
||||
from modules.videos.models import Video
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
video = video_factory()
|
||||
video.file_group.length = 5
|
||||
test_session.commit()
|
||||
file_group_id = video.file_group_id
|
||||
video_path = video.video_path
|
||||
probes = FakeProbes(make_probe('h264', 'aac', 5.3), make_probe('h264', None, 5.3))
|
||||
|
||||
async def fake_run_command(cmd, stdout_callback=None, **kwargs):
|
||||
if cmd[-1] != '-':
|
||||
shutil.copy(PROJECT_DIR / 'test/big_buck_bunny_720p_1mb.mp4', cmd[-1])
|
||||
return CommandResult(return_code=0, cancelled=False, stdout=b'', stderr=b'', elapsed=1)
|
||||
|
||||
with mock.patch('modules.videos.transcode.probe_for_verify', side_effect=probes), \
|
||||
mock.patch('wrolpi.cmd.run_command', side_effect=fake_run_command):
|
||||
job_id = transcode_video_job.enqueue(file_group_id=file_group_id, video_codec=None,
|
||||
audio_codec=REMOVE_AUDIO, container='mp4')
|
||||
record = await jobs.wait_for_job(job_id)
|
||||
|
||||
assert record['status'] == jobs.COMPLETE, record
|
||||
assert video_path.with_suffix('.mp4').is_file()
|
||||
|
||||
test_session.expire_all()
|
||||
video = Video.find_by_file_group_id(test_session, file_group_id)
|
||||
assert video.file_group.mimetype.startswith('video/')
|
||||
assert test_session.query(FileGroup).count() == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_transcode_api_remove_audio(async_client, test_session, video_factory, audio_factory):
|
||||
"""Remove-audio is queued for a video with a video container; refused on an audio file, with an
|
||||
audio container, and together with remove-video."""
|
||||
video = video_factory()
|
||||
audio = audio_factory()
|
||||
test_session.commit()
|
||||
|
||||
request, response = await async_client.post(f'/api/videos/{video.file_group_id}/transcode',
|
||||
content=json.dumps({'audio_codec': REMOVE_AUDIO, 'container': 'mp4'}))
|
||||
assert response.status == 200, response.json
|
||||
assert 'Remove audio' in jobs.get_job(response.json['job_id'])['description']
|
||||
jobs.cancel_job(response.json['job_id'])
|
||||
|
||||
request, response = await async_client.post(f'/api/videos/{video.file_group_id}/transcode',
|
||||
content=json.dumps({'audio_codec': REMOVE_AUDIO, 'container': 'm4a'}))
|
||||
assert response.status == 400, response.json
|
||||
request, response = await async_client.post(f'/api/videos/{video.file_group_id}/transcode',
|
||||
content=json.dumps({'video_codec': REMOVE_VIDEO,
|
||||
'audio_codec': REMOVE_AUDIO, 'container': 'mp4'}))
|
||||
assert response.status == 400, response.json
|
||||
request, response = await async_client.post(f'/api/videos/{audio.file_group_id}/transcode',
|
||||
content=json.dumps({'audio_codec': REMOVE_AUDIO, 'container': 'mp4'}))
|
||||
assert response.status == 400, response.json
|
||||
|
|
|
|||
|
|
@ -57,6 +57,8 @@ AUDIO_CONTAINER_CODECS = {
|
|||
}
|
||||
# `target_vcodec` value meaning "drop the video stream"; the output is an audio file.
|
||||
REMOVE_VIDEO = 'none'
|
||||
# `target_acodec` value meaning "drop the audio stream"; the output is a silent video.
|
||||
REMOVE_AUDIO = 'none'
|
||||
# ffprobe's `format_name` token for each container (mkv reports "matroska,webm").
|
||||
CONTAINER_FORMAT_NAMES = {'mp4': 'mp4', 'mkv': 'matroska', 'm4a': 'm4a', 'ogg': 'ogg', 'mp3': 'mp3'}
|
||||
# `-movflags +faststart` is an mp4-muxer option; other muxers warn about it.
|
||||
|
|
@ -184,7 +186,8 @@ async def transcode_video_file(video_path: pathlib.Path,
|
|||
raise RuntimeError('ffmpeg was not found')
|
||||
# No target for either stream is a remux: both streams are copied into the (new) container.
|
||||
# `target_vcodec=REMOVE_VIDEO` (or an audio-only source) makes the output an audio file; see
|
||||
# `resolve_output`, which also settles an unsuitable container.
|
||||
# `resolve_output`, which also settles an unsuitable container. `target_acodec=REMOVE_AUDIO`
|
||||
# makes the output a silent video.
|
||||
|
||||
lock = await _acquire_transcode_lock()
|
||||
try:
|
||||
|
|
@ -216,8 +219,9 @@ def verify_transcode_output(source: dict, output: dict, target_vcodec: Optional[
|
|||
complete rendition of the source before the source is replaced by it.
|
||||
|
||||
Checks: the output parses (its index was written), it is in the requested container, it has a
|
||||
video stream (and an audio stream when the source has one), each stream's codec is the target
|
||||
or, when copied, the source's, and the duration matches within a tolerance.
|
||||
video stream (and an audio stream when the source has one, unless the audio was removed), each
|
||||
stream's codec is the target or, when copied, the source's, and the duration matches within a
|
||||
tolerance.
|
||||
|
||||
@raise RuntimeError: describing the first failed check.
|
||||
"""
|
||||
|
|
@ -242,12 +246,17 @@ def verify_transcode_output(source: dict, output: dict, target_vcodec: Optional[
|
|||
else:
|
||||
if not output_video:
|
||||
raise RuntimeError('Transcode output has no video stream')
|
||||
if source_audio and not output_audio:
|
||||
if target_acodec == REMOVE_AUDIO and output_audio:
|
||||
raise RuntimeError(f'Transcode output still has an audio stream ({output_audio[0]})')
|
||||
if source_audio and not output_audio and target_acodec != REMOVE_AUDIO:
|
||||
raise RuntimeError('Transcode output has no audio stream, but the source has one')
|
||||
expected_video = target_vcodec or (source_video[0] if source_video else None)
|
||||
if expected_video and output_video[0] != expected_video:
|
||||
raise RuntimeError(f'Transcode output video codec is {output_video[0]}, expected {expected_video}')
|
||||
expected_audio = target_acodec or (source_audio[0] if source_audio else None)
|
||||
if target_acodec == REMOVE_AUDIO:
|
||||
expected_audio = None
|
||||
else:
|
||||
expected_audio = target_acodec or (source_audio[0] if source_audio else None)
|
||||
if output_audio and expected_audio and output_audio[0] != expected_audio:
|
||||
raise RuntimeError(f'Transcode output audio codec is {output_audio[0]}, expected {expected_audio}')
|
||||
|
||||
|
|
@ -307,8 +316,12 @@ def resolve_output(source_probe: dict, target_vcodec: Optional[str], target_acod
|
|||
source_video = get_stream_codec_names(source_probe, 'video')
|
||||
source_audio = get_stream_codec_names(source_probe, 'audio')
|
||||
|
||||
if target_vcodec == REMOVE_VIDEO and target_acodec == REMOVE_AUDIO:
|
||||
raise RuntimeError('Cannot remove both the video and the audio; nothing would remain')
|
||||
if target_vcodec and target_vcodec != REMOVE_VIDEO and not source_video:
|
||||
raise RuntimeError(f'Cannot transcode video to {target_vcodec}: the file has no video stream')
|
||||
if target_acodec == REMOVE_AUDIO and not source_video:
|
||||
raise RuntimeError('Cannot remove the audio: the file has no video stream, nothing would remain')
|
||||
|
||||
audio_only = target_vcodec == REMOVE_VIDEO or not source_video
|
||||
if not audio_only:
|
||||
|
|
@ -354,16 +367,22 @@ async def _transcode_video_file(video_path: pathlib.Path, target_vcodec: Optiona
|
|||
source_probe = await probe_for_verify(video_path)
|
||||
audio_only, container = resolve_output(source_probe, target_vcodec, target_acodec, container)
|
||||
|
||||
audio_args = TRANSCODE_AUDIO_TARGETS[target_acodec] if target_acodec else ('-c:a', 'copy')
|
||||
if audio_only:
|
||||
# `-vn` also drops embedded cover art; the audio stream is required.
|
||||
map_args = ('-map', '0:a:0', '-vn')
|
||||
video_args = ()
|
||||
audio_args = TRANSCODE_AUDIO_TARGETS[target_acodec] if target_acodec else ('-c:a', 'copy')
|
||||
elif target_acodec == REMOVE_AUDIO:
|
||||
# `-an`: a silent video; only the video stream is mapped.
|
||||
map_args = ('-map', '0:V:0', '-an')
|
||||
video_args = TRANSCODE_VIDEO_TARGETS[target_vcodec] if target_vcodec else ('-c:v', 'copy')
|
||||
audio_args = ()
|
||||
else:
|
||||
# 0:V:0 excludes attached-picture streams (embedded thumbnails); 0:a:0? tolerates a video
|
||||
# with no audio stream.
|
||||
map_args = ('-map', '0:V:0', '-map', '0:a:0?')
|
||||
video_args = TRANSCODE_VIDEO_TARGETS[target_vcodec] if target_vcodec else ('-c:v', 'copy')
|
||||
audio_args = TRANSCODE_AUDIO_TARGETS[target_acodec] if target_acodec else ('-c:a', 'copy')
|
||||
if container in FASTSTART_CONTAINERS and fragmented:
|
||||
mux_args = ('-movflags', FRAGMENTED_MOVFLAGS, '-frag_duration', str(FRAGMENT_DURATION_US))
|
||||
elif container in FASTSTART_CONTAINERS:
|
||||
|
|
@ -438,12 +457,15 @@ async def _transcode_video_file(video_path: pathlib.Path, target_vcodec: Optiona
|
|||
def validate_transcode_request(video_codec: Optional[str], audio_codec: Optional[str], container: str,
|
||||
fragmented: bool = False):
|
||||
"""@raise ValueError: when the request cannot be transcoded. No codec at all is a remux;
|
||||
`video_codec=REMOVE_VIDEO` drops the video stream and needs an audio container; `fragmented`
|
||||
`video_codec=REMOVE_VIDEO` drops the video stream and needs an audio container;
|
||||
`audio_codec=REMOVE_AUDIO` drops the audio stream and needs a video container; `fragmented`
|
||||
needs an mp4 container."""
|
||||
if video_codec and video_codec != REMOVE_VIDEO and video_codec not in TRANSCODE_VIDEO_TARGETS:
|
||||
raise ValueError(f'Cannot transcode video to {video_codec!r}; supported: {sorted(TRANSCODE_VIDEO_TARGETS)}')
|
||||
if audio_codec and audio_codec not in TRANSCODE_AUDIO_TARGETS:
|
||||
if audio_codec and audio_codec != REMOVE_AUDIO and audio_codec not in TRANSCODE_AUDIO_TARGETS:
|
||||
raise ValueError(f'Cannot transcode audio to {audio_codec!r}; supported: {sorted(TRANSCODE_AUDIO_TARGETS)}')
|
||||
if video_codec == REMOVE_VIDEO and audio_codec == REMOVE_AUDIO:
|
||||
raise ValueError('Cannot remove both the video and the audio; nothing would remain')
|
||||
containers = TRANSCODE_CONTAINERS + TRANSCODE_AUDIO_CONTAINERS
|
||||
if container not in containers:
|
||||
raise ValueError(f'Unsupported container {container!r}; supported: {containers}')
|
||||
|
|
@ -451,6 +473,8 @@ def validate_transcode_request(video_codec: Optional[str], audio_codec: Optional
|
|||
raise ValueError(f'A fragmented file must be mp4 (or m4a), not {container}')
|
||||
if video_codec == REMOVE_VIDEO and container not in TRANSCODE_AUDIO_CONTAINERS:
|
||||
raise ValueError(f'Removing the video needs an audio container ({TRANSCODE_AUDIO_CONTAINERS}), not {container}')
|
||||
if audio_codec == REMOVE_AUDIO and container not in TRANSCODE_CONTAINERS:
|
||||
raise ValueError(f'Removing the audio needs a video container ({TRANSCODE_CONTAINERS}), not {container}')
|
||||
if container in TRANSCODE_AUDIO_CONTAINERS:
|
||||
if video_codec and video_codec != REMOVE_VIDEO:
|
||||
raise ValueError(f'A video stream cannot be stored in {container}; remove the video or choose a video container')
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from wrolpi.errors import InvalidOrderBy, InvalidJob
|
|||
from wrolpi.schema import JSONErrorResponse
|
||||
from . import lib
|
||||
from .. import schema
|
||||
from ..transcode import transcode_video_job, validate_transcode_request, REMOVE_VIDEO
|
||||
from ..transcode import transcode_video_job, validate_transcode_request, REMOVE_VIDEO, REMOVE_AUDIO
|
||||
|
||||
video_bp = Blueprint('Video', '/api/videos')
|
||||
|
||||
|
|
@ -88,8 +88,12 @@ async def video_transcode(_: Request, file_group_id: int, body: schema.VideoTran
|
|||
is_audio = (video.file_group.mimetype or '').startswith('audio/')
|
||||
if is_audio and body.video_codec and body.video_codec != REMOVE_VIDEO:
|
||||
raise InvalidJob(f'This is an audio file; it has no video stream to transcode to {body.video_codec}')
|
||||
if is_audio and body.audio_codec == REMOVE_AUDIO:
|
||||
raise InvalidJob('This is an audio file; removing its audio would leave nothing')
|
||||
if body.video_codec == REMOVE_VIDEO and not is_audio:
|
||||
action = 'Extract audio from'
|
||||
elif body.audio_codec == REMOVE_AUDIO:
|
||||
action = 'Remove audio from'
|
||||
elif not body.video_codec and not body.audio_codec:
|
||||
action = 'Remux'
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue