MoviePy Audio doesn't exist on iPhone - python

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

Related

ImageMagick problem on Windows 10 with Windows 2 error code

I want to make a python script to create a video with moviepy but I have some problems with windows. I tried installing ImageMagick but can not find a convert.exe I do not know why it is. But I am already stuck.
my code:
import moviepy.editor as mp
import os
print("Start")
def create_vertical_video(image_file, music_file, quote):
# Open image and resize to 9:16 aspect ratio (vertical video)
if not os.path.isfile(image_file):
raise ValueError(f"{image_file} is not a valid file path")
image = mp.ImageClip(image_file).resize(height=1080)
# Open music file and set to fit the duration of the image
if not os.path.isfile(music_file):
raise ValueError(f"{music_file} is not a valid file path")
music = mp.AudioFileClip(music_file)
music = music.set_duration(image.duration)
# Split quote into multiple lines if necessary
MAX_CHARS_PER_LINE = 25
lines = []
current_line = ""
for word in quote.split():
if len(current_line) + len(word) <= MAX_CHARS_PER_LINE:
current_line += word + " "
else:
lines.append(current_line.strip())
current_line = word + " "
lines.append(current_line.strip())
# Create text clip for each line
text_clips = [mp.TextClip(line, fontsize=24, color='white', bg_color='black')
.set_pos('center').set_duration(image.duration)
for line in lines]
# Overlay text clips on image
text_overlay = mp.concatenate_videoclips(text_clips, method='compose')
final_clip = mp.CompositeVideoClip([image, text_overlay])
# Add music to final clip
final_clip = final_clip.set_audio(music)
# Save video
final_clip.write_videofile("vertical_video.mp4", fps=24)
create_vertical_video("nature.jpg","music.mp3","Don't be pushed around by the fears in your mind. Be led by the dreams in your heart.")
my error:
[WinError 2] The system cannot find the file specified.
.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect`
In ImageMagick v7, the convert.exe program is replaced by magick.exe.
So if your Python script is looking for convert.exe, you must click "Install legacy commands" when installing ImageMagick.

Errno 5 Input/output error when closing console

I have a video converter which is converting audio and video files. Everything works but if I close my terminal from my server the audio file convert doesnt work anymore. I use PyTube for converting and moviepy for converting the mp4 from pytube into mp3. (I think the problem has something to do with moviepy bc. before I didnt have it.)
This is my code for converting audio:
if format == "3":
yt = YouTube(videolink)
downloads = MEDIA_ROOT + "/videos/"
ys = yt.streams.filter(file_extension='mp4').first().download(downloads)
base, ext = os.path.splitext(ys)
basename = os.path.basename(base + uuid + '.mp3')
videoclip = VideoFileClip(ys)
audioclip = videoclip.audio
audioclip.write_audiofile(base + uuid + ".mp3")
audioclip.close()
videoclip.close()
maybe something with the os code is wrong.
But I cant figure out why it works if I leave the console open.
Im thankful for every help I get.
So I found the solution, for everyone who faces the same problem. You have to disable the console output in moviepy. You can do the with the logger parameter in the "write" function. Then the error should disappear.

Combining mp4 with wav in python

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

Video is much faster than audio moviepy

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])

LibVlc can't be used in Qt Frame in Python

So I'm trying to embed a few VLC media players into their separate QTFrames. Everything is done in python. However for some reason the official example doesn't want to work (found here)
My code looks like this:
for files in tutorialVideoFiles:
#frame = QFrame()
mediaplayer = vlcInstance.media_player_new() #create media player
media = vlcInstance.media_new(mainTutorialDirectory + "/" + self.tutorialName + "/" + files) #load video file
mediaplayer.set_media(media) #assign video file
media.parse()
self.mediaplayers.append(mediaplayer)
#mediaplayer.set_hwnd(frame.winId())
If I uncomment the lines, I get the following error:
"directdraw vout display error: Win32VoutCreateWindow RegisterClass FAILED (err=1410)"
I'm using Windows 64 bit and Python 3.6

Categories

Resources