-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg.py
89 lines (77 loc) · 2.19 KB
/
ffmpeg.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
import datetime
import sh
def check_binaries():
sh.ensure("ffmpeg")
sh.ensure("ffprobe")
def get_file_info(file_path):
result = sh.execute(
[
"ffprobe",
"-v",
"error",
"-hide_banner",
"-i",
file_path,
"-show_format",
"-show_streams",
]
)
info = {"format": {}, "streams": []}
lines = iter(result.stdout.splitlines())
while (line := next(lines, None)) is not None:
if line == "[FORMAT]":
while (line := next(lines, None)) is not None and line != "[/FORMAT]":
datum = line.split("=")
info["format"][datum[0]] = datum[1]
elif line == "[STREAM]":
stream = {}
while (line := next(lines, None)) is not None and line != "[/STREAM]":
datum = line.split("=")
stream[datum[0]] = datum[1]
info["streams"].append(stream)
return info
def get_video_info(file_path):
info = get_file_info(file_path)
video_streams = filter(lambda i: i["codec_type"] == "video", info["streams"])
return next(iter(video_streams or []), None)
def extract_image(file_path, timestamp, output_path):
timestamp = str(datetime.timedelta(seconds=timestamp))
result = sh.execute(
[
"ffmpeg",
"-y",
"-ss",
timestamp,
"-i",
file_path,
"-vframes",
"1",
"-q:v",
"2",
output_path,
]
)
# TODO: Error handling
def extract_1m_loop(file_path, timestamp, output_path):
timestamp = str(datetime.timedelta(seconds=timestamp))
result = sh.execute(
[
"ffmpeg",
"-y",
"-ss",
timestamp,
"-i",
file_path,
"-t",
"00:59",
"-codec",
"copy",
output_path,
]
)
# TODO: Error handling
def filter_video(file_path, video_filters, output_path):
vf = ", ".join(video_filters)
result = sh.execute(
["ffmpeg", "-y", "-i", file_path, "-vf", vf, "-c:a", "copy", output_path]
)