-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda1.py
More file actions
63 lines (51 loc) · 1.76 KB
/
lambda1.py
File metadata and controls
63 lines (51 loc) · 1.76 KB
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
import subprocess
import json
def get_video_duration(path) -> str:
result = subprocess.run(
[
"ffprobe",
"-v", "error",
"-show_entries", "format=duration",
"-of", "json",
path
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
info = json.loads(result.stdout)
duration = info["format"]["duration"].split('.')[0]
return duration
def diff_time(start: str, end:str) -> int:
start_sec = list(map(int, start.split(":")))
end_sec = list(map(int, end.split(":")))
return 3600 * start_sec[0] + 60 * start_sec[1] + start_sec[2] - 3600 * end_sec[0] + 60 * end_sec[1] + end_sec[2]
def extract(path, start_tp=None, end_tp=None, frames=8):
"""
path to the video,
start_tp: start timestamp, if None it is 00:00:00
end_tp: end timestamp, if None it takes the whole video
frame: number of frames, by default it is 8
"""
fps = 0
time_stamp = ""
duration = get_video_duration(path)
if not start_tp:
start_tp = "00:00:00"
time_stamp += "-ss " + start_tp
if end_tp:
time_stamp += " -to " + end_tp
else:
time_stamp += " -t " + duration
fps = frames / int(duration)
if not fps:
fps = frames / diff_time(start_tp, end_tp)
cmd = f"ffmpeg -i {path} {time_stamp} -vn -ar 16000 -ac 2 -c:a pcm_s16le audio.wav -an -vf \"fps={fps}\" frame/f%3d.jpg"
try:
subprocess.run("mkdir frame", check=True, shell=True)
res = subprocess.run(cmd, text=True, capture_output=True, check=True, shell=True)
except subprocess.CalledProcessError as e:
print("Error:", e.stderr)
if __name__ == "__main__":
path = input()
extract(path)