Reputation: 3
I have a very basic question. I combined an .wav and .mp4 file to output.mp4 using ffmpeg in python:
!ffmpeg -i /content/input.mp4 -i audio.wav -c:v copy -c:a copy output.mp4
Now how do I save the output.mp4 to an .mp4 file on my computer? Thank you very much in advance
Upvotes: 0
Views: 6699
Reputation: 32084
For executing ffmpeg
withing Python, you may use subprocess
:
import subprocess as sp
sp.run('ffmpeg -y -i /content/input.mp4 -i audio.wav -c:v copy -c:a copy output.mp4')
Test the conversion in ffmpeg
command line (in console) before using Python and verify there are no errors.
In most cases you can't keep the audio codec (using -c:a copy
) with .wav
file as input, and store result into .mp4
video file.
I am getting the following error message:
[mp4 @ 000001d150ea6780] Could not find tag for codec pcm_s16le in stream #1, codec not currently supported in container Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
In case of an error, use: -c:a aac
.
Using -c:a copy
works when the codec of the wav
file is ac3 for example (but it's a rare case).
Testing the solution by generating synthetic audio and video input files:
import subprocess as sp
sp.run('ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=30 -vcodec libx264 -pix_fmt yuv420p -t 5 input1.mp4')
sp.run('ffmpeg -y -f lavfi -i sine=frequency=400 -acodec ac3 -ar 22050 -t 5 audio1.wav')
sp.run('ffmpeg -y -i input1.mp4 -i audio1.wav -c:v copy -c:a copy output1.mp4')
The above code is working because I selected ac3
codec for audio1.wav
.
Upvotes: 2