-
Notifications
You must be signed in to change notification settings - Fork 0
/
piss.py
61 lines (50 loc) · 1.91 KB
/
piss.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
import json
from os import path, getcwd, remove
from dateutil import parser
from datetime import datetime
from glob import glob
from time import time
import subprocess
def getVideoStoriesFromJsonFiles():
combinedJsonData = []
for jsonFile in glob('json/*.json'):
file = open(jsonFile)
data = file.read()
jsonData = json.loads(data)
for story in jsonData['stories']:
if story['path'].endswith('.mp4') and path.exists(story['path']):
combinedJsonData.append(story)
file.close()
stories = sorted(combinedJsonData, key=lambda item:item['taken_at'])
return stories
def generateFfmpegInputFiles(stories):
lastStoryTS = 0
for story in stories:
dateObj = parser.parse(story['taken_at'])
storyTS = datetime.timestamp(dateObj)
if (storyTS - lastStoryTS > 60):
if (lastStoryTS != 0):
tmpFile.close()
key = dateObj.strftime('%Y%m%d_%H%M')
tmpFile = open('out/' + key + '.tmp', 'a')
tmpFile.write('file ' + getcwd() + '/' + story['path'] + '\n')
lastStoryTS = storyTS
def convertVideoFilesToStories():
numStories = 0
for file in glob('out/*.tmp'):
storyFilename = file.replace('tmp', 'mp4')
print('Generating story %s...' % (storyFilename))
subprocess.call(['ffmpeg', '-hide_banner', '-loglevel', 'warning', '-f', 'concat', '-safe', '0', '-i', file, '-c', 'copy', storyFilename ])
remove(file)
numStories += 1
return numStories
def main():
startTime = time()
print('\n\nGenerating stories\n\n')
stories = getVideoStoriesFromJsonFiles()
generateFfmpegInputFiles(stories)
numStories = convertVideoFilesToStories()
endTime = time()
timeTaken = endTime - startTime
print('\n\nDONE!\nConverted %s videofiles into %s stories in %.2f seconds\n\n' % (len(stories), numStories, timeTaken))
main()