-
Notifications
You must be signed in to change notification settings - Fork 8
/
splitter.py
executable file
·114 lines (101 loc) · 4.62 KB
/
splitter.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
import csv
import subprocess
import math
import json
import os
import shlex
from optparse import OptionParser
def split_by_manifest(filename, manifest, vcodec="copy", acodec="copy",
extra="", **kwargs):
""" Split video into segments based on the given manifest file.
Arguments:
filename (str) - Location of the video.
manifest (str) - Location of the manifest file.
vcodec (str) - Controls the video codec for the ffmpeg video
output.
acodec (str) - Controls the audio codec for the ffmpeg video
output.
extra (str) - Extra options for ffmpeg.
"""
if not os.path.exists(manifest):
print("File does not exist: %s".format(manifest))
raise SystemExit
with open(manifest) as manifest_file:
manifest_type = manifest.split(".")[-1]
if manifest_type == "json":
config = json.load(manifest_file)
elif manifest_type == "csv":
config = csv.DictReader(manifest_file)
else:
print("Format not supported. File must be a csv or json file")
raise SystemExit
split_cmd = ["ffmpeg", "-i", filename, "-vcodec", vcodec,
"-acodec", acodec, "-y"] + shlex.split(extra)
try:
fileext = filename.split(".")[-1]
except IndexError as e:
raise IndexError("No . in filename. Error: " + str(e))
for video_config in config:
split_str = ""
split_args = []
try:
split_start = video_config["start_time"]
split_length = video_config.get("end_time", None)
if not split_length:
split_length = video_config["length"]
filebase = video_config["rename_to"]
if fileext in filebase:
filebase = ".".join(filebase.split(".")[:-1])
split_args += ["-ss", str(split_start), "-t",
str(split_length), filebase + "." + fileext]
print("########################################################")
print("About to run: "+" ".join(split_cmd+split_args))
print("########################################################")
subprocess.check_output(split_cmd+split_args)
except KeyError as e:
print("############# Incorrect format ##############")
if manifest_type == "json":
print("The format of each json array should be:")
print("{start_time: <int>, length: <int>, rename_to: <string>}")
elif manifest_type == "csv":
print("start_time,length,rename_to should be the first line ")
print("in the csv file.")
print("#############################################")
print(e)
raise SystemExit
def get_video_length(filename):
output = subprocess.check_output(("ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename)).strip()
video_length = int(float(output))
print("Video length in seconds: "+str(video_length))
return video_length
def ceildiv(a, b):
return int(math.ceil(a / float(b)))
def split_by_seconds(filename, split_length, vcodec="copy", acodec="copy",
extra="", video_length=None, split_dir="./", **kwargs):
if split_length and split_length <= 0:
print("Split length can't be 0")
raise SystemExit
if not video_length:
video_length = get_video_length(filename)
split_count = ceildiv(video_length, split_length)
if(split_count == 1):
print("Video length is less then the target split length.")
raise SystemExit
split_cmd = ["ffmpeg", "-i", filename, "-vcodec", vcodec, "-acodec", acodec] + shlex.split(extra)
try:
filebase = ".".join(filename.split(".")[:-1])
fileext = filename.split(".")[-1]
except IndexError as e:
raise IndexError("No . in filename. Error: " + str(e))
for n in range(0, split_count):
split_args = []
if n == 0:
split_start = 0
else:
split_start = split_length * n
filebase = os.path.join(split_dir, "split")
split_args += ["-ss", str(split_start), "-t", str(split_length),
filebase + "-" + str(n+1) + "-of-" + \
str(split_count) + "." + fileext]
print("About to run: "+" ".join(split_cmd+split_args))
subprocess.check_output(split_cmd+split_args)