FLV格式文件转换为MP3格式文件的python运行程序
一:前提
FLV格式的文件是当前互联网比较常用的视频播放文件格式
最近在下载得到一些flv文件之后,想将它转换为更加方便的mp3格式文件。最开始打算使用网上的在线转换,但是发现网上的在线转换速度实在是太慢了,又从知乎上一位大佬的文章了解到python有这样一个转换的库MoviePy,索性自行编写一个python的执行程序来更加的便捷使用。权当是对之前学习的python知识做巩固。
废话不多说,下面直接开始实现步骤。
二:实现
首先要安装MoviePy
pip install moviepy
然后是python代码(非常的简陋)
import os
import pathlib
import moviepy.editor as mp
import PySimpleGUI as simpleGui
from tkinter import filedialog
import tkinter as tk
from pathlib import Path
def getLocalFile():
root=tk.Tk()
root.withdraw()
filePath=filedialog.askopenfilename().replace('/','\\')
return filePath
def getLocalFolder(values):
root=tk.Tk()
root.withdraw()
FolderPath=filedialog.askdirectory().replace('/','\\')
return FolderPath
def Transof(values):
positionToBeTransfor = values['keyMainWindowPlaintextToBeTransfor']
positionTransforTo = values['keyMainWindowPlaintextTransforTo']
print(positionToBeTransfor)
print(positionTransforTo)
count=0
position=0
for i in positionToBeTransfor:
if i=='\\':
position=count
count+=1
print(position)
MP3Name = positionToBeTransfor[position+1:-4]
print(MP3Name)
positionTransforTo = positionTransforTo+'\\'+MP3Name+'.mp3'
print(positionTransforTo)
clip = mp.AudioFileClip(positionToBeTransfor)
clip.write_audiofile(positionTransforTo)
def main():
try:
mainWindowLayout = [
[simpleGui.Text('输入待转换的flv文件路径'), simpleGui.Input(key='keyMainWindowPlaintextToBeTransfor', size=(80, 1)), ],
[simpleGui.Text(' ')],
[simpleGui.Text(' ')],
[simpleGui.Text('输入输出的mp3文件路径'), simpleGui.Input(key='keyMainWindowPlaintextTransforTo', size=(80, 1)), ],
[simpleGui.Text(' ')],
[simpleGui.Text(' ')],
[simpleGui.Button('选择待转换文件flv文件', key='keyMainWindowFLVFileChoose')],
[simpleGui.Text(' ')],
[simpleGui.Text(' ')],
[simpleGui.Button('选择mp3文件存放位置', key='keyMainWindowMP3FileChoose')],
[simpleGui.Text(' ')],
[simpleGui.Text(' ')],
[simpleGui.Button('开始转换', key='keyMainWindowTransforStart')],
]
mainWindow = simpleGui.Window('flv转换mp3文件', layout=mainWindowLayout, finalize=True)
while True:
window, event, values = simpleGui.read_all_windows()
print("点击了主窗口")
if window == mainWindow:
if event in (simpleGui.WIN_CLOSED, 'keyMainWindowOk', 'keyMainWindowCancel'):
break
elif event == 'keyMainWindowFLVFileChoose':
filePath = getLocalFile();
mainWindow['keyMainWindowPlaintextToBeTransfor'].update(filePath)
elif event == 'keyMainWindowMP3FileChoose':
folderPath = getLocalFolder(values);
mainWindow['keyMainWindowPlaintextTransforTo'].update(folderPath)
elif event == 'keyMainWindowTransforStart':
Transof(values)
mainWindow.close()
except Exception as result:
print("函数 main 捕捉到异常:%s" % result)
if __name__ == '__main__':
main()
最后的运行效果如下(很简陋,别骂了)
正常人类的思路使用应该没有问题,不正常使用会遇到错误,所以正常使用吧!(懒得对那些问题修改了,感觉没啥技术提升,而且懒~~)
|