-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_split.py
123 lines (97 loc) · 3.91 KB
/
video_split.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
# -*- coding: utf-8 -*-
"""Video_Split_Script.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1yi3kD_Uo1DLJzmjmDB0lhp1Sn1mXfBDH
"""
#split all videos in a folder into chunks and save them to a different folder
#folder paths should not include spaces
import re
import math
length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)
import subprocess
from subprocess import check_call, PIPE, Popen
import shlex
from optparse import OptionParser
def split(filename,split_length,dest,ext,last):
if split_length <= 0:
print("Split length can't be 0")
raise SystemExit
p1 = Popen(["ffmpeg", "-i", filename], stdout=PIPE, stderr=PIPE, universal_newlines=True)
# get p1.stderr as input
output = Popen(["grep", 'Duration'], stdin=p1.stderr, stdout=PIPE, universal_newlines=True)
p1.stdout.close()
matches = re_length.search(output.stdout.read())
if matches:
video_length = int(matches.group(1)) * 3600 + \
int(matches.group(2)) * 60 + \
int(matches.group(3))
#print("Video length in seconds: {}".format(video_length))
else:
print("Can't determine video length.")
raise SystemExit
split_count = math.ceil(video_length / split_length)
if split_count == 1:
print("Video length is less than the target split length.")
raise SystemExit
if last == 1:
split_count = split_count - 1
for n in range(split_count):
split_start = split_length * n
inPth, name1 = filename.rsplit("/", 1)
name, blah = name1.rsplit(".", 1)
pth = dest+'/'+name
cmd = "ffmpeg -i {} -vcodec copy -strict -2 -ss {} -t {} {}-{}.{}".\
format(filename, split_start, split_length, pth, n, ext)
check_call(shlex.split(cmd), universal_newlines=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
def split_videos(dir_path,dest,length,ext, last = 0):
import os
catalogs = os.walk(dir_path)
count = 0
for root, _, files in catalogs:
# get the videos
for name in files:
vid_path = os.path.join(root, name)
try:
split(vid_path,length,dest,ext, last)
count+=1
print(count)
except:
print('Could not split video')
def main():
parser = OptionParser()
parser.add_option("-i", "--input folder",
dest = "input_direct",
help = "File path of folder of videos to split",
type = "string",
action = "store"
)
parser.add_option("-o", "--output folder",
dest = "output_direct",
help = "File path of folder to store split videos",
type = "string",
action = "store"
)
parser.add_option("-l", "--length",
dest = "length",
help = "length of each segment in seconds",
type = "int",
action = "store"
)
parser.add_option("-e", "--extension",
dest = "ext",
help = "file format to save as, for example mp4",
type = "string",
action = "store"
)
parser.add_option("-s", "--last",
dest = "last",
help = "set as 1 to remove last split video",
type = "int",
action = "store"
)
(options, args) = parser.parse_args()
split_videos(options.input_direct,options.output_direct,options.length,options.ext,last = options.last)
if __name__ == '__main__':
main()