有一个基于python生成器的逻辑,它使用PyAv创建一个固定的声音文件和一个等于长度或1帧长的图像。但是,我希望立即执行这个过程,并且不需要另外调用ffmpeg来实现它。
我知道如何分解声音和视频文件,并通过PyAv对它们进行修改,但是由于系统存储空间不足,我无法保存我试图与最终产品转换的文件的视频副本。
那么,如何mux音频包和视频帧?
import av
import numpy as np
def video_generator(config):
for _ in range( int(<audio time by second> * config.fps)):
yield np.zeros((config.height, config.width, 3), dtype = np.uint8)
output = av.open(config.output_file, 'w')
ovstream = output.add_stream('libx264rgb', config.fps)
ovstream.pix_fmt = 'rgb24'
ovstream.width = config.width
ovstream.height = config.height
audio_input = av.open(<input wav>, 'r')
oastream = output.add_stream('mp3')
v_frames = (av.VideoFrame.from_ndarray(img, format='rgb24') for img in video_generator(config))
v_frames = (ovstream.encode(frame) for frame in v_frames)
for img_packet in v_frames:
output.mux(img_packet)
output.close()发布于 2022-05-31 05:55:26
...
for img_packet in v_frames:
output.mux(img_packet)
stream = audio_input.streams.audio[0]
for packet in audio_input.demux((stream,)):
for frame in packet.decode():
a_frames = oastream.encode(frame)
output.mux(a_frames)
output.close()
...此外,如果出现帧丢失,则在output.mux(ovstream.encode()); output.mux(oastream.encode())之前添加output.close()
https://github.com/PyAV-Org/PyAV/issues/597#issuecomment-575349451
https://stackoverflow.com/questions/72415486
复制相似问题