-
Notifications
You must be signed in to change notification settings - Fork 16
/
cataloger.py
128 lines (93 loc) · 3.57 KB
/
cataloger.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
import copy
from datetime import datetime
from JSONImport import *
import os
import requests
from shutil import copy2, rmtree
class Cataloger:
def __init__(self, db_data, url, s):
self.db_data = db_data
self.url = url
self.s = s
def cataloging(self):
save_path = self.db_data[1]
video_full_path = self.db_data[2]
video_id = video_full_path.split("/")[video_full_path.count("/") - 1]
save_path = save_path
if not os.path.exists(video_full_path):
raise Exception("No videos found.")
if not os.path.exists(save_path):
os.makedirs(save_path)
if len(os.listdir(save_path)) != 0:
rmtree(save_path)
if not os.path.exists(save_path):
os.makedirs(save_path)
self.post_data(10)
self.extract_all(video_full_path, save_path + "/tmp")
self.post_data(60)
self.get_shot_info(video_full_path, save_path)
fps = video_fps
if int(data["fps"]) != 0:
fps = int(data["fps"]
self.post_data(95)
with open(video_full_path[:-4] + "_result.json", 'w', encoding='UTF8') as file:
json.dump(result, file, ensure_ascii=False)
os.remove(save_path + "/tmp.log")
os.remove(save_path + "/tmp.txt")
rmtree(save_path + "/tmp")
def cut_scene(self):
FMT = '%H:%M:%S.%f'
duration = datetime.strptime(end_time, FMT) - datetime.strptime(start_time, FMT)
scene_path = save_path + ("/SCENE_%010d.mp4" % sceneid)
# cmd = "ffmpeg -i {0} -ss {1} -c copy -t {2} {3}".format(video_path, start_time, duration, scene_path)
# subprocess.call(cmd, shell=True)
return scene_path
def get_shot_info(self):
cmd = "ffmpeg -i {0} -filter:v \"select='gt(scene,0.24)',showinfo\" -f null - 2> {1}".format(video_path, save_path + '/tmp.log')
subprocess.call(cmd, shell=True)
cmd2 = "grep showinfo {0} | grep pts_time:[0-9.]* -o | grep '[0-9]*\\.[0-9]*' -o > {1}".format(save_path + '/tmp.log', save_path + '/tmp.txt')
subprocess.call(cmd2, shell=True)
def get_video_info(self):
f = open(save_path + "/tmp.log", "r")
duration = ""
fps = 0.0
while True:
line = f.readline()
if not line: break
if line.find("Duration: ") != -1:
duration = line.replace(", ", "@").replace(": ", "@").split("@")[1]
elif line.find(" fps,") != -1:
t = line.split(" fps,")[0]
fps = float(t[t.rfind(" ") + 1:])
break
f.close()
return duration, fps
def calcStep(self):
return int(video_fps / fps)
def my_round(self):
x = num - int(num)
if x >= 0.9:
return int(num) + 1
else:
return int(num)
def second_to_ms(self):
return int(float(second) * 1000)
def time_to_ms(self):
times = time.split(":")
return (((int(times[0]) * 3600) + (int(times[1]) * 60) + int(times[2].split(";")[0])) * 1000) + int(int(times[2].split(";")[1]) / video_fps * 1000)
def second_to_time(self):
second = int(second)
h = second // (60 * 60)
m = (second // 60) % 60
s = second % 60
return "%02d:%02d:%02d" % (h, m, s)
def extract_all(self):
if not os.path.exists(save_path):
os.makedirs(save_path)
cmd = "ffmpeg -i {0} -ss 00:00:00.000 -qscale:v 2 {1}/IMAGE_%010d.jpg".format(video_path, save_path)
subprocess.call(cmd, shell=True)
def post_data(self):
params = {"status": 1, "progress": number, "error_msg": "", "startFlag": "N", "req_fps": 0}
response = self.s.post(url=self.url, data=params)
if response.status_code != 200:
print("ERROR : failed the post request about '%d' progress" % number)