gpt4 book ai didi

python - 为什么子进程ffmpeg损坏文件?

转载 作者:行者123 更新时间:2023-12-04 23:18:16 25 4
gpt4 key购买 nike

我有以下代码,它读取视频并将其保存在另一个路径中,问题是保存文件时这是不可重现的吗?

import subprocess
import shlex
from io import BytesIO

file = open("a.mkv", "rb")

with open('a.mkv', 'rb') as fh:
buf = BytesIO(fh.read())


args = shlex.split('ffmpeg -i pipe: -codec copy -f rawvideo pipe:')
proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate(input=buf.getbuffer())
proc.wait()

f = open("a.mp4", "wb")
f.write(out)
f.close()
我需要保留缓冲区,视频大小正确,我该如何解决?

最佳答案

您可以使用 ffmpeg-python

pip install ffmpeg-python
接着:
import subprocess
import shlex
import ffmpeg
from io import BytesIO

file = open("a.mkv", "rb")

with open('a.mkv', 'rb') as fh:
buf = BytesIO(fh.read())

process = (
ffmpeg
.input('pipe:') \
.output('a.mp4') \
.overwrite_output() \
.run_async(pipe_stdin=True) \
)

process.communicate(input=buf.getbuffer())

关于python - 为什么子进程ffmpeg损坏文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72114429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com