-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_downloader.py
41 lines (31 loc) · 1.13 KB
/
video_downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import yt_dlp as yt
import os
import glob
from yt_dlp.utils import download_range_func
def download_video(video_url):
ydl_opts = {
"outtmpl": "video_downloads/%(title)s.%(ext)s",
}
with yt.YoutubeDL(ydl_opts) as ydl:
res = ydl.extract_info(video_url)
ydl.process_info(res)
path = ydl.prepare_filename(res)
return path
def download_shortened_video(video_url, start_time, end_time):
ydl_opts = {
'verbose': True,
'format': '[ext=mp4]',
'download_ranges': download_range_func(None, [(start_time, end_time)]),
'force_keyframes_at_cuts': True,
'outtmpl': 'library/%(title)s.%(ext)s'
}
with yt.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
if __name__ == "__main__":
# Replace with your desired video URL, start time (in seconds), and end time (in seconds)
youtube_video_url = "https://youtu.be/PCN3ZQ9CHR4?feature=shared"
start_time = 5
end_time = 18
# Create the "library" folder if it doesn't exist
os.makedirs("library", exist_ok=True)
download_shortened_video(youtube_video_url, start_time, end_time)