4

When I concatenate videos in Moviepy I get no sound in the output file, I try using various parameters but no clue.

This is my code:

import moviepy.editor as mp
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
clip1 = mp.VideoFileClip("V1.mp4")
clip2 = mp.VideoFileClip(dir_path+"\\V2.mp4")
clip3 = mp.VideoFileClip(dir_path+"\\V3.mp4")

output_movie = 'new_movie1.mp4'

final_clip = mp.concatenate_videoclips([clip1,clip2,clip3])

final_clip.write_videofile(output_movie, remove_temp=False, bitrate="5000k",audio=True, audio_codec="aac",codec='mpeg4')

I tried codec="libx264"

Zoe
  • 27,060
  • 21
  • 118
  • 148
adil zakarya
  • 220
  • 1
  • 4
  • 14

2 Answers2

11

I solved this by adding a temporary audio file path. Just change your final line of code to this:

final_clip.write_videofile(output_movie, temp_audiofile='temp-audio.m4a', remove_temp=True, codec="libx264", audio_codec="aac")

You're specifying where MoviePy can store its temp audio file. Also, change the parameter remove_temp to True so the temp file will be cleaned up automatically.

Ryan Loggerythm
  • 2,877
  • 3
  • 31
  • 39
  • 1
    I had an issue when, concatinated video clip was playing without sound on Iphone safari browser bu fine on chrome/firefox... Adding these parameters solved the issue :) – Pavel Durov Mar 27 '20 at 12:55
1

I solved this through a workaround using ffmpeg directly. It uses the temp audio file and video file from moviepy to create a final file. I found that moviepy 1.0.1 does not call ffmpeg with the right arguments to combine the video and audio for mp4 video. This link helped me with ffmpeg:https://superuser.com/questions/277642/how-to-merge-audio-and-video-file-in-ffmpeg

final_clip.write_videofile(moviepy_outfile, temp_audiofile=temp_audiofile,codec="libx264",remove_temp=False,audio_codec='aac')

import subprocess as sp

command = ['ffmpeg',
           '-y', #approve output file overwite
           '-i', str(moviepy_outfile),
           '-i', str(temp_audiofile),
           '-c:v', 'copy',
           '-c:a', 'copy',
           '-shortest', 
           str(output_movie) ]

with open(ffmpeg_log, 'w') as f:
    process = sp.Popen(command, stderr=f)
filipmu
  • 66
  • 3