I am trying to merge audio with video using MoviePy.
Audio has larger duration than video, so I've changed it to the duration of video.
This is my code:
from moviepy.editor import AudioFileClip, VideoFileClip
video = 'youtube.mp4'
audio = 'voice.mp3'
nName = 'youtube2.mp4'
vClip = VideoFileClip(video)
aClip = AudioFileClip(audio)
print(vClip.duration)
print(aClip.duration)
fAudioClip = aClip.subclip(0.000, vClip.duration)
fVideoClip = vClip.set_audio(fAudioClip)
fVideoClip.write_videofile(nName, codec='libx264',audio_codec='aac')
Output-
*424.96
428.92
Moviepy - Building video youtube2.mp4.
MoviePy - Writing audio in youtube2TEMP_MPY_wvf_snd.mp4
MoviePy - Done.
Moviepy - Writing video youtube2.mp4
Moviepy - Done !
Moviepy - video ready youtube2.mp4*
But still, the video runs much faster than audio & gets over quickly. What can I do to fix this?
I think that you should replace :
fAudioClip = aClip.subclip(0.000, vClip.duration)
fVideoClip = vClip.set_audio(fAudioClip)
by this :
fVideoClip = CompositeAudioClip([vClip.audio, aClip])
Related
I have produced a video using MoviePy and the audio works perfectly fine on PC, but when I try watch it on iPhone it has no audio. I played the uploaded clip on my PC too so it's not the platform where the video is. Also got a friend to listen on iPhone and it also has no audio so not my device. Edit: also tried playing on samsung tablet (android) and it also plays the audio fine.
This is the outputted video files properties:
This is my code:
from moviepy.editor import ImageClip, AudioFileClip, VideoFileClip, CompositeVideoClip
clips = [] # list of clips to be composited
current_duration = 0 # how long the total clip is
bg = VideoFileClip("background.MOV", audio=False) # remove audio from the background
title_audio = AudioFileClip("audio/title.mp3") # title audio
title_clip = ImageClip("screenshots/post.png", duration=title_audio.duration).set_audio(title_audio) # image + audio
clips.append(title_clip.resize(width=bg.w).set_position("center")) # append the resized centred clip
current_duration += title_audio.duration # increase the duration
# loop through clips 1-5 doing the same thing
for comment in range(1, 6):
com_audio = AudioFileClip("audio/voice" + str(comment) + ".mp3")
com_clip = ImageClip("screenshots/comment" + str(comment) + ".png", duration=com_audio.duration).set_audio(com_audio)
clips.append(com_clip.set_start(current_duration).resize(width=bg.w).set_position("center")) # start at current end
current_duration += com_audio.duration
final = CompositeVideoClip([bg.subclip(0, current_duration)] + clips) # composite the clips on top of the background
final.write_videofile("test.mp4", fps=24) # output the file
Based on this, you produced a video file that's (probably) h264/mp3, which isn't a supported format for iPhone - your video file needs to be h264/aac to work on iPhones (and probably any Mac device via Quicktime).
This is also an open issue for moviepy: https://github.com/Zulko/moviepy/issues/1709
You can specify an audio_codec when writing your file to make this work:
final.write_videofile("test.mp4", fps=24, audio_codec='aac') # output the file
I need to get the video dimensions of some videos using there urls on python. Can some one help me?
You can do this using the pafy library:
import pafy
url = "https://www.youtube.com / watch?v = **id**"
video = pafy.new(url)
streams = video.allstreams
stream = streams[7]
value = stream.dimensions
print("Dimension : " + str(value))
Stream is basically the available resolution of the video.
I am trying to combine a .mp4 file with a .wav file. I am rendering my mp4 with cv2 videowriter, and I don't think it has anyway of incorporating audio with it. I have tried moviepy.editor, and ffmpeg. moviepy.editor kept messing up the video file and ffmpeg repeatedly kept giving me an error that it couldn't edit existing files in-place. Combining .mp4 with another audio file type is also fine, but if so it would be nice to also answer how to convert midi files to the file type you answered with. Thanks for the help!
moviepy.editor workflow:
video = mpe.VideoFileClip(mp4_path)
os.system(f"timidity {midi_path} -Ow -o {wav_path)}") # Convert .mid to .wav
video = video.set_audio(mpe.AudioFileClip(wav_path))
video.write_videofile(mp4_path, fps=fps)
ffmpeg workflow:
video = ffmpeg.input(mp4_path)
os.system(f"timidity {midi_path} -Ow -o {wav_path)}") # Convert .mid to .wav
audio = ffmpeg.input(wav_path)
video = ffmpeg.output(video, audio, path, vcodec='copy', acodec='aac', strict='experimental')
ffmpeg.run(video)
I tested both modules and for moviepy I get correct output video with audio even if I use the same name as output. So I don't know what can mess with output.
For ffmpeg I had to use different name for output file to resolve problem with couldn't edit existing files in-place
I had to also use object.video and object.audio to replace audio in output file.
video = ffmpeg.input(video_path).video # get only video channel
audio = ffmpeg.input(audio_path).audio # get only audio channel
My testing code
def test_moviepy(video_path, audio_path, output_path='output-moviepy.mp4', fps=24):
import moviepy.editor as mpe
print('--- moviepy ---')
video = mpe.VideoFileClip(video_path)
video = video.set_audio(mpe.AudioFileClip(audio_path))
video.write_videofile(output_path, fps=fps)
def test_ffmpeg(video_path, audio_path, output_path='output-ffmpeg.mp4', fps=24):
import ffmpeg
print('--- ffmpeg ---')
video = ffmpeg.input(video_path).video # get only video channel
audio = ffmpeg.input(audio_path).audio # get only audio channel
output = ffmpeg.output(video, audio, output_path, vcodec='copy', acodec='aac', strict='experimental')
ffmpeg.run(output)
# --- main ---
video_path = 'movie.mp4'
audio_path = 'sound.wav'
output_path = 'output.mp4'
test_moviepy(video_path, audio_path)#, output_path)
test_ffmpeg(video_path, audio_path)#, output_path)
EDIT:
After installing python module graphviz and program graphviz I could run
ffmpeg.view(output, filename='output-ffmpeg.png')
to get image
I am resizing an mp4 video with this code (moviepy):
video_clip = VideoFileClip(url)
resized = video_clip.resize(width=720)
d = tempfile.mkdtemp()
video_path = os.path.join(d, 'output.mp4')
resized.write_videofile(video_path)
The resized clip's audio works when I play it on my pc, but not on an iPhone. (The original clip's audio does work on my iPhone.)
How can I fix this?
First image: Codec of resized video
Second image: Codec of original video
Here is how I got it working:
resized.write_videofile(video_path, temp_audiofile='temp-audio.m4a', remove_temp=True, codec="libx264", audio_codec="aac")
I created script which one downloading video and sound from Youtube, and after that merging sound and video with ffmpeg, i wondering is another way to make same result but in faster way? Because this script takes about 7 min ~ depends on Video quality and duration. My code bellow:
from pytube import YouTube
import sys
import ffmpeg
import os
class Downloader(YouTube):
def __init__(self, link):
self.link = YouTube(link)
self.hq = []
self.best_video = []
self.best_sound = []
def stream_objects(self):
q = [self.hq.append(x) for x in self.link.streams.all()]
self.best_video.append(str(self.hq[1]).split()[1].split('\"')[1])
self.best_sound.append(str(self.hq[-1]).split()[1].split('\"')[1])
return self.best_video, self.best_sound
def downloady(self):
vid = self.link.streams.get_by_itag(str(self.best_video).strip("['']"))
audio = self.link.streams.get_by_itag(str(self.best_sound).strip("['']"))
self.vid_title = (f"{vid.title}"+".mp4")
vid.download(filename='video')
audio.download(filename='audio')
print('Downloaded, Now Starting Merge \n\n\n\n\n')
print(f'{self.vid_title}'+'\n')
def merge(self):
ffmpeg.output(ffmpeg.input('video.mp4'), ffmpeg.input('audio.webm'), self.vid_title).run()
os.remove('video.mp4')
os.remove('audio.webm')
if __name__=='__main__':
a = Downloader(link = sys.argv[1])
a.stream_objects()
a.downloady()
a.merge()
OKE UPDATE:
Now code looks like that..Second problem is slow downloading mp4 files from YouTube server, i have 10Gb/s internet. Good connection with YT servers, but why so poor downloading ? ? ? :)
from pytube import YouTube
import sys
import ffmpeg
import os
import subprocess
class Downloader(YouTube):
def __init__(self, link):
self.link = YouTube(link)
self.hq = []
def stream_objects(self):
self.best = self.link.streams.filter(file_extension='mp4')
q = [self.hq.append(x) for x in self.best.all()]
self.best_vid_itag = str(self.best.all()[1]).split()[1].split('\"')[1]
self.best_audio_itag = str(self.best.all()[-1]).split()[1].split('\"')[1]
def downloader(self):
vid = self.link.streams.get_by_itag(self.best_vid_itag)
aud = self.link.streams.get_by_itag(self.best_audio_itag)
print('Donwloading Video file...\n')
vid.download(filename='video')
print('Video file downloaded... Now Trying download Audio file..\n')
aud.download(filename='audio')
print('Audio file downloaded... Now Trying to merge audio and video files...\n')
def merger(self):
lin = str(self.link.title).rstrip()
lin2 = (lin+'.mp4')
subprocess.run(f'ffmpeg -i video.mp4 -i audio.mp4 -c copy "{lin2}"', shell=True)
os.remove('video.mp4')
os.remove('audio.mp4')
print('Done....\n')
if __name__=='__main__':
a = Downloader(link = sys.argv[1])
a.stream_objects()
a.downloader()
a.merger()
First of all you download a video file and audio file with different encoding
In your case it is mp4 and webm
You should for example download an mp4 video and m4a audio
Or a webm video and a webm audio
Then it comes to ffmpeg, you should pass a parameter “-c copy”
Example for ffmpeg comand line:
ffmpeg -i myvideo.mp4 -i myaudio.m4a -c copy output.mp4
Here is a link to a python project on github use same technique
https://github.com/pyIDM/pyIDM
Check video.py file
Further explanation:
When you use “-c copy” parameter, ffmpeg will just copy the audio track and merge it with video provided that both audio and video has same codec container, this process take less than 2 seconds
Otherwise it will process every frame in video and every bit in audio then convert them to a desired format, which takes very long time