-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_video_folder.py
More file actions
80 lines (67 loc) · 1.98 KB
/
Copy pathsplit_video_folder.py
File metadata and controls
80 lines (67 loc) · 1.98 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import json
import argparse
import subprocess
def _parse_args():
parser = argparse.ArgumentParser(
usage="python3 split_video_folder.py -i ... -s split_info.json"
)
parser.add_argument(
"-i", "--input_dir", required=True, help="Directory with videos"
)
parser.add_argument(
"-s", "--split_file", required=True, help="JSON file with split info"
)
args, _ = parser.parse_known_args()
return args
def get_video_length(video_name):
length_cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
video_name,
]
length = subprocess.check_output(length_cmd).strip()
return int(float(length))
if __name__ == "__main__":
args = _parse_args()
assert (
os.path.splitext(args.split_file)[1] == ".json"
), "Split info must be in a JSON file"
with open(args.split_file, "r") as fp:
split_info = json.load(fp)
for video_info in split_info:
video_path = os.path.join(args.input_dir, video_info["name"])
split_cmd = [
"ffmpeg",
"-i",
video_path,
"-vcodec",
"copy",
"-acodec",
"copy",
"-y",
]
video_name, video_ext = os.path.splitext(video_path)
for split_count, (split_start, split_end) in enumerate(
video_info["split_time"]
):
split_name = os.path.join(
args.input_dir,
video_name + "_" + str(split_count) + "." + video_ext,
)
print(split_name)
split_args = [
"-ss",
str(split_start),
"-t",
str(split_end - split_start),
split_name,
]
print(split_cmd + split_args)
subprocess.check_output(split_cmd + split_args)
split_count += 1