Remove trailing spaces.
This commit is contained in:
61
removeads.py
61
removeads.py
@@ -548,7 +548,7 @@ def SODB2RBSP(buf):
|
||||
replacement = b'\x00\x00\x03' + b
|
||||
res = res.replace(pattern, replacement)
|
||||
|
||||
logger.debug('RBSP: %s' % hexdump.dump(res, sep=':'))
|
||||
logger.debug('RBSP: %s', hexdump.dump(res, sep=':'))
|
||||
return res
|
||||
|
||||
# Useful for SPS and PPS
|
||||
@@ -658,7 +658,6 @@ class HRD:
|
||||
|
||||
return bitPosition
|
||||
|
||||
|
||||
@dataclass
|
||||
class VUI:
|
||||
aspect_ratio_info_present_flag:bool=False
|
||||
@@ -987,7 +986,6 @@ class SPS:
|
||||
bitPosition = self.vui.fromBytes(buf,bitPosition)
|
||||
logger.debug('VUI present: %s', self.vui)
|
||||
|
||||
|
||||
logger.debug('Parse end of SPS. Bit position: %d. Remaining bytes: %s.' % (bitPosition, hexdump.dump(buf[floor(bitPosition/8):], sep=':')))
|
||||
bitPosition = parseRBSPTrailingBits(buf, bitPosition)
|
||||
logger.debug('End of SPS: %d. Remaining bytes: %s' % (bitPosition, hexdump.dump(buf[floor(bitPosition/8):], sep=':')))
|
||||
@@ -1341,7 +1339,6 @@ class AVCDecoderConfiguration:
|
||||
|
||||
logger.debug('Remaining bits: %s' % hexdump.dump(buf[floor(bitPosition/8):]))
|
||||
|
||||
|
||||
if self.AVCProfileIndication in [100, 110, 122, 144]:
|
||||
bitPosition, reserved = readBits(buf, bitPosition, 6)
|
||||
if reserved != 0b111111:
|
||||
@@ -1463,7 +1460,6 @@ class AVCDecoderConfiguration:
|
||||
|
||||
# TODO: do the same with extended SPS !
|
||||
|
||||
|
||||
def parseCodecPrivate(codecPrivateData):
|
||||
if codecPrivateData[0] != 0x63:
|
||||
raise(Exception('Matroska header is wrong: %x' % codecPrivateData[0]))
|
||||
@@ -1581,7 +1577,6 @@ def parseMKVTree(mkvinfo, inputFile):
|
||||
|
||||
prevDepth = depth
|
||||
key=".".join(map(str, prefix))
|
||||
|
||||
elements[key] = (position, size)
|
||||
|
||||
mkvinfo.wait()
|
||||
@@ -1654,7 +1649,6 @@ def dumpCodecPrivateData(AVCDecoderConfiguration):
|
||||
return res
|
||||
|
||||
|
||||
|
||||
def changeEBMLElementSize(inputFile, position, addendum):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -1707,7 +1701,7 @@ def changeEBMLElementSize(inputFile, position, addendum):
|
||||
sizeOfData = int.from_bytes(oldSizeBuf, byteorder='big')
|
||||
logger.info('Size of data with mask: %x mask: %d.' % (sizeOfData, mask))
|
||||
sizeOfData-= (mask<<((sizeOfDataSize-1)*8))
|
||||
logger.info('Found element at position: %d, size of type: %d size of data: %d maximal size: %d.' % (initialPosition, typeSize, sizeOfData, maxSize))
|
||||
logger.info('Found element at position: %d, size of type: %d size of data: %d maximal size: %d.', initialPosition, typeSize, sizeOfData, maxSize)
|
||||
|
||||
newSize = sizeOfData+addendum
|
||||
delta = 0
|
||||
@@ -1716,7 +1710,7 @@ def changeEBMLElementSize(inputFile, position, addendum):
|
||||
newEncodedSize = getEBMLLength(newSize)
|
||||
sizeOfNewEncodedSize = len(newEncodedSize)
|
||||
if sizeOfNewEncodedSize <= sizeOfDataSize:
|
||||
logger.error('New encoded size is smaller (%d) or equal than previous size (%d). This should not happen.' % (sizeOfNewEncodedSize, sizeOfDataSize))
|
||||
logger.error('New encoded size is smaller (%d) or equal than previous size (%d). This should not happen.' , sizeOfNewEncodedSize, sizeOfDataSize)
|
||||
exit(-1)
|
||||
# The difference of length between old size field and new one.
|
||||
delta = sizeOfNewEncodedSize - sizeOfDataSize
|
||||
@@ -1813,7 +1807,6 @@ def changeCodecPrivateData(mkvinfo, inputFile, codecData):
|
||||
# In that case, we update the new delta that will be larger (because the element has been resized).
|
||||
delta+=changeEBMLElementSize(inputFile, pos, delta)
|
||||
|
||||
|
||||
def getFormat(ffprobe, inputFile):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -1939,7 +1932,6 @@ def parseTimestamp(ts):
|
||||
if us < 0 or us > 1000000:
|
||||
logger.error("milliseconds must be in [0,1000000[")
|
||||
return None
|
||||
|
||||
ts = timedelta(hours=hour, minutes=minute, seconds=second, microseconds=us)
|
||||
|
||||
return ts
|
||||
@@ -2037,7 +2029,6 @@ def ffmpegConvert(ffmpeg, ffprobe, inputFile, inputFormat, outputFile, outputFor
|
||||
set_inheritable(infd, True)
|
||||
set_inheritable(outfd, True)
|
||||
|
||||
|
||||
if logger.getEffectiveLevel() == logging.DEBUG:
|
||||
log = []
|
||||
else:
|
||||
@@ -2052,7 +2043,7 @@ def ffmpegConvert(ffmpeg, ffprobe, inputFile, inputFormat, outputFile, outputFor
|
||||
params.extend(['-scodec', 'dvdsub'])
|
||||
params.extend(['-r:0', '25', '-f', outputFormat, '/proc/self/fd/%d' % outfd])
|
||||
|
||||
logger.debug('Executing %s' % params)
|
||||
logger.debug('Executing %s', params)
|
||||
|
||||
with Popen(params, stdout=PIPE, close_fds=False) as ffmpeg:
|
||||
pb = tqdm(TextIOWrapper(ffmpeg.stdout, encoding="utf-8"), total=int(duration/timedelta(seconds=1)), unit='s', desc='Conversion')
|
||||
@@ -2094,7 +2085,6 @@ def getPacketDuration(packet):
|
||||
|
||||
return duration
|
||||
|
||||
|
||||
def getFramesInStream(ffprobe, inputFile, begin, end, streamKind, subStreamId=0):
|
||||
logger = logging.getLogger(__name__)
|
||||
infd = inputFile.fileno()
|
||||
@@ -2283,7 +2273,6 @@ def extractMKVPart(mkvmerge, inputFile, outputFile, begin, end):
|
||||
pb.refresh()
|
||||
pb.close()
|
||||
|
||||
|
||||
status = mkvmerge.wait()
|
||||
if status == 1:
|
||||
logger.warning('Extraction returns warning')
|
||||
@@ -2292,11 +2281,9 @@ def extractMKVPart(mkvmerge, inputFile, outputFile, begin, end):
|
||||
elif status == 2:
|
||||
logger.error('Extraction returns errors')
|
||||
|
||||
|
||||
def extractPictures(ffmpeg, inputFile, begin, nbFrames, width=640, height=480):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
infd = inputFile.fileno()
|
||||
lseek(infd, 0, SEEK_SET)
|
||||
outfd = memfd_create('pictures', flags=0)
|
||||
@@ -2311,7 +2298,7 @@ def extractPictures(ffmpeg, inputFile, begin, nbFrames, width=640, height=480):
|
||||
|
||||
command = [ffmpeg, '-loglevel', 'quiet' ,'-y', '-ss', '%s'%begin, '-i', '/proc/self/fd/%d' % infd, '-s', '%dx%d'%(width, height),
|
||||
'-vframes', '%d'%nbFrames, '-c:v', 'ppm', '-f', 'image2pipe', '/proc/self/fd/%d' % outfd ]
|
||||
logger.debug('Executing: %s' % command)
|
||||
logger.debug('Executing: %s', command)
|
||||
|
||||
images = bytes()
|
||||
with Popen(command, stdout=PIPE, close_fds=False) as ffmpeg:
|
||||
@@ -2342,7 +2329,7 @@ def extractSound(ffmpeg, inputFile, begin, outputFileName, packetDuration, subCh
|
||||
|
||||
command = [ffmpeg, '-y', '-loglevel', 'quiet', '-ss', '%s'%begin, '-i', '/proc/self/fd/%d' % infd, '-frames:a:%d' % subChannel, '%d' % (nbPackets+1),
|
||||
'-c:a', 'pcm_s32le', '-sample_rate', '%d' % sampleRate, '-channels', '%d' % nbChannels, '-f', 's32le', '/proc/self/fd/%d' % outfd]
|
||||
logger.debug('Executing: %s' % command)
|
||||
logger.debug('Executing: %s', command)
|
||||
|
||||
with Popen(command, stdout=PIPE, close_fds=False) as ffmpeg:
|
||||
status = ffmpeg.wait()
|
||||
@@ -2362,7 +2349,6 @@ def extractSound(ffmpeg, inputFile, begin, outputFileName, packetDuration, subCh
|
||||
def dumpPPM(pictures, prefix, temporaries):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# "P6\nWIDTH HEIGHT\n255\n"
|
||||
pos = 0
|
||||
picture = 0
|
||||
@@ -2457,7 +2443,6 @@ def extractAllStreams(ffmpeg, ffprobe, inputFile, begin, end, streams, filesPref
|
||||
exit(-1)
|
||||
|
||||
memfds.append(memfd)
|
||||
|
||||
if dumpMemFD:
|
||||
dumpPPM(imagesBytes, '%s-%d' % (filesPrefix,videoID), temporaries)
|
||||
|
||||
@@ -2484,7 +2469,7 @@ def extractAllStreams(ffmpeg, ffprobe, inputFile, begin, end, streams, filesPref
|
||||
genericCodecParams.extend(['-metadata:s:a:%d' % audioID, 'language=%s' % stream['tags']['language']])
|
||||
packets = getFramesInStream(ffprobe, inputFile=inputFile, begin=begin, end=end, streamKind='a', subStreamId=audioID)
|
||||
nbPackets = len(packets)
|
||||
logger.debug("Found %d packets to be extracted from audio track." % nbPackets)
|
||||
logger.debug("Found %d packets to be extracted from audio track.", nbPackets)
|
||||
if(nbPackets > 0):
|
||||
packetDuration = getPacketDuration(packets[0])
|
||||
if packetDuration is None:
|
||||
@@ -2492,8 +2477,7 @@ def extractAllStreams(ffmpeg, ffprobe, inputFile, begin, end, streams, filesPref
|
||||
else:
|
||||
packetDuration = 0
|
||||
|
||||
|
||||
logger.info("Extracting %d packets of audio stream: a:%d" % (nbPackets, audioID))
|
||||
logger.info("Extracting %d packets of audio stream: a:%d" , nbPackets, audioID)
|
||||
tmpname = '%s-%d.pcm' % (filesPrefix,audioID)
|
||||
|
||||
soundBytes, memfd = extractSound(ffmpeg=ffmpeg, inputFile=inputFile, begin=begin, nbPackets=nbPackets, packetDuration=packetDuration, outputFileName=tmpname, sampleRate=sampleRate, nbChannels=nbChannels)
|
||||
@@ -2573,7 +2557,6 @@ def extractAllStreams(ffmpeg, ffprobe, inputFile, begin, end, streams, filesPref
|
||||
h264outfd = h264Output.fileno()
|
||||
set_inheritable(h264outfd, True)
|
||||
|
||||
|
||||
videoEncoderParams.extend(videoInputParams)
|
||||
videoEncoderParams.extend(videoCodecParams)
|
||||
|
||||
@@ -2595,7 +2578,7 @@ def extractAllStreams(ffmpeg, ffprobe, inputFile, begin, end, streams, filesPref
|
||||
try:
|
||||
h264TSOutput = open(h264TSFileName,'w+')
|
||||
except IOError:
|
||||
logger.error('Impossible to create file: %s' % h264TSFileName)
|
||||
logger.error('Impossible to create file: %s', h264TSFileName)
|
||||
return None
|
||||
|
||||
h264TSOutput.write('# timestamp format v2\n')
|
||||
@@ -2625,7 +2608,7 @@ def mergeMKVs(mkvmerge, inputs, outputName, concatenate=True, timestamps=None):
|
||||
try:
|
||||
out = open(outputName, 'w+')
|
||||
except IOError:
|
||||
logger.error('Impossible to create file: %s' % outputName)
|
||||
logger.error('Impossible to create file: %s', outputName)
|
||||
return None
|
||||
|
||||
outfd = out.fileno()
|
||||
@@ -2665,7 +2648,7 @@ def mergeMKVs(mkvmerge, inputs, outputName, concatenate=True, timestamps=None):
|
||||
# We merge all files.
|
||||
warnings = []
|
||||
env = {**os.environ, 'LANG': 'C'}
|
||||
logger.debug('Executing: LANG=C %s' % mergeParams)
|
||||
logger.debug('Executing: LANG=C %s', mergeParams)
|
||||
|
||||
with Popen(mergeParams, stdout=PIPE, close_fds=False, env=env) as mkvmerge:
|
||||
pb = tqdm(TextIOWrapper(mkvmerge.stdout, encoding="utf-8"), total=100, unit='%', desc='Merging')
|
||||
@@ -2701,7 +2684,7 @@ def findSubtitlesTracks(ffprobe, inputFile):
|
||||
set_inheritable(infd, True)
|
||||
|
||||
command = [ffprobe, '-loglevel','quiet', '-i', '/proc/self/fd/%d' % infd, '-select_streams', 's', '-show_entries', 'stream=index:stream_tags=language', '-of', 'json']
|
||||
logger.debug('Executing: %s' % command)
|
||||
logger.debug('Executing: %s', command)
|
||||
|
||||
with Popen(command, stdout=PIPE, close_fds=False) as ffprobe:
|
||||
out, _ = ffprobe.communicate()
|
||||
@@ -2713,7 +2696,6 @@ def findSubtitlesTracks(ffprobe, inputFile):
|
||||
|
||||
ffprobe.wait()
|
||||
|
||||
|
||||
def extractTrackFromMKV(mkvextract, inputFile, index, outputFile, timestamps):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -2750,10 +2732,10 @@ def extractTrackFromMKV(mkvextract, inputFile, index, outputFile, timestamps):
|
||||
extract.wait()
|
||||
|
||||
if extract.returncode != 0:
|
||||
logger.error('Mkvextract returns an error code: %d' % extract.returncode)
|
||||
logger.error('Mkvextract returns an error code: %d', extract.returncode)
|
||||
return None
|
||||
else:
|
||||
logger.info('Track %d was succesfully extracted.' % index)
|
||||
logger.info('Track %d was succesfully extracted.', index)
|
||||
|
||||
def removeVideoTracksFromMKV(mkvmerge, inputFile, outputFile):
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -2766,7 +2748,7 @@ def removeVideoTracksFromMKV(mkvmerge, inputFile, outputFile):
|
||||
set_inheritable(outfd, True)
|
||||
|
||||
params = [ mkvmerge, '-o', '/proc/self/fd/%d' % outfd, '-D', '/proc/self/fd/%d' % infd]
|
||||
logger.debug('Executing: LANG=C %s' % params)
|
||||
logger.debug('Executing: LANG=C %s', params)
|
||||
|
||||
env = {**os.environ, 'LANG': 'C'}
|
||||
with Popen(params, stdout=PIPE, close_fds=False, env=env) as remove:
|
||||
@@ -2785,7 +2767,7 @@ def removeVideoTracksFromMKV(mkvmerge, inputFile, outputFile):
|
||||
remove.wait()
|
||||
|
||||
if remove.returncode != 0:
|
||||
logger.error('Mkvmerge returns an error code: %d' % remove.returncode)
|
||||
logger.error('Mkvmerge returns an error code: %d', remove.returncode)
|
||||
return None
|
||||
else:
|
||||
logger.info('Video tracks were succesfully extracted.')
|
||||
@@ -2845,7 +2827,7 @@ def concatenateH264Parts(h264parts, output):
|
||||
fd = h264.fileno()
|
||||
totalLength += fstat(fd).st_size
|
||||
|
||||
logger.info('Total length: %d' % totalLength)
|
||||
logger.info('Total length: %d', totalLength)
|
||||
|
||||
outfd = output.fileno()
|
||||
lseek(outfd, 0, SEEK_SET)
|
||||
@@ -2893,6 +2875,7 @@ def concatenateH264TSParts(h264TSParts, output):
|
||||
if first:
|
||||
first = False
|
||||
|
||||
# TODO: finish this procedure
|
||||
def doCoarseProcessing(ffmpeg, ffprobe, mkvmerge, inputFile, begin, end, nbFrames, frameRate, filesPrefix, streams, width, height, temporaries, dumpMemFD):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -2934,7 +2917,7 @@ def main():
|
||||
logger.info('Setting logging to debug mode')
|
||||
coloredlogs.set_level(level=logging.DEBUG)
|
||||
|
||||
logger.debug('Arguments: %s' % args)
|
||||
logger.debug('Arguments: %s', args)
|
||||
|
||||
if args.coarse and args.threshold is not None:
|
||||
logger.error('--coarse and threshold arguments are exclusive.')
|
||||
@@ -3092,7 +3075,6 @@ def main():
|
||||
logger.error('AVC configurations are different: %s\n%s\n' % (mainAvcConfig, isoAvcConfig))
|
||||
exit(-1)
|
||||
|
||||
|
||||
# Pour chaque portion
|
||||
partnum = 0
|
||||
mkvparts = []
|
||||
@@ -3101,7 +3083,6 @@ def main():
|
||||
checks = []
|
||||
pos = timedelta()
|
||||
|
||||
|
||||
otherAvcConfigs = []
|
||||
|
||||
for ts1, ts2 in parts:
|
||||
@@ -3152,7 +3133,6 @@ def main():
|
||||
# else:
|
||||
# doFineGrainProcessing(ffmpeg=paths['ffmpeg'], ffprobe=paths['ffprobe'], inputFile=mkv, begin=ts1, end=headIFrameTS, nbFrames=nbHeadFrames-1, frameRate=frameRate, filesPrefix='part-%d-head' % (partnum), streams=streams, width=width, height=height, temporaries=temporaries, dumpMemFD=args.dump)
|
||||
|
||||
|
||||
if (not args.coarse) and (nbHeadFrames > args.threshold):
|
||||
# We extract all frames between the beginning upto the frame that immediately preceeds the I-frame.
|
||||
h264Head, h264HeadTS, mkvHead = extractAllStreams(ffmpeg=paths['ffmpeg'], ffprobe=paths['ffprobe'], inputFile=mkv, begin=ts1, end=headIFrameTS, nbFrames=nbHeadFrames-1, frameRate=frameRate, filesPrefix='part-%d-head' % (partnum), streams=streams, width=width, height=height, temporaries=temporaries, dumpMemFD=args.dump)
|
||||
@@ -3167,7 +3147,6 @@ def main():
|
||||
if h264HeadTS is not None:
|
||||
h264TS.append(h264HeadTS)
|
||||
|
||||
|
||||
# Creating MKV file that corresponds to current part between I-frames
|
||||
# Internal video with all streams (video, audio and subtitles)
|
||||
internalMKVName = 'part-%d-internal.mkv' % partnum
|
||||
@@ -3203,7 +3182,6 @@ def main():
|
||||
exit(-1)
|
||||
|
||||
# logger.info('Merge header, middle and trailer subpart into: %s' % internalMKVName)
|
||||
|
||||
# Extract internal part of MKV
|
||||
extractMKVPart(mkvmerge=paths['mkvmerge'], inputFile=mkv, outputFile=internalMKV, begin=headIFrameTS, end=tailIFrameTS)
|
||||
|
||||
@@ -3304,7 +3282,6 @@ def main():
|
||||
logger.info('Changing codec private data with the new one.')
|
||||
changeCodecPrivateData(paths['mkvinfo'], finalWithVideo, finalCodecPrivateData)
|
||||
|
||||
|
||||
if args.srt:
|
||||
if not allOptionalTools:
|
||||
logger.warning("Missing tools for extracting subtitles.")
|
||||
|
||||
Reference in New Issue
Block a user