简体   繁体   English

仅使用 Pathlib 合并 mp4 文件

[英]Merging mp4 files with Pathlib only

I just wanna know if this possible without using shutil to merge the files?我只想知道这是否可能不使用shutil合并文件? but by using pathlib only.但仅使用 pathlib。

my old code for merging files:我用于合并文件的旧代码:

 def combine(source: str, output: str),
 
    with open(output, 'wb') as output_file:
        for file in iglob(os.path.join(source, "*.mp4")):
            print(f'Merging', file, end='\r')
            with open(file, 'rb') as input_file:
                shutil.copyfileobj(input_file, output_file)

This is what I tried with pathlib这是我用 pathlib 尝试的

def combine(source: str, output: str),

    for file in Path(source).glob('*.mp4'):
        print(f'Merging', file)
        Path(output).write_bytes(file.read_bytes())

The problem is, it's not appending the bytes and combining to output file.问题是,它没有附加字节并组合到输出文件。

You should only open the output file once and then write the bytes of each file you read.您应该只打开输出文件一次,然后写入您读取的每个文件的字节。

def combine(source: str, output: str),
    print(f'Creating {output}')
    with open(Path(output), 'wb') as outfile:    

        for file in Path(source).glob('*.mp4'):
            print(f'Appending {file}')
            outfile.write(file.read_bytes())  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM