无法写入输出文件

无法写入输出文件

本文介绍了无法写入输出文件#0的标头-moviepy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近安装了 moviepy ,并想用它来编辑视频.

I recently installed moviepy and wanted to edit videos with it.

我看到人们建议使用 moviepy ffmpeg_tools 以获得更好的性能.

I saw that people recommend using the moviepy ffmpeg_tools for better performance.

所以我尝试了这段代码:

So I tried this code:

moviepy.ffmpeg_tools.ffmpeg_extract_audio('before.mp4', 'audio.wav')
moviepy.ffmpeg_tools.ffmpeg_merge_video_audio('after.mp4', 'audio.wav', 'new.mp4')

但是它给出了这个错误:

But it gave this error:

Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

我看到它已成功将音频保存到单独的文件 audio.wav 中,但问题是它不会将视频与音频合并.

I saw that it successfully saved the audio into a separate file audio.wav, but the problem is that it doesn't merge the video with the audio.

我搜索并发现了一些类似的问题,例如 this 一个,但是我不知道如何在我的代码中实现它.

I searched and found some similar questions, like this one, but I can't figure out how to implement this into my code.

推荐答案

ffmpeg_merge_video_audio 的默认编解码器参数为 vcodec ='copy' acodec ='复制" .

The default codec arguments of ffmpeg_merge_video_audio are vcodec='copy' and acodec='copy'.

在很多情况下,无法复制编解码器.

There are many cases when copying the codec is not going to work.

例如:
不支持带有pcm_s16le音频编解码器和H.264视频编解码器的MP4容器.

For example:
MP4 container with pcm_s16le audio codec and H.264 video codec is not supported.

尝试指定音频编解码器和视频编解码器:

Try specifying audio codec and video codec:

from moviepy.video.io import ffmpeg_tools

ffmpeg_tools.ffmpeg_extract_audio('before.mp4', 'audio.wav')
ffmpeg_tools.ffmpeg_merge_video_audio('after.mp4', 'audio.wav', 'new.mp4', vcodec='libx264', acodec='aac')


我尝试使用合成模式重现错误.
我使用 FFmpeg 命令行工具来构建模式(用作输入):


I tried reproducing the error, using synthetic patterns.
I used FFmpeg command line tool, for building the patterns (used as input):

ffmpeg -y -r 25 -f lavfi -i testsrc=size=192x108:rate=30 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -filter_complex amerge -vcodec libx264 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -t 30 before.mp4

ffmpeg -y -r 25 -f lavfi -i testsrc=size=192x108:rate=30 -vcodec libx264 -crf 17 -pix_fmt yuv420p -t 30 after.mp4


使用模式作为输入执行原始代码会给我一个错误:


Executing your original code with the patterns as input gives me an error:


容器当前不支持的

编解码器与您的错误不同,但是几乎相同...


codec not currently supported in container is not the same as your error, but it's almost the same...

我不知道您使用的是什么音频和视频代码.
如果您使用的是AAC和H.264编解码器,并且想要复制编解码器(避免转码),则可以尝试以下代码示例:

I don't know what audio and video codes you are using.
In case you are using AAC and H.264 codecs, and you want to copy the codecs (avoid transcoding), you can try the following code sample:

from moviepy.video.io import ffmpeg_tools

ffmpeg_tools.ffmpeg_extract_audio('before.mp4', 'audio.mp4')  # Extract the audio to mp4 container
ffmpeg_tools.ffmpeg_merge_video_audio('after.mp4', 'audio.mp4', 'new.mp4')

将音频提取为 audio.mp4 会复制AAC音频流(无需将音频转码为PCM编解码器).

Extracting the audio to audio.mp4 copies the AAC audio stream (without transcoding the audio to PCM codec).

这篇关于无法写入输出文件#0的标头-moviepy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:56