a simple code like this is creating a corrupted audio file for some reasons:
from moviepy import *
clip = VideoFileClip("cut.mp4")
audio = clip.audio
audio.to_audiofile('temp-audio.mp3')
Expected Behavior
Audio should be the same as the audio in the video
Actual Behavior
audio is corrupted in the end (repeats the end segment a few times like a broken record)
Steps to Reproduce the Problem:
Run the code above on this video with latest moviepy version (don't make fun it's just a trial lol) and you will get this audio which is corrupted (I compressed it in a zip):
here
Specifications
Python Version: Python 3.9.9
Moviepy Version: 1.0.3
may this will help you
from moviepy import *
clip = VideoFileClip("cut.mp4").subclip(0,1)
#subclip mean video duration its from the place to start to the end
audio = clip.audio
audio.to_audiofile('temp-audio.mp3')
I hope I've been helpful
Related
I am using moviepy to make simple videos that require adding background music to a video. I have built essentially the whole application but I ran into a problem when showing people the videos. I tried emailing myself the video and it didn't have any sound, then I tried to play the video with the built in mac video player, quicktime, and it didn't have any sound, however, when I played the video in vs code's video player it worked perfectly, I also used the file path to view the video in chrome, and that worked too. I suspect that it has something to do with the encoding but I am a novice when it comes to video and audio encoding. This is what my code looks like:
from moviepy.editor import *
background_video = VideoFileClip("./background.mp4")
background_music_audio = AudioFileClip("./music.wav")
comp = background_video.set_audio(background_music_audio)
comp.write_videofile("./test.mp4")
I tried to make a video with background music but it had no sound
I am trying to make a video with one image and one auido file using the following code:
from moviepy.editor import *
audio = AudioFileClip("/users/qingzhou/downloads/Taylor.wav")
image = ImageClip("/users/qingzhou/downloads/shuangyu.png").set_duration(audio.duration)
clip = image.set_audio(audio)
clip.write_videofile("/users/qingzhou/downloads/rock.mp4",fps=30)
After running it, I got the following notifications:
/Users/qingzhou/venv/Python3.9new/bin/python /Users/qingzhou/IdeaProjects/programFlow/Factory.py
Moviepy - Building video /users/qingzhou/downloads/rock.mp4.
MoviePy - Writing audio in rockTEMP_MPY_wvf_snd.mp3
t: 0%| | 0/1972 [00:00<?, ?it/s, now=None]MoviePy - Done.
Moviepy - Writing video /users/qingzhou/downloads/rock.mp4
Moviepy - Done !
Moviepy - video ready /users/qingzhou/downloads/rock.mp4
Process finished with exit code 0
The video generated has no sound at all after trying using different forms of auido file (.mp3 and .wav).
I cannot find any useful information online to address it.
I hope to resolve this issue because I am building a video processing bot to make a large number of videos with the same foramt using different content.
The code is working properly. Please pay attention to the audio file.
I'm programming a youtube video downloader that allows you to download youtube videos in different resolutions and refresh rates. However, when I try to list the available resolutions and fps, I weirdly only get lower quality options. In my code example I am using the YouTube Rewind 2019 video that is used for an example in the docs. Here is my code:
from pytube import YouTube
youtube = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo')
for stream in youtube.streams.filter(progressive=True):
print("resolution: " + stream.resolution)
print("fps: " + str(stream.fps))
print("----------------------")
and here is my output:
/mnt/d/youtube-downloader$ python3 main.py
resolution: 360p
fps: 24
----------------------
resolution: 720p
fps: 24
----------------------
As you can see the only qualities I get are 720p and 360p although the youtube video used can scale all the way up to 1080p. Is there a different function I should be aware of.
I've also found a closed GitHub issue which seems to be similar to mine but doesn't have an answer I can work with which is why I've posted this issue here.
Thanks for your time.
I've just gone through the same issue. You can use something like this
from pytube import YouTube
import os
youtube = YouTube('https://youtu.be/kdmgpMfnjdU')
video = youtube.streams.filter(res="480p").first().download()
os.rename(video,"video_best.mp4")
Notice that 480p is the best available quality in that video.
Note: if you keep getting a video without audio, refer to this answer on how to tackle it.
You can download it in lowest or highest quality by--
highest-
youtube.streams.get_highest_resolution.download()
lowest-
youtube.streams.get_lowest_resolution.download()
To list all available resolutions in a video, you can use the adaptive filter.
video = youtube("URL")
resolution = video.streams.filter(adaptive=true)
progressive=true allows you to view only progressive downloads. A progressive download is a legacy stream containing the audio and video in a single file, available for resolutions 720 and below.
Because "progressive=True" in
for stream in youtube.streams.filter(progressive=True)
The highest quality you can get is 720p and below when "progressive=True", read this section on pytube docs it explains it all.
If u want the highest avaliable quality with the best audio u can do this by downloading the two files then merging them using ffmpeg.
I've been trying to download certain portions of YouTube video. The long way is to download the video then extract the certain portion of it. But when it comes to a big dataset with long videos, the method is costly.
The code works. But downloads the entire video instead of the certain portion.
from pytube import YouTube
YouTube('https://www.youtube.com/embed/yf8Ub90OWFM?start=15&end=25').streams.first().download()
Expected result: 10 second video in the time interval of 15-25 seconds.
According to issue Support to download partial videos of PyTube it is not currently possible.
Therefore you might use one of Python videos post processing library, eg moviepy:
from moviepy.editor import *
video = VideoFileClip("myHolidays.mp4").subclip(50,60)
video.write_videofile("myHolidays_edited.webm",fps=25)
Or get the command-line ffmpeg tool:
ffmpeg -ss (start time) -i (direct video link) -t (duration needed) -c:v copy -c:a copy (destination file)
I am new to python (2.7) and opencv (3.0) (and video streaming/writing in general) so forgive this.
I am using the logitech c920 as my webcam and it can stream video compressed in h264 format so I am trying to write a simple app that sets 4 properties of the VideoCapture instance (fourcc to h264; width to 1920; height to 1080; and fps to 30), and then records a video to the directory one level up named test.mp4 and shows the recording on my screen. Here is code:
import sys
import cv2 as cv
cap = cv.VideoCapture(0)
fourcc = cv.VideoWriter_fourcc('H','2','6','4')
cap.set(6, fourcc)
cap.set(3,1920)
cap.set(4,1080)
cap.set(5, 30)
vid = cv.VideoWriter('../test.mp4', fourcc, 20.0, (640,480))
print vid.isOpened() #returns false :(
while (cap.isOpened()):
ret, frame = cap.read()
if (ret == True):
#gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
vid.write(frame)
cv.imshow('window', frame)
if (cv.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
vid.release()
cv.destroyWindow('window')
cv.imshow('window',frame) works just fine, and the properties are all set; however, vid.isOpened() return false so clearly I have done something wrong in the above. If I pass -1 for the fourcc, I am allowed to pick from a list of codecs and i420 is available and says (for logitech cameras) and vid.isOpened() returns true if I change the file extension from mp4 to avi (I guess that means i420 cannot be stored as .avi ?), however, test.avi is always huge and seemingly raw, 100MB for a few second test video and will not open.
Any help with this would be great, thanks a lot
So the workaround I found so far is not that efficient but it works for me on Ubuntu 16.04. After writing the video regularly, I convert it again using H264 coder and the final video size is much smaller than the one I got from opencv alone.
Here is the workaround:
import os # We will use it to access the terminal
# Write the video normally using mp4v and make the extension to be '.mp4'
# Note: the output using "mp4v" coder may be efficient for you, if so, you do not need to add the command below
cv2.VideoWriter('Video.mp4',cv2.VideoWriter_fourcc(*"mp4v"), NewFPS, (width,height))
# When your video is ready, just run the following command
# You can actually just write the command below in your terminal
os.system("ffmpeg -i Video.mp4 -vcodec libx264 Video2.mp4")
Keep in mind that OpenCV automatically decompresses the incoming H264 stream from the webcam. You cannot pass on the compressed stream. You must recompress.
Since the codec ID of -1 displays a prompt asking you for chosing a codec, I assume you are on Windows. On Windows, OpenCV uses ffmpeg or "Video for Windows" backends. Unfortunately, it seems you cannot tell when OpenCV uses which backend. The dialog asking to select an encoder looks like the VfW dialog. There are no sane h264 VfW encoders, you probably did not install a hackish one like x264vfw, so you cannot choose h264 here.
The AVI container does not support b-Frames. H264 uses b-frames by default. You cannot change these encoder settings from within OpenCV. As a result, you should not choose AVI for H264 encoded video.
In order to save to another container, ffmpeg should be used. I can only guess OpenCV cooses backends wisely and based on container. You should be able to encode h264 into MKV or MP4. Bear in mind that there is some kind of "FOURCC to ffmpeg-Encoder-ID" translation takes place.
If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg.
You'll have to compile OpenCV manually to get support for H264 encoding.
Related issue on Github: https://github.com/skvark/opencv-python/issues/100#issuecomment-394159998