我有一个视频文件,我需要将划分分成几个大小 256KB 的小文件,并将所有文件名保存在文本文件中,然后我需要全部阅读小文件和将它们合并到原始文件中。
这可以在 python 中完成吗?
答案 0 :(得分:2)
首先分裂:
input_file = open(input_filename, 'rb')
blocksize = 4096
chunksize = 1024 * 256
buf = None
chunk_num = 0
current_read = 0
output_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file = open(output_filename, 'wb')
while buf is None or len(buf) > 0:
buf = input_file.read(blocksize)
current_read += len(buf)
output_file.write(buf)
if chunksize <= current_read:
output_file.close()
current_read = 0
chunk_num += 1
output_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file = open(output_filename, 'wb')
output_file.close()
input_file.close()
这可能会让你在那里;根据需要进行调整。
融合:
blocksize = 4096
chunk_num = 0
input_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_filename = 'reconstructed.bin'
output_file = open(output_filename, 'wb')
while True:
try:
input_file = open(input_filename, 'rb')
except IOError:
break
buf = None
while buf is None or len(buf) > 0:
buf = input_file.read(blocksize)
output_file.write(buf)
input_file.close()
chunk_num += 1
input_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file.close()