目的:想把喜欢视频转换为音频,在手机上听
以下的是转换的关键代码,首先要确保moviepy
库是安装成功了。
import moviepy.editor as mp
if __name__ == "__main__":
clip = mp.AudioFileClip(r'D:\studying\project\python\P.flv')
clip.write_audiofile(r'D:\studying\project\python\P.mp3')
以下是批量读取指定目录下.flv
文件转换为.mp3
文件
import moviepy.editor as mp
import os
if __name__ == "__main__":
# 指定文件目录
current_address = r"D:\studying\project\python\flvTomp3\src"
# 获取指定目录下的所有文件
file_list = os.listdir(current_address)
for file_address in file_list:
# 获取文件名(不包含后缀.flv)
file_name = file_address.split('.')[0]
# 拼接文件的绝对路径(dir+xxx.flv)
file_address = os.path.join(current_address, file_address)
print("需要转换的文件 >>>>> " + file_address)
# 调用moviepy库的方法
clip = mp.AudioFileClip(file_address)
# 拼接转换后的文件路径
file_name = file_name + '.mp3'
out_address = os.path.join(current_address, file_name)
print("转换后的文件 >>>>> " + out_address)
###
clip.write_audiofile(out_address)
按常理其他格式也试用吧,等有机会再试吧。
https://www.jianshu.com/p/1b7004db2439
https://www.jianshu.com/p/d731ebabd9fb