-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
57 lines (43 loc) · 2.16 KB
/
main.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
import os
import sys
import pdb
import time
import numpy as np
from operator import itemgetter
from moviepy.editor import VideoFileClip, concatenate_videoclips
from videoUtils import Video, download_videos, find_valid_mp4_videos, split_into_compilations
from settings import topics
download = True
export = True
DOWNLOAD_DIR = "downloads"
EXPORT_DIR = "youtube"
if download and not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
for topic in topics:
if download:
print("\n\nInitializing downloads for topic " + topic["name"] + "...\n\n")
download_videos(topic["subreddits"], dirname=DOWNLOAD_DIR)
if export:
print("\n\nInitializing YouTube postprocessing for topic " + topic["name"] + "...\n\n")
videos = find_valid_mp4_videos(topic["subreddits"], dirname=DOWNLOAD_DIR)
# Resize
for video in videos:
ratio = video.width() / video.height()
video.resize(width=1920) if ratio > (16.0 / 9) else video.resize(height=1080)
# Remove videos that are too long
duration = [video.duration() for video in videos]
duration, videos = map(list, zip(*[(d, v) for (d, v) in zip(duration, videos) if d <= topic["MAX_SUBCLIP_TIME"]]))
# Sort videos by duration
duration, videos = map(list, zip(*sorted(zip(duration, videos), key=itemgetter(0))))
# Cumulative time
total_time = np.cumsum(duration)
compilations = split_into_compilations(total_time, videos, max_length=topic["MAX_CLIP_TIME"], max_subsets=3)
print("\n\nGenerating YouTube compilations for topic " + topic["name"] + "...\n\n")
if not os.path.exists(EXPORT_DIR):
os.makedirs(EXPORT_DIR)
for n, compilation in enumerate(compilations):
output_video = concatenate_videoclips([video.file for video in compilation], method="compose")
video_name = topic["name"] + "_" + str(n + 1) + ".mp4"
video_path = os.path.join(EXPORT_DIR, video_name)
output_video.write_videofile(video_path, audio=True, threads=8)
print("\n\nDone with YouTube topic " + topic["name"] + ".\n\n\n")