python 提取视频中的音频工具类详解
作者:剑客阿良_ALiang 时间:2023-08-15 06:10:26
前言
利用Python的ffmpy库提取视频中的音频。本文提供工具类代码。
环境依赖
需要安装ffmpy,安装指令:
pip install ffmpy -i https://pypi.douban.com/simple
工具代码
不废话,上代码。
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author : huyi
@file : extract_audio_from_audio.py
@ide : PyCharm
@time : 2021-11-12 17:16:56
"""
from ffmpy import FFmpeg
import os
import uuid
def extract(video_path: str, tmp_dir: str, ext: str):
file_name = '.'.join(os.path.basename(video_path).split('.')[0:-1])
print('文件名:{},提取音频'.format(file_name))
if ext == 'mp3':
return _run_ffmpeg(video_path, os.path.join(tmp_dir, '{}.{}'.format(uuid.uuid4(), ext)), 'mp3')
if ext == 'wav':
return _run_ffmpeg(video_path, os.path.join(tmp_dir, '{}.{}'.format(uuid.uuid4(), ext)), 'wav')
def _run_ffmpeg(video_path: str, audio_path: str, format: str):
ff = FFmpeg(inputs={video_path: None},
outputs={audio_path: '-f {} -vn'.format(format)})
print(ff.cmd)
ff.run()
return audio_path
代码说明
1、入参有视频地址、目录存放地址、音频后缀。
2、按照音频后缀可以适配mp3或者wav格式的音频输出。
验证代码
if __name__ == '__main__':
print(extract('C:/Users/huyi/Desktop/test1.mp4', 'C:/Users/huyi/Desktop', 'wav'))
总结
把一些工具类记下,以后方便查阅,对自己说。
来源:https://huyi-aliang.blog.csdn.net/article/details/121293293
标签:python,音频提取,视频音频提取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 利用栈和队列模拟递归的过程
2023-12-14 14:08:12
Python基础之logging模块知识总结
2021-12-20 04:20:07
Python栈的实现方法示例【列表、单链表】
2023-07-20 15:51:42
Python 判断奇数偶数的方法
2023-03-03 18:16:31
快速实现基于Python的微信聊天机器人示例代码
2022-05-30 19:22:50
![](https://img.aspxhome.com/file/2023/4/121374_0s.png)
一篇文章带你了解Python之Selenium自动化爬虫
2023-12-15 14:30:53
![](https://img.aspxhome.com/file/2023/2/98802_0s.png)
OpenCV图像变换之傅里叶变换的一些应用
2023-12-01 22:11:34
![](https://img.aspxhome.com/file/2023/9/85359_0s.jpg)
JavaScript 学习 - 提高篇
2024-04-30 08:54:46
python使用PyFetion来发送短信的例子
2022-08-22 09:43:54
Python数据清洗工具之Numpy的基本操作
2023-08-31 02:39:10
![](https://img.aspxhome.com/file/2023/0/66300_0s.png)
vue中view-model双向绑定基础原理解析
2024-05-28 15:53:17
Python 如何批量更新已安装的库
2023-06-05 12:15:40
![](https://img.aspxhome.com/file/2023/9/105269_0s.jpg)
Linux中将txt导入到mysql的方法教程
2024-01-12 13:04:27
python实现傅里叶级数展开的实现
2022-06-14 14:35:17
![](https://img.aspxhome.com/file/2023/1/117971_0s.png)
解决vue项目input输入框双向绑定数据不实时生效问题
2024-05-28 15:42:13
MySQL优化之数据表的处理
2008-12-22 14:45:00
python nohup 实现远程运行不宕机操作
2023-10-21 02:21:44
![](https://img.aspxhome.com/file/2023/8/71438_0s.jpg)
使用pyinstaller打包PySide2程序中遇到的问题
2023-02-10 18:23:32
![](https://img.aspxhome.com/file/2023/0/121020_0s.png)
Python文本特征抽取与向量化算法学习
2023-07-19 09:44:29
![](https://img.aspxhome.com/file/2023/1/81501_0s.jpg)
服务器端的代码是如何被解释成客户端的?
2009-11-01 15:15:00