ffmpeg合并音频,视频命令详解,Python 执行命令,linux,window

前言时刻:每次写到ffmpeg,都是心中一种敬畏,简直是神器呀,而且还免费开源,简直业界良心,功能强大到爆炸!

运行环境:
window10
python 3.5
ffmpeg

1.window安装ffmpeg

2.ffmpeg合并视频

2.1使用contact合并

ffmpeg cmd命令:

index_ts_txt_path = '0000index_ffmpeg.txt'       # index_ffmpeg txt路径
output_video_path = 'out_merge.mp4'        # 输出合并视频路径
'ffmpeg -f concat -i "%s" -c copy "%s"' % (index_ts_txt_path ,output_video_path)

0000index_ffmpeg.txt的目录结构:
file ‘001.ts’
file ’ 002.ts’
file ‘003.ts’

with open('0000index_ffmpeg.txt', 'w') as f:
    for ts in list_dir:
        mes = 'file ' + "'%s'" % ts      # file 003.ts
        f.write(mes + '\n')

3.window下合并音频

语法:
ffmpeg -i 01.mp3 -i 02.mp3 -filter_complex "[1:0] [2:0] concat=n=2:v=0:a=1 [a]" -map [a] out_merge.mp3

import sys
import os
from subprocess import Popen


def merge_sound(__sound_path, __out_path):
    cmd = 'ffmpeg'
    s_list = ''
    s_num = 0
    file_name_list = os.listdir(__sound_path)
    print(file_name_list)
    # sound_name_list = [(each if 'mp3' in each ese '') for each in file_name_list]      # 装个比,写的列表推导式

    # 1.除去非音频的文件名称,例如
    sound_name_list = []
    for each in file_name_list:
        if 'mp3' in each:
            sound_name_list.append(each)
    print(sound_name_list)
    sound_name_list.sort(key=lambda x: int(x[:-4]))
    for filename in sound_name_list:     # os.listdir(path)打印出路径中的所有文件
        # if filename:     # 不为空,也就是上面的,非mp3文件
        cmd = cmd + ' -i "%s"' % (__sound_path + '/' + filename)
        # print(cmd)
        s_list = s_list + ' [%s:0]' % str(s_num)
        s_num += 1

    # print(s_list)
    # out_path = 'E:/' + '666.mp3'
    cmd = cmd + ' -filter_complex "%s concat=n=%s:v=0:a=1 [a]" -map [a] "%s"' % (s_list, len(sound_name_list), __out_path)
    print(cmd)
    # os.system(cmd)
    test = Popen(cmd)           # 非阻塞型的cmd执行
    print(test)


if __name__ == "__main__":
    root_path = r''
    # out_path = 'E:/' + '666.mp3'
    # sound_out_path = '/'.join(root_path.split('/')[0:-1]) + "/" + root_path.split('/')[-1] + '.mp3'
    sound_out_path =  '666.mp3'
    print(sound_out_path)
    merge_sound(root_path, sound_out_path)

4.linux下安装ffmpeg

只需三条命令:

1.添加源
sudo add-apt-repository ppa:djcj/hybrid
2.更新源
sudo apt-get update
3.下载安装
sudo apt-get install ffmpeg

但是我在ubuntu下测试一下合并视频的操作,发现使用命令ffmpeg -f concat -i 0000index_ffmpeg.txt -c copy test.mp4 会出错,然后根据提示加了一个:-acodec copy -vcodec copy -absf aac_adtstoasc
更改为:

ffmpeg -f concat -i 0000index_ffmpeg.txt -c copy -acodec copy -vcodec copy -absf aac_adtstoasc test.mp4

你可能感兴趣的:(python)