代码
[Python] 纯文本查看 复制代码import os
import math
ffmpeg = r'.\bin\ffmpeg -loglevel quiet -i %s -i %s -c copy %s'
class ret(object):
def __init__(self, hasFiles, files) -> None:
self.hasFiles = hasFiles
self.files = files
pass
class cacheItem(object):
def __init__(self, name, path) -> None:
self.name = name
self.path = path
pass
def getOutputName(self):
return os.path.join(self.path, self.name + '.mp4')
def getFiles(self):
files = []
for file in os.listdir(self.path):
if file.endswith('.m4s'):
files.append(os.path.join(self.path, file) )
targetFile = []
result = len(files) == 2
if result:
if '30280' in files[0] > 0:
targetFile.append(files[1])
targetFile.append(files[0])
else:
targetFile.append(files[0])
targetFile.append(files[1])
return ret(result, targetFile)
def rewriteFile(fileName, fileIndex):
tmpFile = fileName + ('.mp4' if fileIndex == 0 else '.mp3')
try:
with open(fileName, 'rb') as file: # https://zhuanlan.zhihu.com/p/631220387
file.seek(9)
context = file.read()
with open(tmpFile, 'wb') as tvideo:
tvideo.write(context)
tvideo.close()
file.close()
return True
except Exception as e:
print(e)
return False
def process(cacheItem, p):
targetOutput = cacheItem.getOutputName()
if os.path.exists(targetOutput):
print('文件已存在:' + targetOutput)
return
ret = cacheItem.getFiles()
if not ret.hasFiles:
print('\033[0;37;41m跳过目录: ' + cacheItem.name + '\033[0m')
return
fileIndex = 0
for file in ret.files:
if not rewriteFile(file, fileIndex):
break
fileIndex +=1
try:
os.system(ffmpeg%(ret.files[0]+'.mp4', ret.files[1]+'.mp3', cacheItem.getOutputName()))
os.remove(ret.files[0]+'.mp4')
os.remove(ret.files[1]+'.mp3')
except Exception as e:
print(e)
if p 0:
for i in range(total):
process(cacheItems, math.ceil((i + 1) * 100/ total))
if __name__ == '__main__':
main()