How to download a clip of an YouTube video with pytube? - python

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)

Related

Video generated from a picture and one auido file has no sound using moviepy

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.

Moviepy - audio gets corrupted when extracted from video

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

Pytube not all available video qualities are showing | Python 3.9

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.

How to record playing interactive dashboard so as to show to others

In many interactive dashboard posts on internet,writers record how they play the dashboard so as to show to readers like the result at the end of the post:
Bokeh interactive dashboard can not remove lines from plot
How to do it?
I am making a interactive dashboard and would like to record it so as to show it to others.
Thanks
If you are using Mac you could use Quick Time Player (File => New Screen Recording) to record a short video and then use e.g. FFMPEG to convert it to a GIF image. FFMPEG is not standard included on Mac so you will need to install it. The FFMPEG command is:
ffmpeg -i input.mov -r 10 -pix_fmt rgb24 -f gif - | gifsicle --delay=8 > output.gif
Where input.mov is your screen recording and output.gif your final GIF animation.
There are several products out there to do this. When I am recoding Visual Studio work for my students, I use Camtasia. Lookup Video screen recording software and see what you find that you like.

How to make screenshots from TS video stream?

I would like to make screenshots each 1 minutes from the video stream. The video stream is provided as m3u8 file:
#EXTM3U
#EXT-X-TARGETDURATION:6
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:112076
#EXT-X-PROGRAM-DATE-TIME:2019-03-19T16:16:53Z
#EXTINF:6.000, 2019/03/19/16/16/53-06000.ts
#EXTINF:6.000, 2019/03/19/16/16/59-06000.ts
#EXTINF:6.000, 2019/03/19/16/17/05-06000.ts
#EXTINF:6.000, 2019/03/19/16/17/11-06000.ts
I found a library to parse it - https://github.com/globocom/m3u8. But I don't understand how I can convert this TS video stream to single jpeg file.
Am I supposed to
download TS file
find needed frame
extract it
delete ts file?
Should I use OpenCV or is there any easier solution?
use OpenV
This is a job for ffmpeg.
To capture a frame from a playlist every minute, you can use:
ffmpeg -i "http://cam.l-invest.ru/nagatinskaya4/tracks-v1/index.m3u8" -vf fps=1/60 invest.ru_%04d.jpg -hide_banner
The above will produce:
invest.ru_0001.jpg
invest.ru_0002.jpg
and so on... once every 60″
Notes:
invest.ru_0002.jpg was taken exactly 60″ after invest.ru_0001.jpg, as you can see in the upper-right timestamp.
-vf indicates ffmpeg to use a video filter fps=1/60, so it will extract one frame every 60″ (src).
The output format and filename structure can be changed if needed (ex: %Y-%m-%d_%H-%M-%S.jpg). Please check the ffmpeg image2 docs for available options.
I think you can use VLC to do that.
EDIT: looks very similar to https://superuser.com/questions/1379361/vlc-and-m3u8-file.
The following answer might not work for your file format (unless higher versions of VLC work correctly ...). May be have a look to this question which might give you some more insight
To my knowledge, VLC works fines with TS files/streams
Once you have a TS file, you should be able to use vlc to perform your screenshots.
According to this link and to this SO question and answers, one can launch VLC and make it perform screen captures.
And according to VLC documentation, it seems possible.
Should work on win/linux/mac.
I do have tested it yet, I need to reach my personal computer to do that.
Quoting:
With new VLC versions (VLC 1.1.0 and above), the thumbnails are generated with scene video filter
vlc C:\video\to\process.mp4 --rate=1 --video-filter=scene --vout=dummy --start-time=10 --stop-time=11 --scene-format=png --scene-ratio=24 --scene-prefix=snap --scene-path=C:\path\for\snapshots\ vlc://quit
If you want to get rid of the sound you can add "--aout=dummy" next to "--vout=dummy".
For older VLC versions (1.0.0 and below) the same can be done with image output module
vlc C:\video\to\process.mp4 -V image --start-time 0 --stop-time 1 --image-out-format jpg --image-out-ratio 24 --image-out-prefix snap vlc://quit
What it does:
When VLC media player runs it 'plays' the video for one second without actually showing the video on screen, and then quits, leaving us with a file named 'snap000000.jpg', containing an image of the first frame of the video.

Categories