From 09fa38ae046810f5d36a21ac545ed0e8ed443dfe Mon Sep 17 00:00:00 2001 From: henryruhs Date: Sat, 12 Sep 2026 19:16:33 +0200 Subject: [PATCH] stream uploads for real --- facefusion/apis/asset_helper.py | 7 +++---- facefusion/ffmpeg.py | 24 ++++++++++++------------ tests/test_ffmpeg.py | 28 +++++++++++++++------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/facefusion/apis/asset_helper.py b/facefusion/apis/asset_helper.py index 8a4457d1..1045d8e4 100644 --- a/facefusion/apis/asset_helper.py +++ b/facefusion/apis/asset_helper.py @@ -75,17 +75,16 @@ async def save_asset_files(upload_files : List[UploadFile]) -> List[str]: asset_file_name = uuid.uuid4().hex asset_path = os.path.join(temp_path, asset_file_name + file_extension) - file_content = await upload_file.read() process_manager.start() - if media_type == 'audio' and await asyncio.to_thread(ffmpeg.sanitize_audio, file_content, asset_path, api_security_strategy): + if media_type == 'audio' and await asyncio.to_thread(ffmpeg.sanitize_audio, upload_file.file, asset_path, api_security_strategy): asset_paths.append(asset_path) - if media_type == 'image' and await asyncio.to_thread(ffmpeg.sanitize_image, file_content, asset_path): + if media_type == 'image' and await asyncio.to_thread(ffmpeg.sanitize_image, upload_file.file, asset_path): asset_paths.append(asset_path) - if media_type == 'video' and await asyncio.to_thread(ffmpeg.sanitize_video, file_content, asset_path, api_security_strategy): + if media_type == 'video' and await asyncio.to_thread(ffmpeg.sanitize_video, upload_file.file, asset_path, api_security_strategy): asset_paths.append(asset_path) process_manager.end() diff --git a/facefusion/ffmpeg.py b/facefusion/ffmpeg.py index 1de0ea2e..97890436 100644 --- a/facefusion/ffmpeg.py +++ b/facefusion/ffmpeg.py @@ -3,7 +3,7 @@ import subprocess import tempfile from functools import lru_cache from types import SimpleNamespace -from typing import List, Optional, cast +from typing import BinaryIO, List, Optional, cast import facefusion.choices from facefusion import cli_progress, ffmpeg_builder, ffprobe, logger, process_manager, state_manager, translator, vision @@ -41,10 +41,10 @@ def run_ffmpeg_with_progress(commands : List[Command], progress : SimpleNamespac return process -def run_ffmpeg_with_pipe(commands : List[Command], file_content : Buffer) -> subprocess.Popen[Buffer]: +def run_ffmpeg_with_pipe(commands : List[Command], file : BinaryIO) -> subprocess.Popen[Buffer]: commands = ffmpeg_builder.run(commands) - process = subprocess.Popen(commands, stdin = subprocess.PIPE, stderr = subprocess.PIPE, stdout = subprocess.PIPE) - process.communicate(input = file_content) + process = subprocess.Popen(commands, stdin = file, stderr = subprocess.PIPE, stdout = subprocess.PIPE) + process.communicate() return process @@ -359,7 +359,7 @@ def concat_video(output_path : str, temp_output_paths : List[str]) -> bool: return process.returncode == 0 -def sanitize_audio(file_content : Buffer, asset_path : str, security_strategy : ApiSecurityStrategy) -> bool: +def sanitize_audio(file : BinaryIO, asset_path : str, security_strategy : ApiSecurityStrategy) -> bool: if security_strategy == 'strict': commands = ffmpeg_builder.chain( ffmpeg_builder.set_input('pipe:0'), @@ -367,7 +367,7 @@ def sanitize_audio(file_content : Buffer, asset_path : str, security_strategy : ffmpeg_builder.strip_metadata(), ffmpeg_builder.force_output(asset_path) ) - return run_ffmpeg_with_pipe(commands, file_content).returncode == 0 + return run_ffmpeg_with_pipe(commands, file).returncode == 0 commands = ffmpeg_builder.chain( ffmpeg_builder.set_input('pipe:0'), @@ -375,20 +375,20 @@ def sanitize_audio(file_content : Buffer, asset_path : str, security_strategy : ffmpeg_builder.strip_metadata(), ffmpeg_builder.force_output(asset_path) ) - return run_ffmpeg_with_pipe(commands, file_content).returncode == 0 + return run_ffmpeg_with_pipe(commands, file).returncode == 0 -def sanitize_image(file_content : Buffer, asset_path : str) -> bool: +def sanitize_image(file : BinaryIO, asset_path : str) -> bool: commands = ffmpeg_builder.chain( ffmpeg_builder.set_input('pipe:0'), ffmpeg_builder.deep_copy_image(), ffmpeg_builder.strip_metadata(), ffmpeg_builder.force_output(asset_path) ) - return run_ffmpeg_with_pipe(commands, file_content).returncode == 0 + return run_ffmpeg_with_pipe(commands, file).returncode == 0 -def sanitize_video(file_content : Buffer, asset_path : str, security_strategy : ApiSecurityStrategy) -> bool: +def sanitize_video(file : BinaryIO, asset_path : str, security_strategy : ApiSecurityStrategy) -> bool: if security_strategy == 'strict': available_video_encoders = get_static_available_encoder_set().get('video') commands = ffmpeg_builder.chain( @@ -401,7 +401,7 @@ def sanitize_video(file_content : Buffer, asset_path : str, security_strategy : ffmpeg_builder.strip_metadata(), ffmpeg_builder.force_output(asset_path) ) - return run_ffmpeg_with_pipe(commands, file_content).returncode == 0 + return run_ffmpeg_with_pipe(commands, file).returncode == 0 commands = ffmpeg_builder.chain( ffmpeg_builder.set_input('pipe:0'), @@ -410,7 +410,7 @@ def sanitize_video(file_content : Buffer, asset_path : str, security_strategy : ffmpeg_builder.strip_metadata(), ffmpeg_builder.force_output(asset_path) ) - return run_ffmpeg_with_pipe(commands, file_content).returncode == 0 + return run_ffmpeg_with_pipe(commands, file).returncode == 0 def fix_audio_encoder(video_format : VideoFormat, audio_encoder : AudioEncoder) -> AudioEncoder: diff --git a/tests/test_ffmpeg.py b/tests/test_ffmpeg.py index 1f166889..262747a3 100644 --- a/tests/test_ffmpeg.py +++ b/tests/test_ffmpeg.py @@ -249,43 +249,45 @@ def test_replace_audio() -> None: def test_sanitize_audio() -> None: file_path = get_test_example_file('source.wav') - file_content = open(file_path, 'rb').read() output_paths =\ [ get_test_output_path('test-sanitize-audio-strict.mp3'), get_test_output_path('test-sanitize-audio-moderate.wav') ] - assert sanitize_audio(file_content, output_paths[0], 'strict') is True - assert probe_audio_entries(output_paths[0], [ 'codec_name' ]).get('codec_name') == 'mp3' + with open(file_path, 'rb') as file: + assert sanitize_audio(file, output_paths[0], 'strict') is True + assert probe_audio_entries(output_paths[0], [ 'codec_name' ]).get('codec_name') == 'mp3' - assert sanitize_audio(file_content, output_paths[1], 'moderate') is True - assert probe_audio_entries(output_paths[1], [ 'codec_name' ]).get('codec_name') == 'pcm_s16le' + with open(file_path, 'rb') as file: + assert sanitize_audio(file, output_paths[1], 'moderate') is True + assert probe_audio_entries(output_paths[1], [ 'codec_name' ]).get('codec_name') == 'pcm_s16le' def test_sanitize_image() -> None: file_path = get_test_example_file('source.jpg') - file_content = open(file_path, 'rb').read() output_path = get_test_output_path('test-sanitize-image.jpg') - assert sanitize_image(file_content, output_path) is True - assert is_image(output_path) is True + with open(file_path, 'rb') as file: + assert sanitize_image(file, output_path) is True + assert is_image(output_path) is True def test_sanitize_video() -> None: file_path = get_test_example_file('target-240p-h265.mp4') - file_content = open(file_path, 'rb').read() output_paths =\ [ get_test_output_path('test-sanitize-video-strict.mp4'), get_test_output_path('test-sanitize-video-moderate.mp4') ] - assert sanitize_video(file_content, output_paths[0], 'strict') is True - assert probe_video_entries(output_paths[0], [ 'codec_name' ]).get('codec_name') == 'h264' + with open(file_path, 'rb') as file: + assert sanitize_video(file, output_paths[0], 'strict') is True + assert probe_video_entries(output_paths[0], [ 'codec_name' ]).get('codec_name') == 'h264' - assert sanitize_video(file_content, output_paths[1], 'moderate') is True - assert probe_video_entries(output_paths[1], [ 'codec_name' ]).get('codec_name') == 'hevc' + with open(file_path, 'rb') as file: + assert sanitize_video(file, output_paths[1], 'moderate') is True + assert probe_video_entries(output_paths[1], [ 'codec_name' ]).get('codec_name') == 'hevc' def test_fix_audio_encoder() -> None: