-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomGif.py
75 lines (60 loc) · 2.18 KB
/
randomGif.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
import os
import random
import subprocess
from moviepy.editor import *
def scanfolder(root):
"""
Get all movies with certain file extenstion andd append to list
Params: root - path to root movies Directory
Returns: movies - list of movies found.
"""
movies = []
for path, dirs, files in os.walk(root):
for f in files:
if f.endswith('.mkv') or f.endswith('.m2ts') or f.endswith('.avi'):
#print os.path.join(path, f)
movies.append(os.path.join(path,f))
#print movies
return movies
def getLength(filename):
"""
Get information on choosen movie file.
Params: filename - path to movie to check
Returns: list containing the line "Duration"
Note: For some reason json output wouldn't show Duration while this call does.
"""
result = subprocess.Popen(["ffprobe", filename],stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
return [x for x in result.stdout.readlines() if "Duration" in x]
def makeGif(root):
files = scanfolder(root) #Get all Movies
moviepath = random.choice(files) #Random Movie from list
moviename = moviepath[root.__len__():].split("/")[2] #Remove Root Directory and Subfolder
moviename = moviename[:moviename.index(".")] #Remove file extenstion
moviename = moviename.split("[")[0] #Remove any [2013/1080p/720p]
moviename = moviename.split("(")[0] #Remove any (2013/1080p/720p)
print "Movie Chosen: " + moviename
duration = getLength(moviepath)[0].split(",") #Get Duration
duration = duration[0].split(" ")
duration = duration[3].split(":") #Break into hour, mins, secs
#print duration
hour = random.randint(0,int(duration[0])) #Random hour
mins = random.randint(0,int(duration[1])+1) #Random min
secs = float(duration[2]) #Parse secs to float
timePassed = round(random.uniform(0, 3),2) #Random seconds to elapse up to 3.
"""
print hour
print mins
print secs
print timePassed
"""
#Make GIF 1/3 sized.
VideoFileClip(moviepath).\
subclip((hour,mins,secs),(hour,mins,secs+timePassed)).\
resize(0.3).\
to_gif('movie.gif')
return moviename, hour, mins, secs
if __name__ == "__main__":
var = raw_input("Enter Path to Movie Directory: ")
print "Movies Path: ", var
moviename, hour, mins, secs = makeGif(var)
exit()