Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output video has no sound

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"

like image 568
adil zakarya Avatar asked Oct 19 '25 02:10

adil zakarya


2 Answers

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.

like image 103
Ryan Loggerythm Avatar answered Oct 21 '25 18:10

Ryan Loggerythm


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)
like image 21
filipmu Avatar answered Oct 21 '25 17:10

filipmu