在FFMPEG中使用Python脚本将长视频切割成块

Mic*_*ton 5 python video ffmpeg python-2.7 python-3.x

首先说我不是程序员,但我真的需要这个我发现的 Python 脚本说它可以做的应用程序。

Icephoenix 南极巢的自动拆分脚本

基本上我有一个长 .MP4 的目录,需要根据 3 小时 15 分钟的总运行时间将其切成相等的部分。例如,我有一个 8 小时的视频,需要在 3:15:00 下将每个部分切成更小的部分。

我们一直在手动编写 FFMPEG 代码来执行此操作,但我发现上面的 Python 脚本似乎可以满足我们的需求。问题是我没有 Python 经验。我不知道在脚本的哪个位置输入视频文件夹路径,或者在哪里指定我的编解码器,或者在哪里告诉程序每个视频块的最长时间是 3:15:00。

我在 64 位 Windows 系统上在命令提示符下工作

这是我所做的:

  • 安装python 3
  • 下载了脚本
  • 我可以单击脚本以查看 cmd 窗口闪烁以指示它正在运行
  • 我在cmd中输入“C:\Python34\python.exe V:\ffmpeg\ffmpeg-split.py”
  • 输出是

    文件“V:\ffmpeg\ffmpeg-split.py”,第16行打印“分割长度不能为0”

      SyntaxError: Missing parentheses in call to 'print'
    
    Run Code Online (Sandbox Code Playgroud)

我不知道从这里去哪里。似乎脚本正在正确加载,但我还没有输入我的变量。任何有关将信息放在哪里的帮助将不胜感激。

下面是我们通常使用的FFMPEG代码:

ffmpeg -i V:\ffmpeg\88518_63c392af.mp4 -vcodec libx264 -acodec copy -vf fps=fps=30000/1001 -ss 00:05:01.000 -t 02:43:49.000 V:\event.

我们使用的ffmpeg代码:

-i 是 .mp4

-vcodec h.264 编解码器

-acodec 应该是“copy”或者可以是“libvo_aacenc”

-vf fps=30000/1000 强制 fps 为 29.97

-ss 是开始时间(我们将使用它与 -t 一起手动切割部分)

-t 是持续时间(我们将计算每个部分的持续时间,即总运行时间除以 3:15:00 下的相等时间,无论是两部分、三部分还是四部分)

谢谢你一百万

Pad*_*ham 4

您需要在打印中添加括号并将异常语法更改为 except Exception as e

import subprocess
import re
import math
from optparse import OptionParser


length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)

def main():

    (filename, split_length) = parse_options()
    if split_length <= 0:
        print("Split length can't be 0")
        raise SystemExit

    output = subprocess.Popen("ffmpeg -i '"+filename+"' 2>&1 | grep 'Duration'",
                            stdout = subprocess.PIPE
                            ).stdout.read()
    print(output)
    matches = re_length.search(output)
    if matches:
        video_length = int(matches.group(1)) * 3600 + \
                        int(matches.group(2)) * 60 + \
                        int(matches.group(3))
        print("Video length in seconds: " + str(video_length))
    else:
        print("Can't determine video length.")
        raise SystemExit

    split_count = math.ceil(video_length/float(split_length))
    if  split_count == 1:
        print("Video length is less then the target split length.")
        raise SystemExit

    split_cmd = "ffmpeg -i '"+filename+"' -vcodec copy "
    for n in range(0, split_count):
        split_str = ""
        if n == 0:
            split_start = 0
        else:
            split_start = split_length * n

        split_str += " -ss "+str(split_start)+" -t "+str(split_length) + \
                    " '"+filename[:-4] + "-" + str(n) + "." + filename[-3:] + \
                    "'"
        print("About to run: " + split_cmd + split_str)
        output = subprocess.Popen(split_cmd+split_str, shell = True, stdout =
                               subprocess.PIPE).stdout.read()


def parse_options():
    parser = OptionParser()

    parser.add_option("-f", "--file",
                        dest = "filename",
                        help = "file to split, for example sample.avi",
                        type = "string",
                        action = "store"
                        )
    parser.add_option("-s", "--split-size",
                        dest = "split_size",
                        help = "split or chunk size in seconds, for example 10",
                        type = "int",
                        action = "store"
                        )
    (options, args) = parser.parse_args()

    if options.filename and options.split_size:

        return (options.filename, options.split_size)

    else:
        parser.print_help()
        raise SystemExit

if __name__ == '__main__':

    try:
        main()
    except Exception as e:
        print("Exception occured running main():")
        print(e)
Run Code Online (Sandbox Code Playgroud)

经过这些更改后,脚本将运行良好,并且还可以改进。

更新版本应与 python2 和 3 兼容:

import re
import math
from optparse import OptionParser

length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)

from subprocess import check_call, PIPE, Popen
import shlex

def main():
    filename, split_length = parse_options()
    if split_length <= 0:
        print("Split length can't be 0")
        raise SystemExit

    p1 = Popen(["ffmpeg", "-i", filename], stdout=PIPE, stderr=PIPE, universal_newlines=True)
    # get p1.stderr as input
    output = Popen(["grep", 'Duration'], stdin=p1.stderr, stdout=PIPE, universal_newlines=True)
    p1.stdout.close()
    matches = re_length.search(output.stdout.read())
    if matches:
        video_length = int(matches.group(1)) * 3600 + \
                       int(matches.group(2)) * 60 + \
                       int(matches.group(3))
        print("Video length in seconds: {}".format(video_length))
    else:
        print("Can't determine video length.")
        raise SystemExit

    split_count = math.ceil(video_length / split_length)

    if split_count == 1:
        print("Video length is less than the target split length.")
        raise SystemExit

    for n in range(split_count):
        split_start = split_length * n
        pth, ext = filename.rsplit(".", 1)
        cmd = "ffmpeg -i {} -vcodec copy  -strict -2 -ss {} -t {} {}-{}.{}".\
            format(filename, split_start, split_length, pth, n, ext)
        print("About to run: {}".format(cmd))
        check_call(shlex.split(cmd), universal_newlines=True)


def parse_options():
    parser = OptionParser()

    parser.add_option("-f", "--file",
                      dest="filename",
                      help="file to split, for example sample.avi",
                      type="string",
                      action="store"
    )
    parser.add_option("-s", "--split-size",
                      dest="split_size",
                      help="split or chunk size in seconds, for example 10",
                      type="int",
                      action="store"
    )
    (options, args) = parser.parse_args()

    if options.filename and options.split_size:

        return options.filename, options.split_size

    else:
        parser.print_help()
        raise SystemExit

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print(e)
Run Code Online (Sandbox Code Playgroud)