在多个文件上运行相同的 FFMPEG 命令

在多个文件上运行相同的 FFMPEG 命令

我有大约 10 个文件,我想通过这个相当简单的命令来运行

ffmpeg -i“输入”-map 0:0-map 0:1-c:v复制-c:a ac3“输出”

通常我会单独执行每个文件并运行 10 个单独的命令提示符窗口,所以我想知道是否有办法一起运行所有命令?

答案1

使用Python

前任。ffmpeg-copy.py

import os
import os.path
import subprocess

# What directory do we wish to work with?
# '.' is shorthand for the current directory.
root_dir = '.'

# What type of files are we looking for?
# prefix = 'image_'
# ext = '.png'
ext = '.mp4'

# Lists to hold directory item names
included_files = []
skipped_files = []
directories = []

# Get a list of items in our root_dir
everything = []
everything = os.listdir(root_dir)

# For each of the items in our root_dir
for item in everything:

    # Get the full path to our item
    temp_item = os.path.join (root_dir, item)

    # If our item is a file that ends with our selected extension (ext), put
    # its name in our included_files list. Otherwise, put its name in either
    # our skipped_files or directories lists, as appropriate.
    if os.path.isfile(temp_item):
        #if item.startswith(prefix) and item.endswith(ext):
        if item.endswith(ext):
            included_files.append(item)
        else:
            skipped_files.append(item)
    else:
        directories.append(item)

# Visual aid
print('')

# Process our included_files with ffmpeg
for file in included_files:

    input_file = file

    # Get just the base file name, excluding any extension
    output_file = os.path.splitext(file)[0]

    # Form our final output name e.g. example-output.mp4
    output_file += '-output' + ext

    ffmpeg_command = 'ffmpeg -i "' + input_file + \
                     '" -map 0:0 -map 0:1 -c:v copy -c:a ac3 "' + \
                     output_file + '"'

    # Run our ffmpeg command on our file
    try:
        print(ffmpeg_command)
        print('')
        subprocess.run(ffmpeg_command)
    except CalledProcessError as err:
        print('')
        print(err)
        exit()

相关内容