1. 程式人生 > 其它 >windows平臺 ffmpeg音訊濾鏡 python 驗證指南

windows平臺 ffmpeg音訊濾鏡 python 驗證指南

技術標籤:ffmpeg例項音訊技術ffmpegpythonwindowshybaseaudiofilter

windows下ffmpeg音訊濾鏡驗證,驗證方式如下,可以二選一

第一種 ffplay實時播放 (註明:amix不支援ffplay

第二種 ffmpeg輸出ts檔案

ffmpeg python3在windows上搭建環境方法請參考:

https://blog.csdn.net/zymill/article/details/110500944

依賴項hysmm_def / ffmpeg_run,請參考:

https://github.com/zymill/ffmpeg_hys

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#/**************************************************************************
#* ffmpeg_af.py: audio filter cases on windows: python 3.x, ffmpeg-4.3.1
#***************************************************************************
#* Copyright (c) 2020-2020 
[email protected]
#* #* This program is free software: you can redistribute it and/or modify #* it under the terms of the GNU General Public License as published by #* the Free Software Foundation, either version 3 of the License, or #* (at your option) any later version. #* #* This program is distributed in the hope that it will be useful, #* but WITHOUT ANY WARRANTY; without even the implied warranty of #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #* GNU General Public License for more details. #* #****************************************************************************/ import os import re import sys import hysmm_def import ffmpeg_run ################################################################################ # 通用引數字串 dst_dir = hysmm_def.dst_dir log_param = hysmm_def.log_param tt_src_dir = 'e:/material/ffmpeg_hys_python' input_mp4_file = ' -i ' + tt_src_dir + '/tt.mp4 ' input_mp3_file = ' -i ' + tt_src_dir + '/es_mp3.mp3 ' af_out = dst_dir + '/af_effect' ################################################################################ # case flags: (default: the first be set, please set others by yourself) play_flag = False # true - ffplay, false - ffmpeg sfx = '.ts' start_num = 1 end_num = 15 class af_case_c: def __init__(self): # aformat 轉換格式 self.case001 = [af_out + '/d001_aformat' + sfx, " aformat=f=s16:r=48000:cl=stereo "] # volume 調整音量 self.case002 = [af_out + '/d002_volume' + sfx, " volume=0.25 "] # amix 混音 (需要兩路以上輸入): 不支援 ffplay self.case003 = [af_out + '/d003_amix' + sfx, " amix=inputs=2:duration=first:dropout_transition=3 "] # adelay 延遲 self.case004 = [af_out + '/d004_adelay' + sfx, " adelay=delays=1500:all=1 "] # aecho 回聲 self.case005 = [af_out + '/d005_aecho' + sfx, " aecho=0.8:0.88:60:0.4 "] # afade 淡入淡出 self.case006 = [af_out + '/d006_afadein' + sfx, " afade=t=in:ss=0:d=5 "] # aloop 迴圈 self.case007 = [af_out + '/d007_aloop' + sfx, " aloop=loop=-1 "] # asubboost 低音炮效果 self.case008 = [af_out + '/d008_asubboost' + sfx, " asubboost=dry=0.5:wet=0.8:decay=0.7:feedback=0.5:cutoff=100:slope=0.5:delay=20"] # atempo 節拍速度 self.case009 = [af_out + '/d009_atempo' + sfx, " atempo=0.5"] # chorus 和聲 # | ^ " &等字元是特殊字元。 # windows cmd.exe特殊字元需轉義。 # 轉義時,^^代表^,^|代表|,^"代表"等等 #af_str = ''' chorus=0.7:0.9:55:0.4:0.25:2''' #af_str = ''' chorus='0.6:0.9:50^|60:0.4^|0.32:0.25^|0.4:2^|1.3' ''' self.case010 = [af_out + '/d010_chorus' + sfx, " chorus='0.7:0.9:55:0.4:0.25:2' "] self.case011 = [af_out + '/d011_chorus' + sfx, " chorus='0.6:0.9:50^|60:0.4^|0.32:0.25^|0.4:2^|1.3' "] self.case012 = [af_out + '/d012_chorus' + sfx, " chorus='0.5:0.9:50^|60^|40:0.4^|0.32^|0.3:0.25^|0.4^|0.3:2^|2.3^|1.3' "] # aresample 重取樣 self.case013 = [af_out + '/d013_aresample' + sfx, " aresample=44100 "] # tremolo 顫音 self.case014 = [af_out + '/d014_tremolo' + sfx, " tremolo "] ################################################################################# # main process if __name__ == "__main__": # init logger log = hysmm_def.Logger('sys_all.log',level='debug') hysmm_def.Logger('sys_err.log', level='error') log.logger.info("========= program start ============") # check source and output dir, create output dir if necessary hysmm_def.checkDir(log, tt_src_dir) hysmm_def.createDirIfNotFound(log, dst_dir) hysmm_def.createDirIfNotFound(log, af_out) # init af case af_case = af_case_c() af_case_dict = af_case.__dict__ # *********************************** # transcode parameters trc_str = ' -vcodec copy -acodec aac -ab 128k ' + hysmm_def.tsmux_vbr_str log.logger.info(trc_str) for i in range(start_num, end_num): idx = '%03d' %i key = "case" + idx if (i == 3): # only for amix input_file = input_mp4_file + input_mp3_file else: input_file = input_mp4_file output_file = af_case_dict[key][0] if (play_flag): af_str = ' -af ' + af_case_dict[key][1] log.logger.info("key:%s, af: %s, dst: %s", key, af_str, output_file) cmd = 'ffplay ' + log_param + input_file + af_str ffmpeg_run.ffplay_run(key, True, log, cmd) else: af_str = ' -filter_complex ' + af_case_dict[key][1] log.logger.info("key:%s, af: %s, dst: %s", key, af_str, output_file) cmd = 'ffmpeg -y ' + log_param + input_file + af_str + trc_str + output_file ffmpeg_run.ffmpeg_run(key, True, log, cmd) ## end of all cases log.logger.info("=== all cases run ===") log.logger.info("============ program exit ============") sys.exit(0) ################################################################################ # end #