多媒体科技

python调用FFmpeg把视频剪辑成多段

2019-05-12  本文已影响3人  smallest_one

目录

  1. 参考
  2. 概述
  3. 程序

1. 参考

2. 概述

现在很多短视频的平台对视频的长度有限制,想把现有的长视频快速地剪辑为满足长度需要的多个短视频的片段。FFmpeg是处理视频的瑞士军刀,如要截取视频的一段,可以使用下面的命令。
ffmpeg -i input.mp4 -ss 00:00:01 -to 00:00:05 -c:v copy -c:a copy output.mp4

而需要把视频剪辑成多段,需要写一个脚本比较方便,使用python当然是不二之选!

前提:先安装好ffmpeg和python。

3. 程序

脚本的用法:

jkeditor.py <input> <output_dir> <start_time> <end_time> <slice_duration>

使用示例

jkeditor.py input.mp4 ./slices 00:00:05 00:06:00 15

代码

#jkeditor.py
#coding:utf-8
import sys
import os
import re
import shutil
import subprocess
from datetime import datetime, timedelta

TIME_FROMAT = '%H:%M:%S'

def do_cut(file_input, file_output, s1_time, s2_time):
    start_time = s1_time.strftime(TIME_FROMAT)
    end_time = s2_time.strftime(TIME_FROMAT)
    cmd = 'ffmpeg -i ' + file_input + ' -ss ' + start_time + ' -to ' + end_time + '  -c:v copy -c:a copy ' + file_output
    print "cmd=", cmd
    subprocess.call(cmd, shell=True)

def do_edit():
    file_input = sys.argv[1]
    output_dir = sys.argv[2]
    start_time = sys.argv[3]
    end_time = sys.argv[4]
    slice_duration = int(sys.argv[5])
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.mkdir(output_dir)
    (filepath, tempfilename) = os.path.split(file_input)
    (filename, extension) = os.path.splitext(tempfilename)

    s_time = datetime.strptime(start_time, TIME_FROMAT)
    e_time = datetime.strptime(end_time, TIME_FROMAT)
    n_slice = (int)((e_time - s_time).total_seconds() / slice_duration)
    print "n_slice=", n_slice

    s1_time = s_time;
    for i in range(0, n_slice):
        s2_time = s1_time + timedelta(seconds=slice_duration)
        file_output = output_dir + '/' + filename + str(i) + extension
        do_cut(file_input, file_output, s1_time, s2_time)
        s1_time = s2_time

def usage():
    print "usage:", sys.argv[0], "<input> <output_dir> <start_time> <end_time> <slice_duration>"
    exit(0)

if __name__ == "__main__":
    if len(sys.argv) != 6:
        usage()
    else:
        do_edit()

说明:

后续:

上一篇 下一篇

猜你喜欢

热点阅读