-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYTTikTokSplitter.py
99 lines (91 loc) · 3.18 KB
/
YTTikTokSplitter.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
from curses.ascii import isdigit, isspace
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import csv
import os
import moviepy.editor as mp
# name of output file
naming = 'trim_'
def get_sec(time_str):
"""Get Seconds from time."""
time = time_str.split(':')
if(len(time)==3):
h = time[0]
m = time[1]
s = time[2]
return int(h) * 3600 + int(m) * 60 + int(s)
else:
m = time[0]
s = time[1]
return int(m) * 60 + int(s)
# sample = open(sys.argv[0], 'r')
# csv1 = csv.reader(sample,delimiter='\n')
with open("chapters.txt", 'r', encoding="utf8") as r, open('chapters_new.txt', 'w', encoding="utf8") as o:
for line in r:
#strip() function
if line.strip():
if line[0].isdigit():
o.write(line.strip()+"\n")
elif line[0].isspace():
while line[0].isspace():
line = line[1:]
if line[0].isdigit():
o.write(line.strip()+"\n")
file1 = open('chapters_new.txt', 'r', encoding="utf8")
Lines = file1.readlines()
# print(Lines)
timestamps = []
titles = []
file_name = 'video.mp4'
for eachline in Lines:
print(eachline)
timestamp = eachline.split(' ')[0]
timestamps.append(timestamp)
length = len((eachline.split(' ')[0]))
title = eachline[length:].strip()
titles.append(title)
print("title is: " + title)
for timestamp in timestamps:
if timestamps.index(timestamp) != (len(timestamps) - 1):
start = timestamp
end = timestamps[timestamps.index(timestamp)+1]
start = get_sec(start)
end = get_sec(end)
out_file = titles[timestamps.index(timestamp)]
# out_file = './' + out_file + '.mp4'
# out_file = out_file + '.mp4'
name, ext = os.path.splitext(file_name)
# name = timestamps.index(timestamp)
name = titles[timestamps.index(timestamp)]
T1 = start
T2 = end
index = timestamps.index(timestamp)
targetname = "%s %s%s" % (index,name, ext)
print(targetname)
print(name)
ffmpeg_extract_subclip(file_name, start, end, targetname=targetname)
# titles[timestamps.index(timestamp)]
else:
start = timestamp
# end = timestamps[timestamps.index(timestamp)+1]
start = get_sec(start)
out_file = str(titles[timestamps.index(timestamp)])
# out_file = './' + out_file + '.mp4'
name, ext = os.path.splitext(file_name)
name = titles[timestamps.index(timestamp)]
# name = timestamps.index(timestamp)
duration = mp.VideoFileClip("video.mp4").duration
print(duration)
T1 = start
T2 = duration
index = timestamps.index(timestamp)
targetname = "%s %s%s" % (index,name, ext)
ffmpeg_extract_subclip(file_name, start, duration, targetname=targetname)
# # file_name = sys.argv[0]
# out_file = eachline # naming + file_name
# start = eachline[1]
# start = get_sec(start)
# end = eachline[2]
# end = get_sec(end)
#ffmpeg_extract_subclip(file_name, start, end, targetname=out_file)
if os.path.exists("chapters_new.txt"):
os.remove("chapters_new.txt")