-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstreamImagesToVideo.py
114 lines (94 loc) · 3.46 KB
/
streamImagesToVideo.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
'''
Streams a series of images to video via ffmpeg
author: steven rick
'''
from datetime import datetime
import shutil
import os
from decimal import Decimal
import subprocess
import sys
import csv
import tkinter as tk
import re
from PIL import Image
root = tk.Tk()
root.withdraw()
from tkinter import filedialog
from tkinter import messagebox
messagebox.showinfo("","Select directory to start search")
search_dir = filedialog.askdirectory()
search_dir = search_dir.replace('/', os.sep)
messagebox.showinfo("","Select directory where you want to stream output")
work_dir = filedialog.askdirectory()
work_dir = work_dir.replace('/', os.sep)
messagebox.showinfo("","Select ffmpeg directory")
ffmpeg_dir = filedialog.askdirectory()
ffmpeg_dir = ffmpeg_dir.replace('/', os.sep)
root.update()
ffmpeg_path = os.path.join(ffmpeg_dir, "ffmpeg.exe")
frame_rate = 60.0
regex = r"Q\d\d\d-\d\d"
def hms2sec(hms):
h, m, s = hms.split(':')
return int(h) * 3600 + int(m) * 60 + Decimal(s)
def createTimeFileDict(f_path):
timeFileDict = dict()
with open(f_path, "r") as f:
csv_file = csv.reader(f, delimiter=",")
skipOne = True
for row in csv_file:
if(skipOne):
skipOne = False
else:
timeFileDict[hms2sec(row[0].strip(' \t\r\n'))] = row[1].strip(' \t\r\n')
return timeFileDict
def readImgToFfmpeg(img_path, subproc):
poll = subproc.poll()
if poll == None:
img = Image.open(img_path)
img.save(subproc.stdin, 'JPEG', quality=100, subsampling=0)
del(img)
return
def streamToVideo(img_dir, out_dir, session):
timeFileDict = createTimeFileDict(os.path.join(img_dir, session)+"_colorImages.csv")
timeArray = sorted(timeFileDict.keys())
offset = Decimal(1/frame_rate)
outVideoPath = os.path.join(out_dir,session+"_colorVideo.mp4")
ffmpeg = '"' + str(ffmpeg_path) + '"'
input_rate = '"' + str(int(frame_rate)) + '"'
out = '"' + outVideoPath + '"'
ffmpeg_cmd = '{0} -f mjpeg -r {1} -i - -c:v libx264 -pix_fmt yuvj444p -r {1} {2}'
subProc = subprocess.Popen(ffmpeg_cmd.format(ffmpeg, input_rate, out), shell=True, bufsize=0, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
previousTime = timeArray[0]
previousImgPath = os.path.join(img_dir, timeFileDict[previousTime])
for currentTime in timeArray[1:]:
while (round((currentTime - previousTime),4) >= offset):
readImgToFfmpeg(previousImgPath, subProc)
previousTime += offset
previousTime = currentTime
previousImgPath = os.path.join(img_dir, timeFileDict[previousTime])
subProc.stdin.close()
subProc.wait()
return
def populateKinectDirectories(search_path):
directoryList = []
print("Searching:", search_path)
for dirpath, dirnames, filenames in os.walk(search_path):
for dirname in dirnames:
if dirname.lower() == "rgb":
directoryList.append(os.path.join(dirpath,dirname))
continue
return directoryList
if __name__ == '__main__':
rgb_dir_list = populateKinectDirectories(search_dir)
print("Found", len(rgb_dir_list), "directories")
input("Hit any key to proceed...")
for d in rgb_dir_list:
raw_dir = d
session = re.search(regex, raw_dir).group(0)
print("Processing:", session)
out_dir = os.path.join(work_dir,'out')
if (not os.path.isdir(out_dir)):
os.mkdir(out_dir)
streamToVideo(raw_dir, out_dir, session)