On gère le framerate de la vidéo. On gère le cas où le début et/ou la fin tombe sur une i-frame. On affiche les timestamps à vérifier dans la vidéo finale.

This commit is contained in:
Frédéric Tronel
2023-11-28 16:19:16 +01:00
parent 73c44f0da3
commit 28933b8281

View File

@@ -194,6 +194,12 @@ def extractAllStreams(inputFile, begin, end, streams, filesPrefix, nbFrames, wid
if stream['codec_type'] == 'video':
print("Extracting video stream: %s" % stream)
frameRate = stream['r_frame_rate']
pattern = re.compile('^(?P<numerator>[0-9]+)/(?P<denominator>[0-9]+)$')
m = pattern.match(frameRate)
print(m)
if m != None:
frameRate = float(m['numerator']) / float(m['denominator'])
print(frameRate)
sar = stream['sample_aspect_ratio']
dar = stream['display_aspect_ratio']
pixelFormat = stream['pix_fmt']
@@ -203,9 +209,9 @@ def extractAllStreams(inputFile, begin, end, streams, filesPrefix, nbFrames, wid
colorPrimaries = stream['color_primaries']
codec = stream['codec_name']
extractPictures(inputFile=inputFile, begin=begin, nbFrames=nbFrames, prefix="%s-%d" % (filesPrefix, videoID), width=width, height=height)
inputParams.extend(['-i', '%s-%d-%%03d.ppm' % (filesPrefix, videoID)])
inputParams.extend(['-framerate', '%f'%frameRate, '-i', '%s-%d-%%03d.ppm' % (filesPrefix, videoID)])
codecsParams.extend(['-c:v:%d' % videoID, codec, '-pix_fmt', pixelFormat, '-colorspace:v:%d' % videoID, colorSpace, '-color_primaries:v:%d' % videoID, colorPrimaries,
'-color_trc:v:%d' % videoID, colorTransfer, '-color_range:v:%d' % videoID, colorRange ])
'-color_trc:v:%d' % videoID, colorTransfer, '-color_range:v:%d' % videoID, colorRange])
videoID=videoID+1
elif stream['codec_type'] == 'audio':
print("Extracting audio stream: %s" % stream)
@@ -472,6 +478,8 @@ def main():
# Pour chaque portion
partnum = 0
mkvparts = []
checks = []
pos = timedelta()
for ts1, ts2 in parts:
# Trouver l'estampille de la trame 'I' la plus proche (mais postérieure) au début de la portion.
@@ -501,13 +509,16 @@ def main():
headIFrameTS = timedelta(seconds=float(headIFrame['pts_time']))
tailIFrameTS = timedelta(seconds=float(tailIFrame['pts_time']))
checks.append(pos+headIFrameTS-ts1)
subparts = []
head = extractAllStreams(inputFile=mkv, begin=ts1, end=headIFrameTS, nbFrames=nbHeadFrames, filesPrefix='part-%d-head' % (partnum), streams=streams, width=width, height=height)
subparts.append(head)
if nbHeadFrames > 0:
head = extractAllStreams(inputFile=mkv, begin=ts1, end=headIFrameTS, nbFrames=nbHeadFrames, filesPrefix='part-%d-head' % (partnum), streams=streams, width=width, height=height)
subparts.append(head)
tail = extractAllStreams(inputFile=mkv, begin=tailIFrameTS, end=ts2, nbFrames=nbTailFrames, filesPrefix='part-%d-tail' % (partnum), streams=streams, width=width, height=height)
if nbTailFrames > 0:
tail = extractAllStreams(inputFile=mkv, begin=tailIFrameTS, end=ts2, nbFrames=nbTailFrames, filesPrefix='part-%d-tail' % (partnum), streams=streams, width=width, height=height)
# Creating MKV file that corresponds to current part between I-frames
internal = open('part-%d-internal.mkv' % partnum, 'w')
@@ -515,18 +526,27 @@ def main():
extractMKVPart(inputFile=mkv, outputFile=internal, begin=headIFrameTS, end=tailIFrameTS)
subparts.append(internal)
subparts.append(tail)
if nbTailFrames > 0:
subparts.append(tail)
part = mergeMKVs(inputs=subparts, outputName="part-%d.mkv" % partnum)
mkvparts.append(part)
pos = pos+tailIFrameTS-ts1
# We need to check the end also
checks.append(pos)
nbParts = len(mkvparts)
if nbParts > 1:
mergeMKVs(inputs=mvkparts, outputName=args.output)
mergeMKVs(inputs=mkvparts, outputName=args.outputFile)
elif nbParts == 1:
print("A single part")
else:
print("Nothing produced !")
for c in checks:
logger.info("Please check cut smoothness at: %s" % c)
if __name__ == "__main__":
main()