python - ffmpeg 作为子进程需要太长时间
问题描述
我的程序的一部分需要 FFMPEG 使用 GPU 对文件进行转码。
当我在终端中单独执行 FFMPEG 命令时,没有 Python,大约需要 0.7 秒。同时运行 50 个命令不会影响结果。时间总是在 0.7 秒左右(感谢强大的 GPU 可以处理这么多的请求)。
将此命令放入子进程会将时间增加到 1.7 秒,并在我运行多个实例时最多增加 20 秒,即使此命令应该异步工作。
def run_ffmpeg(filename, gop, representation, output_filename=None):
process_result = subprocess.run(
args=[
'sudo', FFMPEG_PATH,
'-vsync', '0',
'-hwaccel', 'cuvid', # transport decoded file through GPU without touching CPU
'-hwaccel_device', '0',
'-c:v', 'h264_cuvid', # Decoding using GPU
'-i', filename,
# ...
'-c:v', 'hevc_nvenc', # Encoding using GPU
# ...more filters...
output_filename
]
)
log_process_result(process_result)
return output_filename
序列要复杂得多:
def transcode_file(media_id, target_repr, target_nr):
# ... variables definitions
run_ffmpeg(target_segment_path, gop='120', representation=target_repr, output_filename=result)
def prepare_file(media_id, representation_id, file_name):
# ... variables definitions
path = transcode_file(media_id, target_repr, target_nr)
async def download_media_handler(request):
# ... variables definitions
file_info = await loop.run_in_executor(
executor, prepare_file, media_dir_name, representation_id, file_name)
GPU 或 CPU 都没有完全使用,所以情况并非如此。
你知道为什么吗?subprocess
真的要花那么多时间跑吗?
我怎样才能重写它以最小化时间?有更快的替代方案吗?
解决方案
推荐阅读
- git - Git拉取请求冲突
- ada - 如何使用 ASIS 在记录中找到字符串字段的“第一个”和“最后一个”表达式
- spring-boot - Spring boot @Transactional 不回滚
- stata - 如何计算c-index
- java - 无法在运行时使用数据绑定获取视图的宽度
- unit-testing - 调用方法时是否可以完成单元测试?
- azure - VSTS 数据库部署到 Azure - 循环任务组(或类似的东西)
- android - 屏幕旋转后恢复对话框
- ios - ViewController 未从内存中释放
- laravel - 根据 Laravel 项目中两个 ID 的组合从数据透视表中获取结果