-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·123 lines (98 loc) · 4.24 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
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
#!/usr/bin/python3
import glob
import os
import subprocess
import random
import argparse
import magic
import time
import signal
import sys
from progress.bar import ChargingBar
from progress.spinner import PixelSpinner
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', type=str, help='source videos full path (recursive)')
parser.add_argument('-d', '--destination', type=str,
help='location to save gifs (each video creates a folder same name as the video)')
parser.add_argument('-l', '--length', type=str, help='gif length in seconds (default 4.3s)', default='4.3')
parser.add_argument('-b', '--begin', type=int,
help='gif generation starts from this value. Good for skipping intros(in seconds, default 90s)',
default=90)
parser.add_argument('-r1', '--randstart', type=int,
help='sets the start value of the randomizer. Minimum distance from the previous gif in seconds '
'(default 20s)',
default=20)
parser.add_argument('-r2', '--randend', type=int,
help='sets the end value of the randomizer. Maximum distance from the previous gif in seconds '
'(default 80s)',
default=80)
args = parser.parse_args()
# Check if at least the source and the destination are set
if args.source is None or args.destination is None:
raise SystemExit("Please enter a valid source and a destination folder. Rest is optional. "
"Please use --help for details")
os.chdir(args.source)
save_to = args.destination
# Clean exit with CTRL+C
def signal_handler(_, frame):
print(" Exiting...")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def generate_gif(input_file):
if not os.path.exists(folder):
os.makedirs(folder)
video_duration = (int(float(subprocess.check_output(
["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of",
"default=noprint_wrappers=1:nokey=1",
input_file], universal_newlines=True).strip())))
# creating vars for easy reading
current_time = args.begin
random_start = args.randstart
random_end = args.randend
# workaround for videos in source root folder
if len(input_file.split("/")) == 1:
print("\nCurrent file: " + input_file)
else:
print("\nCurrent file: " + input_file.split("/")[1])
bar = ChargingBar('Processing', max=video_duration)
# initialize bar
bar.goto(0)
while current_time <= video_duration:
current_time = current_time + (random.randrange(random_start, random_end))
if current_time > video_duration:
# set progress bar to 100% before breaking
bar.goto(video_duration)
break
# creating vars for easy reading
filename_generator = args.destination + '%s-%s.gif'
gif_length = args.length
# you can increase the fps=12 and scale=w=480 values with a higher number for smoother/bigger gifs,
# increases the file size.
subprocess.check_output(
['ffmpeg', '-y', '-ss', str(current_time), '-t', gif_length, '-i', input_file, '-filter_complex',
'[0:v] fps=12,scale=w=480:h=-1,split [a][b];[a] palettegen=stats_mode=single [p];['
'b][p] paletteuse=new=1',
filename_generator % (input_file, current_time)],
stderr=subprocess.STDOUT,
universal_newlines=True).strip()
bar.goto(current_time)
bar.finish()
progress_start = time.time()
video_files = []
video_files_populated = False
spinner = PixelSpinner('Please wait while generating video files list ')
while not video_files_populated:
for file in glob.iglob('**/*.*', recursive=True):
split_path_name = file.split("/")
folder = save_to + split_path_name[0]
if not os.path.isdir(file):
if magic.from_file(file, mime=True)[:5] == 'video':
video_files.append(file)
spinner.next()
video_files_populated = True
for video in range(len(video_files)):
generate_gif(video_files[video])
progress_end = time.time()
total_run = int(progress_end - progress_start)
print("== FINISHED ==")
print("Total Runtime: " + str(total_run) + " seconds")