This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.py
177 lines (156 loc) · 5.86 KB
/
twitch.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import random
import subprocess
from datetime import datetime
def output(string):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(f"{current_time} | {string}")
def getLength(filename):
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return float(result.stdout)
def download_clip(clip_url):
output(f"Downloading clip: {clip_url}")
os.system(f"twitch-dl download -q source {clip_url} > NUL")
def createCompilationAuto(filename):
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
streamer, period, videoNum, minDuration = line.strip().split()
createCompilation(streamer, period, int(videoNum), int(minDuration))
def createCompilation(streamer, period='last_week', videoNum=0, minDuration=10):
output(f"Creating compilation for {streamer}.")
# make output directory if it does not exist
output_directory = f'output/{streamer}'
if not os.path.exists(output_directory):
os.makedirs(output_directory)
output("Retrieving clips.")
# get list of clips from twitch-dl
os.system(f'twitch-dl clips {streamer} --period {period} --limit 100 > temp.txt')
with open('temp.txt', encoding='utf-8') as f:
clipLines = f.readlines()
os.remove('temp.txt')
# reformat into style retrieved from twitch
channelClips = []
i = 0
while (i < len(clipLines)):
if ('Clip' in clipLines[i]):
try:
tempClip = {}
tempClip['id'] = clipLines[i][9:-5]
tempClip['title'] = clipLines[i + 1][5:-5]
tempClip['url'] = clipLines[i + 4][4:-5]
tempClip['game'] = clipLines[i + 2].split('playing')[1][6:-5]
channelClips.append(tempClip)
i = i + 5
except:
i = i + 1
else:
i = i + 1
output("Beginning clip download. (THIS WILL TAKE SOME TIME)")
duration = 0
i = 0
downloads = []
games = []
titles = []
invalid = []
# downloads required clips
while ((duration < minDuration * 60) and i < len(channelClips)):
try:
title = channelClips[i]['title']
except:
i += 1
continue
if '\\' in title:
i += 1
continue
oldFiles = [f for f in os.listdir(os.getcwd()) if os.path.isfile(os.path.join(os.getcwd(), f))]
download_clip(channelClips[i]['url'])
newFiles = [f for f in os.listdir(os.getcwd()) if os.path.isfile(os.path.join(os.getcwd(), f))]
download = ""
for file in newFiles:
if file not in oldFiles:
download = file
if (len(newFiles) == len(oldFiles)) or (download == ""):
invalid.append(channelClips[i]['url'])
i += 1
continue
duration += getLength(download)
while True:
try:
titles.append(title)
break
except:
title = title[:-1]
if (channelClips[i]['game'] not in games):
games.append(channelClips[i]['game'])
downloads.append(f'output/{streamer}/' + download)
output_path = f'output/{streamer}/' + download
if os.path.exists(download):
if os.path.exists(output_path):
output(f"File {download} already exists in output directory. Skipping.")
else:
os.rename(download, output_path)
output(f"Downloaded: {title}")
else:
output(f"File {download} does not exist. Skipping.")
i += 1
if not downloads: # If the list is empty, no clips were downloaded
output(f"No clips could be downloaded for {streamer} in the specified timeframe ({period}). Skipping to next streamer.")
return
# join clips
output("Joining clips.")
random.shuffle(downloads)
tsClipLocations = []
f = open("temp.txt", "w+")
for clip in downloads:
# convert to .ts
clipname = clip[:-4]
os.system(f"ffmpeg -loglevel error -i {clipname}.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts {clipname}.ts")
tsClipLocations.append(clipname + ".ts")
# concatenate .ts to mp4
f.write(f"file '{clipname}.ts'\n")
f.close()
os.system("ffmpeg -loglevel error -f concat -safe 0 -i temp.txt -c copy output.mp4")
os.remove("temp.txt")
os.system(f'ffmpeg -loglevel error -err_detect ignore_err -i output.mp4 -c copy output/{streamer}/{streamer}_{videoNum}.mp4')
os.remove('output.mp4')
# create text file
output("Creating text file.")
f = open(f'output/{streamer}/{streamer}_{videoNum}.txt', "w+")
# title
random.shuffle(titles)
count = 0
while(True): # tries titles until it finds a valid one
try:
title = titles[count]
break
except:
count += 1
f.write(title + "\n")
# description
desc = streamer + "'s Twitch Channel: https://www.twitch.tv/" + streamer
f.write(desc + "\n")
# tags
tags = ""
for game in games:
tags += game + ', '
f.write("Categories: " + tags + "\n")
f.close()
# clean up
for file in tsClipLocations:
try:
os.remove(file)
except:
output(f"Unable to delete {file}")
for file in invalid:
try:
os.remove(file)
except:
output(f"Unable to delete {file}")
output("Compilation creation completed!\n")
return
output("COMPLETE")