forked from maxweill/Konakona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
150 lines (124 loc) · 4.27 KB
/
run.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
import os
import random
import subprocess
import config
import twitter
# config
script_dir = os.path.dirname(os.path.abspath(__file__))
try:
cfg = config.Config(os.path.join(script_dir,'settings.cfg'))
except config.ConfigFormatError:
print('Error: Check settings.cfg')
exit()
# settings
directory = cfg['settings.general.directory']
video_directory = cfg['settings.general.video.directory']
clip_length = cfg['settings.general.video.length']
video_chance = cfg['settings.general.video.chance']
# twitter keys
CONSUMER_KEY_INPUT = cfg['settings.keys.consumer.key']
CONSUMER_SECRET_INPUT = cfg['settings.keys.consumer.secret']
ACCESS_TOKEN_KEY_INPUT = cfg['settings.keys.access.key']
ACCESS_TOKEN_SECRET_INPUT = cfg['settings.keys.access.secret']
# etc.
tmpfile_img = os.path.join(script_dir,cfg['settings.etc.tmpfile.img'])
tmpfile_vid = os.path.join(script_dir,cfg['settings.etc.tmpfile.vid'])
# randomly select an mkv video from input directory
# look at help diagram to understand which use cases are supported
def get_random_video_filepath(directory):
outdir = ''
direc = directory
while os.path.isdir(direc):
outdir += random.choice(os.listdir(direc))
if outdir.endswith(('mkv', 'mp4', 'avi')):
break
else:
outdir += '/'
direc += outdir
return outdir
# use ffmpeg to generate screenshot at timestamp
def generate_random_screenshot_locally(filepath):
random_time = random.uniform(0.00, get_length(filepath))
command_img = [
'ffmpeg', '-y',
'-ss', str(random_time),
'-i', filepath,
'-vframes', '1',
'-vf', 'scale=1920:-1',
'-q:v', '1',
'-qmin', '1',
tmpfile_img
]
subprocess.call(command_img)
return tmpfile_img
# use ffmpeg to generate 5 sec clip at timestamp
def generate_random_clip_locally(filepath):
random_time = random.uniform(0.00, get_length(filepath) - float(clip_length))
command_vid = [
'ffmpeg', '-y',
'-ss', str(random_time),
'-i', filepath,
'-t', str(clip_length),
'-ac', '2',
'-sn',
'-map_chapters', '-1',
'-c:v', 'libx264',
'-c:a', 'aac',
'-vf', 'scale=1280:-1',
'-pix_fmt', 'yuv420p',
'-movflags', '+faststart',
tmpfile_vid
]
subprocess.call(command_vid)
return tmpfile_vid
# use ffprobe to get the length of the mkv
def get_length(filepath):
command_info = [
'ffprobe',
'-i', filepath,
'-show_entries', 'format=duration',
'-v', 'quiet',
'-of', 'csv=%s' % 'p=0'
]
duration = subprocess.check_output(command_info)
return float(duration)
# uploads the screenshot/clip to twitter
def post_update(tmpfile):
try:
api = twitter.Api(consumer_key=CONSUMER_KEY_INPUT,
consumer_secret=CONSUMER_SECRET_INPUT,
access_token_key=ACCESS_TOKEN_KEY_INPUT,
access_token_secret=ACCESS_TOKEN_SECRET_INPUT)
return api.PostUpdate('', tmpfile)
except twitter.error.TwitterError:
print('Error: Invalid key')
# checks if we should generate a screenshot or clip
def check_video():
r = random.random()
if r <= video_chance:
return True
else:
return False
if __name__ == '__main__':
# determine if we should generate a video or screenshot
shouldGenerateVideo = check_video()
# if we are generating a video, set our working directory to the video directory if one exists.
if shouldGenerateVideo and video_directory:
directory = video_directory
# find our random video by parsing directory
try:
filepath = directory + get_random_video_filepath(directory)
except IndexError:
print('Error: No video file')
exit()
# generate output file
try:
if shouldGenerateVideo:
output = generate_random_clip_locally(filepath)
else:
output = generate_random_screenshot_locally(filepath)
except subprocess.CalledProcessError:
print('Error: Invalid file path')
exit()
# post to twitter
post_update(output)