时间:2020-08-11 python教程 查看: 909
一段把mp4中的音频提取为mp3并保存在原目录的python脚本
需要提前安装好ffmpeg 转换为单线程,耗时较长
github: https://github.com/cuifeiran/extract-mp3-from-mp4
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2020/2/26 17:36
# @Author : CuiFeiran
# @FileName : tool.py
# @Software : PyCharm
# @email :cui2025@126.com
# @Blog : https://blog.csdn.net/qq_33273956
# @bilibili : https://space.bilibili.com/368768799
import os
import glob
from pydub import AudioSegment
wenjianjia = []
path = input('请输入要转码的父文件夹路径:')
for root, dirs, files in os.walk(path):
wenjianjia.append(root)
wjj = wenjianjia
for dir in wjj:
video_dir = dir
extension_list = ('*.mp4', '*.flv')
i = 1
os.chdir(video_dir)
for extension in extension_list:
for video in glob.glob(extension):
mp3_filename = os.path.splitext(os.path.basename(video))[0] + '.mp3'
AudioSegment.from_file(video).export(mp3_filename, format='mp3')
print('已转码', str(i), '个视频!')
i += 1
#
# for infile in glob.glob(os.path.join(video_dir, '*.mp4')):
# os.remove(infile)
总结
到此这篇关于python脚本实现mp4中的音频提取并保存在原目录的文章就介绍到这了,更多相关python mp4 音频提取内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!