I'm downloading audio in mp4/web containers using Pytube, but I'm getting "KeyError: 'video_fps'" when trying to convert them to mp3 with moviepy.
Code:
import moviepy.editor as mp
from pytube import YouTube
#download video
youtubeurl = "https://www.youtube.com/watch?v=jYypQueS3ew&index=3&list=PL1nNuwHORUCPajRTocTSCJheaMzbWRu23&t=0s"
yt = YouTube(youtubeurl)
stream = yt.streams.filter(subtype='webm',only_audio=True).last().download("/home/jaws/Desktop/Stuff/Moosepy/temp/", filename="temp_audio")
print stream
print "Done!"
#convert to mp3
clip = mp.VideoFileClip("/home/jaws/Desktop/Stuff/Moosepy/temp/temp_audio.webm").subclip()
clip.audio.write_audiofile("audio.mp3")
print "Done!"
Output:
Traceback (most recent call last):
File "/home/jaws/Desktop/Stuff/Moosepy/test.py", line 13, in
clip = mp.VideoFileClip("/home/jaws/Desktop/Stuff/Moosepy/temp/temp_audio.webm").subclip()
File "/home/jaws/.local/lib/python2.7/site-packages/moviepy/video/io/VideoFileClip.py", line 91, in __init__
fps_source=fps_source)
File "/home/jaws/.local/lib/python2.7/site-packages/moviepy/video/io/ffmpeg_reader.py", line 34, in __init__
self.fps = infos['video_fps']
KeyError: 'video_fps'
Am I missing the codec? It converts mp4 and Webm video files to mp3 fine. It's just these weird audio files in webm/mp4 containers that won't convert. I guess since the FPS is 0, that's throwing up the error? How can I get around this?
`
Moviepy 0.2.3.5
Python 2.7.12
I have the same issue. But im trying to convert mp4(only audio) to mp3
@adhoc92 provide the full path --> clip.audio.write_audiofile("/home/jaws/Desktop/Stuff/Moosepy/temp/audio.mp3")
@adalbertopc I have the same issue, I am trying to convert an audio-only mp4 to mp3. Were you ever able to solve your problem?
Linking #869
@utkarshmttl it's a bit of a hack, but you could go to moviepy/video/io/ffmpeg_reader.py", line 34 and remove the line, or put a bit of logic around it to ignore it if "video_fps" is not a key in infos. I'll try and get it properly fixed for v2.0.
@adhoc92 provide the full path -->
clip.audio.write_audiofile("/home/jaws/Desktop/Stuff/Moosepy/temp/audio.mp3")
have you tried this @utkarshmttl
I got this problem when trying to do this for my personal mp3 yt downloader. I got the solution.
Instead of using VideoFileClip and its VideoFileClip.audio, just use AudioFileClip.
This worked for me. If you have trouble with this, use the full path instead of the relative path shown here.
# mp4 to mp3
clip = AudioFileClip('testfile.mp4')
clip.write_audiofile('testfile.mp3')
clip.close()
Thanks for the help, @sh0tzz. Presumably that鈥檚 because the video didn鈥檛 actually have any video, even though it was a mp4. It鈥檚 definitely something that could be fixed in MoviePy (or at least it could raise a better error), but that鈥檚 a good workaround for now.
Would you be able to share a file (or instructions on how to create one) that demonstrates the error?
The error occurs when the video doesn't contain any visual content, ensure you are uploading a video which is not blank.
The error occurs when the video doesn't contain any visual content, ensure you are uploading a video which is not blank.
some videos doesn`t have visual content but have audio,how can I get the audio with mp3
Just import it as an AudioFileClip and it should work fine.
it`s worked, thank you very much.
Most helpful comment
I got this problem when trying to do this for my personal mp3 yt downloader. I got the solution.
Instead of using
VideoFileClipand itsVideoFileClip.audio, just useAudioFileClip.This worked for me. If you have trouble with this, use the full path instead of the relative path shown here.