-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncChapter.vpy
More file actions
executable file
·188 lines (164 loc) · 7.84 KB
/
Copy pathsyncChapter.vpy
File metadata and controls
executable file
·188 lines (164 loc) · 7.84 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
import os
import sys
import re
import argparse
import math
import datetime
from vapoursynth import core
from pymediainfo import MediaInfo
from pathlib import Path
from colorama import init as colorama_init
from colorama import Fore
from colorama import Style
from fractions import Fraction
def getDeltaTime(string):
hours = int(re.search("^([0-9]+):",string).group(1))
minutes = int(re.search("^[0-9]+:([0-9]+)",string).group(1))
remain = re.search("^[0-9]+:[0-9]+:([0-9.]+)",string).group(1)
seconds = math.floor(float(remain))
microseconds = int((float(remain)-float(seconds))*1000000)
duration = datetime.timedelta(hours=hours,minutes=minutes,seconds=seconds,microseconds=microseconds)
return duration
colorama_init()
parser = argparse.ArgumentParser( \
description='This script changes timecodes in a xml/txt chapter file to match the nearest I-frame of a video file (or a P-frame if the nearest I-frame is too far)')
parser.add_argument('chapter', type=str, help='Absolute or relative path to xml/txt chapter file')
parser.add_argument('video', type=str, help='Absolute or relative path to mkv/hevc/265/avc/264 video file')
parser.add_argument('-p', '--pframe', type=int, help='Maximal admissible distance to nearest I-frame')
parser.add_argument('-o', '--output', type=str, help='Output filename')
args = parser.parse_args()
chapterFile = os.path.abspath(args.chapter)
videoFile = os.path.abspath(args.video)
pframe = args.pframe
outputFile = args.output
if not os.path.exists(chapterFile):
sys.exit(Fore.RED + "Chapter file not found." + Style.RESET_ALL)
if not os.path.exists(videoFile):
sys.exit(Fore.RED + "Video file not found." + Style.RESET_ALL)
if pframe == None:
pframe = 10000
chapExt = os.path.splitext(chapterFile)[1]
if chapExt == '.xml':
with open(chapterFile, 'r') as chapter:
chapterText = chapter.read()
numberChapter = len(re.findall("<ChapterTimeStart>([0-9.:;]*)</ChapterTimeStart>",chapterText))
if outputFile == None:
outputFile = 'sync.I.frames.chapters.xml'
elif Path(outputFile).suffix != '.xml':
if len(Path(outputFile).suffix) == 4:
outputFile = Path(outputFile).stem + '.xml'
else:
outputFile = outputFile + '.xml'
elif chapExt =='.txt':
with open(chapterFile, 'r') as chapter:
numberChapter = len(chapter.readlines())
numberChapter = int(numberChapter/2)
if outputFile == None:
outputFile = 'sync.I.frames.chapters.txt'
elif Path(outputFile).suffix != '.txt':
if len(Path(outputFile).suffix) == 4:
outputFile = Path(outputFile).stem + '.txt'
else:
outputFile = outputFile + '.txt'
else:
sys.exit(Fore.RED + "Wrong chapter file." + Style.RESET_ALL)
if numberChapter < 1:
sys.exit(Fore.RED + "Wrong chapter file." + Style.RESET_ALL)
media_info = MediaInfo.parse(videoFile)
# frameRate = str(media_info.general_tracks[0].frame_rate)
frameRate = Fraction(int(media_info.video_tracks[0].framerate_num), int(media_info.video_tracks[0].framerate_den))
if frameRate == None:
sys.exit(Fore.RED + "Wrong video file." + Style.RESET_ALL)
if float(frameRate) < 5.0 or float(frameRate) > 100.0:
sys.exit(Fore.RED + "Wrong frame rate." + Style.RESET_ALL)
video = core.ffms2.Source(str(videoFile))
# increment = datetime.timedelta(microseconds=1000000*round(1/float(frameRate),6))
print('')
print(f"{Fore.BLUE}{chapterFile}{Style.RESET_ALL} has {Fore.BLUE}{numberChapter:d}{Style.RESET_ALL} chapters.")
print(f"{Fore.BLUE}{videoFile}{Style.RESET_ALL} has frame rate {Fore.BLUE}{round(float(frameRate),3)}{Style.RESET_ALL} fps.")
chapter = open(chapterFile, 'r')
chapterText = chapter.read()
chapter.close()
if chapExt == '.xml':
timecodeOrig = re.findall("<ChapterTimeStart>([0-9.:;]*)</ChapterTimeStart>",chapterText)
else:
timecodeOrig = re.findall("([0-9]+:[0-9]+:[0-9]+[0-9.]*)",chapterText)
testInit = int(getDeltaTime(timecodeOrig[0]).total_seconds())
if testInit != 0:
sys.exit(Fore.RED + "First chapter does not start at the beginning." + Style.RESET_ALL)
if int(round(float(frameRate)*getDeltaTime(timecodeOrig[int(numberChapter-1)]).total_seconds(),0)) > video.num_frames:
sys.exit(Fore.RED + "Chapter file and video file are inconsistent." + Style.RESET_ALL)
timecodeSynced = []
timecodeSynced.append(timecodeOrig[0])
print('')
frameNumber = int(round(float(frameRate)*getDeltaTime(timecodeOrig[0]).total_seconds(),0))
typeFrame = video.get_frame(frameNumber).props['_PictType']
print(f"Chapter {Fore.BLUE}1{Style.RESET_ALL} shifted by {Fore.BLUE}0{Style.RESET_ALL} frame(s) to get {Fore.BLUE}{typeFrame}{Style.RESET_ALL}-frame.")
for i in range(1,len(timecodeOrig)):
frameNumber = int(round(float(frameRate)*getDeltaTime(timecodeOrig[i]).total_seconds(),0))
typeFrame = video.get_frame(frameNumber).props['_PictType']
if typeFrame != 'I':
matchPlus = False
matchMoins = False
deltaFrame = 0
while matchPlus == False and matchMoins == False and deltaFrame <= pframe:
deltaFrame += 1
typeFramePlus = video.get_frame(frameNumber+deltaFrame).props['_PictType']
typeFrameMoins = video.get_frame(frameNumber-deltaFrame).props['_PictType']
if typeFramePlus == 'I':
matchPlus = True
if typeFrameMoins == 'I':
matchMoins = True
if matchPlus == True:
newFrameNumber = frameNumber + deltaFrame
finalTypeFrame = 'I'
elif matchMoins == True:
newFrameNumber = frameNumber - deltaFrame
finalTypeFrame = 'I'
else:
finalTypeFrame = 'P'
matchPlus = False
matchMoins = False
deltaFrame = 0
while matchPlus == False and matchMoins == False:
deltaFrame += 1
typeFramePlus = video.get_frame(frameNumber+deltaFrame).props['_PictType']
typeFrameMoins = video.get_frame(frameNumber-deltaFrame).props['_PictType']
if typeFramePlus == 'P':
matchPlus = True
if typeFrameMoins == 'P':
matchMoins = True
if matchPlus == True:
newFrameNumber = frameNumber + deltaFrame
elif matchMoins == True:
newFrameNumber = frameNumber - deltaFrame
else:
finalTypeFrame = 'I'
newFrameNumber = frameNumber
chaptIndex = i + 1
deltaFrame = newFrameNumber - frameNumber
print(f"Chapter {Fore.BLUE}{chaptIndex}{Style.RESET_ALL} shifted by {Fore.BLUE}{deltaFrame}{Style.RESET_ALL} frame(s) to get {Fore.BLUE}{finalTypeFrame}{Style.RESET_ALL}-frame.")
total_seconds = round(newFrameNumber/float(frameRate),6)
hours = int(math.floor(total_seconds/3600))
minutes = int(math.floor((total_seconds-hours*3600)/60))
seconds = int(math.floor(total_seconds-hours*3600-minutes*60))
microseconds = int((total_seconds-hours*3600-minutes*60-seconds)*1000000)
if chapExt == '.xml':
newTimeCode = str(hours).zfill(2) + ":" + str(minutes).zfill(2) + ":" + str(seconds).zfill(2) + "." + str(microseconds).zfill(6) + "000"
else:
newTimeCode = str(hours).zfill(2) + ":" + str(minutes).zfill(2) + ":" + str(seconds).zfill(2) + "." + str(int(round(microseconds/1000))).zfill(3)
timecodeSynced.append(newTimeCode)
origChapterFile = Path(chapterFile)
syncedChapterFile = Path(outputFile)
syncedChapterFile.write_text(origChapterFile.read_text())
for i in range(len(timecodeOrig)):
origString = str(timecodeOrig[i])
newString = str(timecodeSynced[i])
syncedChapterFile.write_text(syncedChapterFile.read_text().replace(origString,newString))
fullSyncedChapterFile = os.path.abspath(syncedChapterFile)
print('')
print(f"Chapter time codes updated in {Fore.BLUE}{fullSyncedChapterFile}{Style.RESET_ALL}!")
# print('')
# print(timecodeOrig)
# print(timecodeSynced)