使用ffmpeg创建的视频不会在视频播放器中播放 [英] Video created with ffmpeg won't play in video player
问题描述
我使用Python来创建一个使用ffmpeg的视频。以下代码是我正在使用的...
I'm using Python to create a video using ffmpeg. The following code is what I'm using...
import subprocess as sp
import Image
FFMPEG_BIN = "ffmpeg"
commandWriter = [ FFMPEG_BIN,
'-y',
'-f', 'image2pipe',
'-vcodec','mjpeg',
'-s', '480x360', # size of one frame
'-pix_fmt', 'rgb24',
'-r', '29', # frames per second
'-i', '-',
'-an', # Tells FFMPEG not to expect any audio
'-vcodec', 'mpeg4',
'-qscale', '5',
'-r', '29',
'-b', '250',
'./fire.mp4' ]
pipeWriter = sp.Popen(commandWriter, stdin=sp.PIPE)
fps, duration = 24, 10
for i in range(fps*duration):
im = Image.new("RGB",(480,360),(i%250,1,1))
im.save(pipeWriter.stdin, "JPEG")
pipeWriter.stdin.close()
pipeWriter.wait()
pipeWriter.terminate()
运行上述代码后,我将获得一个数据速率为214 kbps的输出视频。此视频不会播放Windows Media Player。起初我失去了如何让视频播放,所以我把它与另一个我下载的视频进行了比较。我注意到只有真正的差异是在比特率/数据率。我从命令行运行这个命令...
After running the above code, I get an output video with a data rate of 214 kbps. This video won't play in Windows Media Player. At first I was at a loss of how to get the video to play, so I compared it to another video that I downloaded. I noticed the only real difference was in the bit rates/data rates. I ran this command from the command line...
ffmpeg -i fire.mp4 -b:v 250k -bufsize 250k water.mp4
根据我的理解,它需要fire.mp4,只需输出一个修改的位的新视频率。当我在Windows Media Player中打开它时,这个新的输出有效。
which as I understand it takes fire.mp4 and simply outputs a new video with a modified bit rate. This new output works when I open it in Windows Media Player.
我问的问题是如何从Python中直接执行?我已经尝试向commandWriter添加一个-b选项(如图所示),但这不起作用。我还在我的pipeWriter中添加了一个bufsize = 10 ** 8,但这也不起作用。
The question I'm asking is how can I do this straight from Python? I've tried adding a -b option to commandWriter (as shown) but this does not work. I've also added a bufsize = 10**8 in my pipeWriter but that does not work either.
总体而言我正在尝试完成一个视频输入.mp4,在将其加载到内存中时修改每个帧,然后将该帧写入新的文件output.mp4。到目前为止ffmpeg看起来像是最好的工具,因为我无法让OpenCV工作。
Overall what I'm trying to accomplish is taking a video input.mp4, modifying each frame as I load it in memory, and then writing that frame to a new file output.mp4. So far ffmpeg is looking like the best tool 'cause I can't get OpenCV to work at all.
所以如果有人有一个方法来有一个water.mp4输出文件可以在Windows Media Player中运行,而不需要运行额外的命令行代码或更好的方式来完成我的整体任务,我将非常感谢。
So if anyone has a way to have a water.mp4 output file be able to run in Windows Media Player without needing to have that additional command line code run or a better way to complete my overall task, I would much appreciate that.
推荐答案
如果您的问题是如何获取播放的视频,正如您的标题所示,那么我发现删除一些冗余参数工作正常。下面的代码(其中有个人偏好和可读性的其他更改):
If your question is how to get a video that plays, as your title suggests, then I found removing some of the redundant parameters worked fine. Code below (other changes in there for personal preference and readability):
import subprocess
from PIL import Image
FFMPEG_BIN = "ffmpeg"
movie_duration_seconds = 2
movie_fps = 24
ffmpeg_command = [ FFMPEG_BIN,
'-y',
'-f', 'image2pipe',
'-vcodec','mjpeg',
'-s', '480x360', # size of one frame
'-i', 'pipe:0', # take input from stdin
'-an', # Tells FFMPEG not to expect any audio
'-r', str(movie_fps),
#'-pix_fmt', 'yuvj420p', # works fine without this
#'-vcodec', 'mpeg4', # not sure why this is needed
#'-qscale', '5', # works fine without this
#'-b', '250', # not sure why this is needed
'./fire.mp4' ]
ffmpeg_process = subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE)
for i in range(movie_fps * movie_duration_seconds):
# each image is a shade of red calculated as a function of time
im = Image.new("RGB",(480,360),(i%255,1,1))
im.save(ffmpeg_process.stdin, "JPEG")
ffmpeg_process.stdin.flush()
ffmpeg_process.stdin.close()
#ffmpeg_process.wait()
#ffmpeg_process.terminate()
ffmpeg_process.communicate() # not sure if is better than wait but
# terminate seems not required in any case.
但是,我认为问题在于指定比特率。我不知道你修改python时有什么错误,但是添加到args对我来说很好:
However, I think the question is really about specifying bitrate. I'm not sure what error you got when you modified python, but adding this to the args worked fine for me:
'-b:v', '64k',
这篇关于使用ffmpeg创建的视频不会在视频播放器中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!