Add a function to retrieve timestamp of a frame (with multiple ffmpeg version).
This commit is contained in:
43
removeads.py
43
removeads.py
@@ -237,7 +237,18 @@ def ffmpegConvert(inputFile, inputFormat, outputFile, outputFormat, duration):
|
|||||||
status = ffmpeg.wait()
|
status = ffmpeg.wait()
|
||||||
if status != 0:
|
if status != 0:
|
||||||
logger.error('Conversion failed with status code: %d' % status)
|
logger.error('Conversion failed with status code: %d' % status)
|
||||||
|
|
||||||
|
def getTSFrame(frame):
|
||||||
|
if 'pts_time' in frame:
|
||||||
|
pts_time = float(frame['pts_time'])
|
||||||
|
elif 'pkt_pts_time' in frame:
|
||||||
|
pts_time = float(frame['pkt_pts_time'])
|
||||||
|
else:
|
||||||
|
logger.error('Impossible to find timestamp of frame %s' % frame)
|
||||||
|
return None
|
||||||
|
|
||||||
|
ts = timedelta(seconds=pts_time)
|
||||||
|
return ts
|
||||||
|
|
||||||
def getFramesInStream(inputFile, begin, end, streamKind, subStreamId=0):
|
def getFramesInStream(inputFile, begin, end, streamKind, subStreamId=0):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -255,15 +266,9 @@ def getFramesInStream(inputFile, begin, end, streamKind, subStreamId=0):
|
|||||||
if 'frames' in frames:
|
if 'frames' in frames:
|
||||||
frames = frames['frames']
|
frames = frames['frames']
|
||||||
for frame in frames:
|
for frame in frames:
|
||||||
if 'pts_time' in frame:
|
ts = getTSFrame(frame)
|
||||||
pts_time = float(frame['pts_time'])
|
if ts == None
|
||||||
elif 'pkt_pts_time' in frame:
|
|
||||||
pts_time = float(frame['pkt_pts_time'])
|
|
||||||
else:
|
|
||||||
logger.error('Impossible to find timestamp of frame %s' % frame)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
ts = timedelta(seconds=pts_time)
|
|
||||||
if begin <= ts and ts <= end:
|
if begin <= ts and ts <= end:
|
||||||
res.append(frame)
|
res.append(frame)
|
||||||
return res
|
return res
|
||||||
@@ -295,11 +300,15 @@ def getNearestIFrame(inputFile, timestamp, before=True, delta=timedelta(seconds=
|
|||||||
iframes.append(frame)
|
iframes.append(frame)
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
for frame in iframes:
|
for frame in iframes:
|
||||||
if before and timedelta(seconds=float(frame['pts_time'])) <= timestamp:
|
ts = getTSFrame(frame)
|
||||||
|
if ts == None
|
||||||
|
return None
|
||||||
|
|
||||||
|
if before and ts <= timestamp:
|
||||||
found = True
|
found = True
|
||||||
iframe = frame
|
iframe = frame
|
||||||
if not before and timedelta(seconds=float(frame['pts_time'])) >= timestamp:
|
if not before and ts >= timestamp:
|
||||||
found = True
|
found = True
|
||||||
iframe = frame
|
iframe = frame
|
||||||
break
|
break
|
||||||
@@ -308,10 +317,16 @@ def getNearestIFrame(inputFile, timestamp, before=True, delta=timedelta(seconds=
|
|||||||
logger.info("Found i-frame at: %s" % iframe)
|
logger.info("Found i-frame at: %s" % iframe)
|
||||||
logger.debug("Found i-frame at %s" % iframe)
|
logger.debug("Found i-frame at %s" % iframe)
|
||||||
|
|
||||||
its = timedelta(seconds=float(iframe['pts_time']))
|
its = getTSFrame(iframe)
|
||||||
|
if its == None:
|
||||||
|
return None
|
||||||
|
|
||||||
nbFrames = 0
|
nbFrames = 0
|
||||||
for frame in frames:
|
for frame in frames:
|
||||||
ts = timedelta(seconds=float(frame['pts_time']))
|
ts = getTSFrame(frame)
|
||||||
|
if ts == None
|
||||||
|
return None
|
||||||
|
|
||||||
if before:
|
if before:
|
||||||
if its <= ts and ts <= timestamp:
|
if its <= ts and ts <= timestamp:
|
||||||
logger.info("Retrieve a frame between %s and %s at %s" % (its, timestamp, ts))
|
logger.info("Retrieve a frame between %s and %s at %s" % (its, timestamp, ts))
|
||||||
|
|||||||
Reference in New Issue
Block a user