您好,登錄后才能下訂單哦!
這篇文章主要介紹Python如何實現視頻中添加音頻工具,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
ffmpeg環境安裝,可以參考:windows ffmpeg安裝部署
ffmpy安裝:
pip install ffmpy -i https://pypi.douban.com/simple
不廢話,上代碼。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/12/5 1:49 # @Author : 劍客阿良_ALiang # @Site : # @File : video_add_audio_tool.py import os import uuid from ffmpy import FFmpeg # 視頻添加音頻 def video_add_audio(video_path: str, audio_path: str, output_dir: str): _ext_video = os.path.basename(video_path).strip().split('.')[-1] _ext_audio = os.path.basename(audio_path).strip().split('.')[-1] if _ext_audio not in ['mp3', 'wav']: raise Exception('audio format not support') _codec = 'copy' if _ext_audio == 'wav': _codec = 'aac' result = os.path.join( output_dir, '{}.{}'.format( uuid.uuid4(), _ext_video)) ff = FFmpeg( inputs={video_path: None, audio_path: None}, outputs={result: '-map 0:v -map 1:a -c:v copy -c:a {} -shortest'.format(_codec)}) print(ff.cmd) ff.run() return result
代碼說明
1、video_add_audio方法參數有視頻地址、音頻地址、輸出目錄。
2、音頻支持mp3以及wav格式,其中wav格式ffmpeg命令會有所差別。
驗證一下
if __name__ == '__main__': print(video_add_audio('data/999.mp4', 'data/2dc9fa4f-802d-4076-b2c3-b1da886a7cc0.wav', 'data/'))
E:\ProgramData\Anaconda3\envs\pytorch\python.exe C:/Users/yi/PycharmProjects/test/video_add_audio_tool.py ffmpeg -i data/999.mp4 -i data/2dc9fa4f-802d-4076-b2c3-b1da886a7cc0.wav -map 0:v -map 1:a -c:v copy -c:a aac -shortest data/6854281e-19ac-4edc-9638-4124a2a22e52.mp4 ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3-win32 (GCC) 20200320 configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'data/999.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2mp41 encoder : Lavf58.76.100 Duration: 00:00:04.88, start: 0.000000, bitrate: 8420 kb/s Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1280x688 [SAR 1:1 DAR 80:43], 8417 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default) Metadata: handler_name : VideoHandler Guessed Channel Layout for Input Stream #1.0 : stereo Input #1, wav, from 'data/2dc9fa4f-802d-4076-b2c3-b1da886a7cc0.wav': Metadata: copyright : bbs.cnxp.com(C)2009 title : ??????????(?????????).Leon.1994.Directors.Cut.720p.Bluray encoder : Lavf58.45.100 Duration: 00:00:05.02, bitrate: 1411 kb/s Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native)) Press [q] to stop, [?] for help Output #0, mp4, to 'data/6854281e-19ac-4edc-9638-4124a2a22e52.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2mp41 encoder : Lavf58.45.100 Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1280x688 [SAR 1:1 DAR 80:43], q=2-31, 8417 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s Metadata: encoder : Lavc58.91.100 aac frame= 122 fps=0.0 q=-1.0 Lsize= 5096kB time=00:00:04.87 bitrate=8561.0kbits/s speed=72.6x video:5015kB audio:77kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.087828% [aac @ 00000192e5913d40] Qavg: 181.360 data/6854281e-19ac-4edc-9638-4124a2a22e52.mp4 Process finished with exit code 0
OK,最終輸出合成后的視頻地址。
以上是“Python如何實現視頻中添加音頻工具”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。