说明:仅供学习使用,请勿用于非法用途,若有侵权,请联系博主删除
作者:zhu6201976
一、moviepy实现视频转音频
github地址:GitHub - Zulko/moviepy: Video editing with Python
二、示例代码
"""
@Time : 2024-04-15 20:02
@Author : Tesla
@File : 视频转音频.py
@Description :
1.方式1 https://github.com/Zulko/moviepy -> 推荐 510M转换耗时 112秒=2分钟 大小98M
MoviePy can read and write all the most common audio and video formats, including GIF, and runs on Windows/Mac/Linux, with Python 3.6+
pip install moviepy
2.方式2 特殊格式如m3u8 ffmpeg
"""
import time
from moviepy.editor import *
def test1():
"""
先读取mp4 再提取音频
cost time 112.1316225528717
"""
# 输入视频文件路径
video_path = r"D:\softbackup\mo_tt13394304.mp4"
# 输出音频文件路径
audio_path = r"D:\softbackup\mo_tt13394304.mp3"
# 加载视频
video = VideoFileClip(video_path)
# 提取音频
audio = video.audio
# 保存音频
audio.write_audiofile(audio_path)
print(f"转换完成,音频文件保存在 {audio_path}")
def test2():
"""
直接将mp4文件当成mp3文件读取 省略过程
cost time 113.1316225528717
"""
# 输入视频文件路径
video_path = r"D:\softbackup\mo_tt13394304.mp4"
# 输出音频文件路径
audio_path = r"D:\softbackup\mo_tt13394304.mp3"
# 加载视频
audio = AudioFileClip(video_path)
# 保存音频
audio.write_audiofile(audio_path)
print(f"转换完成,音频文件保存在 {audio_path}")
if __name__ == '__main__':
s = time.time()
# test1()
test2()
e = time.time()
print(f'cost time {e - s}')