使用 FFmpeg 和 Python 的 PNG 电影 - rgb24 vs yuv420p
Movie from PNGs using FFmpeg and Python - rgb24 vs yuv420p
我写了一个 Python 小程序来抓取一堆 PNG 并使用 FFmpeg 命令行将它们渲染成电影。 PNG 被读入 [X*Y*3] numpy 数组(忽略 alpha 通道),通过插值添加新帧,数据作为管道输入 FFmpeg 并保存为 mp4。
这些文件在 Windows 上的 VLC 中播放良好,但在 Mac 上的 iMovie 中无法播放。我认为这可能与大多数期望 H264 视频采用 YUV420P 颜色 space 的程序有关,而我的电影则不是。我试过将 ffmpeg 命令 -pix_fmt
从 rgb24
更改为 yuv420p
,但不行。
相关Python代码附在下面。
def init_vars(args):
global log_file, file_names, command, num_int_frames, num_files, silent
file_names = glob('./*.png')
num_files = len(file_names)
if args.log:
log_file = 'bmmp.log'
else:
log_file = os.devnull
silent = args.silent
frames_per_second = args.fps
wanted_movie_length = args.length
movie_file_name = args.name + '.mp4'
num_int_frames = round((frames_per_second * wanted_movie_length - 1) / (num_files - 1) - 1)
if sys.platform == 'win32':
ffmpeg_bin = 'ffmpeg.exe'
else:
ffmpeg_bin = 'ffmpeg'
command = [ffmpeg_bin,
'-y', # (optional) overwrite output file if it exists
'-f', 'rawvideo',
'-vcodec','rawvideo',
'-s', '1280x720', # size of one frame
'-pix_fmt', 'rgb24',
'-r', str(frames_per_second), # frames per second
'-i', '-', # The input comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
movie_file_name]
干杯,
伊拉姆
假设Python中的参数顺序代表构造命令的顺序,那么在'-i', '-', # The input comes from a pipe
之后应该插入一个'-pix_fmt', 'yuv420p',
。不要删除或更改较早的 pix_fmt
,因为它指定了输入属性。
我写了一个 Python 小程序来抓取一堆 PNG 并使用 FFmpeg 命令行将它们渲染成电影。 PNG 被读入 [X*Y*3] numpy 数组(忽略 alpha 通道),通过插值添加新帧,数据作为管道输入 FFmpeg 并保存为 mp4。
这些文件在 Windows 上的 VLC 中播放良好,但在 Mac 上的 iMovie 中无法播放。我认为这可能与大多数期望 H264 视频采用 YUV420P 颜色 space 的程序有关,而我的电影则不是。我试过将 ffmpeg 命令 -pix_fmt
从 rgb24
更改为 yuv420p
,但不行。
相关Python代码附在下面。
def init_vars(args):
global log_file, file_names, command, num_int_frames, num_files, silent
file_names = glob('./*.png')
num_files = len(file_names)
if args.log:
log_file = 'bmmp.log'
else:
log_file = os.devnull
silent = args.silent
frames_per_second = args.fps
wanted_movie_length = args.length
movie_file_name = args.name + '.mp4'
num_int_frames = round((frames_per_second * wanted_movie_length - 1) / (num_files - 1) - 1)
if sys.platform == 'win32':
ffmpeg_bin = 'ffmpeg.exe'
else:
ffmpeg_bin = 'ffmpeg'
command = [ffmpeg_bin,
'-y', # (optional) overwrite output file if it exists
'-f', 'rawvideo',
'-vcodec','rawvideo',
'-s', '1280x720', # size of one frame
'-pix_fmt', 'rgb24',
'-r', str(frames_per_second), # frames per second
'-i', '-', # The input comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
movie_file_name]
干杯, 伊拉姆
假设Python中的参数顺序代表构造命令的顺序,那么在'-i', '-', # The input comes from a pipe
之后应该插入一个'-pix_fmt', 'yuv420p',
。不要删除或更改较早的 pix_fmt
,因为它指定了输入属性。