-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_script.py
58 lines (47 loc) · 1.41 KB
/
video_script.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from pytube import YouTube
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import VideoFileClip
def video_trim(
link, path=None, filename="video", video_length=5, output_filename="output"
):
yt = YouTube(link)
stream = (
yt.streams.filter(progressive=True, file_extension="mp4")
.order_by("resolution")
.desc()
.first()
)
video_path = (
str(path) + "/" + str(filename) + ".mp4"
if path != None
else str(filename) + ".mp4"
)
stream.download(path, filename=filename)
clip = VideoFileClip(video_path)
start_time = 0
end_time = (
clip.duration if clip.duration <= (60 * video_length) else 60 * video_length
)
time_length = clip.duration
count = 0
while True:
targetname_file = (
str(path) + "/" + str(output_filename) + str(count) + ".mp4"
if path != None
else str(output_filename) + str(count) + ".mp4"
)
try:
ffmpeg_extract_subclip(
video_path, start_time, end_time, targetname=targetname_file,
)
except:
break
start_time = end_time
end_time = end_time + 60 * 5
time_length -= 60 * 5
count += 1
if time_length <= 0:
break
if __name__ == "__main__":
link = input("YouTube link: ")
video_trim(link)