0
from moviepy.editor import *


clip = VideoFileClip('add_audio.mp4')

audio = AudioFileClip('liver_1.wav')

videoclip = clip.set_audio(audio)

clip.write_videofile('does this work.mp4')

Please help, when I run this code the video exports but with no audio. I've tried many different formations of this with slightly different functions and still nothing works. I have yet to find someone with this issue.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
torin
  • 21
  • 5
  • The issue ended up being that Macs won't play certain codecs I think. I uploaded the video to YouTube and there was audio even though when I played it on my desktop it didn't work – torin Jan 28 '23 at 06:16

2 Answers2

0

You are doing clip.write_videofile when you need to do videoclip.write_videofile. You are saving the unmodified original.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thanks for the response, I believe that was a mistake on my end when I cut my code down to put up here. Nonetheless I tried that and it still doesn't work – torin Aug 04 '22 at 12:57
0

You can add encoding as argument in videoclip.write_videofile like this:

videoclip.write_videofile('sample.mp4', codec='libx264', audio_codec='pcm_s32le')

As it's explained in the documentation, you can change video and audio codec when you writing video:

codec

'libx264' (default codec for file extension .mp4) makes well-compressed videos (quality tunable using ‘bitrate’).

'mpeg4' (other codec for extension .mp4) can be an alternative to 'libx264', and produces higher quality videos by default.

audio_codec

Which audio codec should be used? Examples are ‘libmp3lame’ for ‘.mp3’, ‘libvorbis’ for ‘ogg’, ‘libfdk_aac’:’m4a’, ‘pcm_s16le’ for 16-bit wav and ‘pcm_s32le’ for 32-bit wav. Default is ‘libmp3lame’, unless the video extension is ‘ogv’ or ‘webm’, at which case the default is ‘libvorbis’.