Python实现视频中添加音频工具详解
作者:剑客阿良_ALiang 时间:2022-06-03 12:32:28
前言
本文提供在在无音频的视频中添加音频的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,最终输出合成后的视频地址。
来源:https://blog.csdn.net/zhiweihongyan1/article/details/121735158
标签:Python,视频,音频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python ndarray 数组的变形详情
2023-05-13 14:58:47
![](https://img.aspxhome.com/file/2023/9/86239_0s.png)
Python爬虫爬取电影票房数据及图表展示操作示例
2021-07-18 08:34:32
![](https://img.aspxhome.com/file/2023/9/103599_0s.png)
python命令行参数argparse模块基本用法详解
2023-07-31 03:14:21
![](https://img.aspxhome.com/file/2023/2/59282_0s.png)
浅析PHP中的字符串编码转换(自动识别原编码)
2023-09-08 08:04:44
Python的Scrapy爬虫框架简单学习笔记
2023-09-25 06:41:44
python制作简单五子棋游戏
2021-09-13 06:49:47
python 异步async库的使用说明
2022-03-03 20:37:01
python单线程文件传输的实例(C/S)
2023-04-07 22:45:48
![](https://img.aspxhome.com/file/2023/0/76050_0s.jpg)
thinkPHP中配置的读取与C方法详解
2023-11-14 17:12:35
简述php环境搭建与配置
2023-11-15 09:08:28
微软建议的ASP性能优化28条守则(1)
2008-02-22 16:54:00
![](https://img.aspxhome.com/file/UploadPic/up/2008032516342961.gif)
异步任务队列Celery在Django中的使用方法
2023-08-03 06:26:39
![](https://img.aspxhome.com/file/2023/3/110033_0s.png)
PHP接口多继承及tarits实现多继承效果的方法
2023-11-21 04:23:33
pytorch collate_fn的基础与应用教程
2021-06-03 02:55:57
![](https://img.aspxhome.com/file/2023/2/103492_0s.png)
python lambda表达式(匿名函数)写法解析
2023-07-30 20:56:12
Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
2023-08-09 00:46:47
![](https://img.aspxhome.com/file/2023/7/92527_0s.png)
Python中functools模块的常用函数解析
2022-08-12 08:10:50
Dreamweaver量身打造Wordpress留言板(三)
2009-12-13 18:45:00
![](https://img.aspxhome.com/file/UploadPic/200912/13/1444290-13s.gif)
3个 Python 编程技巧
2023-11-30 08:05:19
Django ContentType组件详解
2022-04-11 14:19:26