diff --git a/removeads.py b/removeads.py index 956e434..88034be 100755 --- a/removeads.py +++ b/removeads.py @@ -237,7 +237,18 @@ def ffmpegConvert(inputFile, inputFormat, outputFile, outputFormat, duration): status = ffmpeg.wait() if status != 0: 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): logger = logging.getLogger(__name__) @@ -255,15 +266,9 @@ def getFramesInStream(inputFile, begin, end, streamKind, subStreamId=0): if 'frames' in frames: frames = frames['frames'] for frame in frames: - 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) + ts = getTSFrame(frame) + if ts == None return None - - ts = timedelta(seconds=pts_time) if begin <= ts and ts <= end: res.append(frame) return res @@ -295,11 +300,15 @@ def getNearestIFrame(inputFile, timestamp, before=True, delta=timedelta(seconds= iframes.append(frame) found = False - for frame in iframes: - if before and timedelta(seconds=float(frame['pts_time'])) <= timestamp: + for frame in iframes: + ts = getTSFrame(frame) + if ts == None + return None + + if before and ts <= timestamp: found = True iframe = frame - if not before and timedelta(seconds=float(frame['pts_time'])) >= timestamp: + if not before and ts >= timestamp: found = True iframe = frame break @@ -308,10 +317,16 @@ def getNearestIFrame(inputFile, timestamp, before=True, delta=timedelta(seconds= logger.info("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 for frame in frames: - ts = timedelta(seconds=float(frame['pts_time'])) + ts = getTSFrame(frame) + if ts == None + return None + if before: if its <= ts and ts <= timestamp: logger.info("Retrieve a frame between %s and %s at %s" % (its, timestamp, ts))