python mp3转mp4工具

成品UI

安装moviepy库

pip install moviepy

转换demo

from moviepy.editor import *

# 创建一个颜色剪辑,时长与音频相同
audioclip = AudioFileClip(r"C:\Users\Administrator\PycharmProjects\pythonProject44\test4\赵照 - 灯塔守望人.mp3")
videoclip = ColorClip((800, 600), col=(255, 255, 255), duration=audioclip.duration)

# 把音频文件添加到视频剪辑中
videoclip = videoclip.set_audio(audioclip)

# 保存视频文件
videoclip.write_videofile("output_video.mp4", fps=24)

通过demo代码,进行优化,实现以下功能点

  1. Tk界面,展示一个输入框,输入框展示,选择MP3文件的完整路径。
  2. 展示一个转换按钮,在选择MP3文件后,点击转换按钮,进行转换时,按钮置灰。
  3. 界面展示转换状态,点击转换按钮,展示"正在转换文本"的提示文本,转换成功后,展示"转换成功"的提示文本

完整代码

import tkinter as tk
from tkinter import filedialog
from moviepy.editor import *
from threading import Thread


def select_file():
    file_path = filedialog.askopenfilename(filetypes=[("MP3 files", "*.mp3")])
    if file_path:
        path_entry.delete(0, tk.END)
        path_entry.insert(0, file_path)
        return file_path


def convert():
    file_path = path_entry.get()
    # 创建一个颜色剪辑,时长与音频相同
    audioclip = AudioFileClip(file_path)
    videoclip = ColorClip((800, 600), col=(255, 255, 255), duration=audioclip.duration)

    # 把音频文件添加到视频剪辑中
    videoclip = videoclip.set_audio(audioclip)

    # 获取文件名,不包括扩展名
    base_name = os.path.basename(file_path)
    file_name = os.path.splitext(base_name)[0]

    # 保存视频文件,文件名与音频文件相同,保存在脚本的同一目录下
    videoclip.write_videofile(f"{file_name}.mp4", fps=24)

    convert_button.config(state=tk.NORMAL)
    status_label.config(text='成功!')


def start_conversion():
    convert_button.config(state=tk.DISABLED)
    status_label.config(text='正在转换...')
    Thread(target=convert).start()


def main():
    global path_entry, convert_button, status_label
    root = tk.Tk()
    root.geometry('500x150')

    path_entry = tk.Entry(root, width=40)
    path_entry.pack()

    select_button = tk.Button(root, text='Select MP3', command=select_file)
    select_button.pack()

    convert_button = tk.Button(root, text='Convert', command=start_conversion)
    convert_button.pack()

    status_label = tk.Label(root, text='')
    status_label.pack()

    root.mainloop()

if __name__ == "__main__":
    main()

安装pyinstaller库

把py脚本打包为exe程序

pip install pyinstaller

pyinstaller -w --onefile C:\Users\Administrator\PycharmProjects\pythonProject44\test6\MP3zhuanmp4.py

文章已上传打包附件

相关推荐
凡人的AI工具箱8 分钟前
15分钟学 Python 第38天 :Python 爬虫入门(四)
开发语言·人工智能·后端·爬虫·python
码农超哥同学18 分钟前
Python知识点:在Python编程中,如何使用Gensim进行主题建模
开发语言·python·面试·编程
奔跑吧邓邓子22 分钟前
JSON 全知全解:深入探索 JSON 的奥秘
开发语言·python·json
theoxiong40 分钟前
深度解析:Tkinter 界面布局与优化技巧
python·ui·pyqt·tkinter
背水1 小时前
pytorch数据读入
人工智能·pytorch·python
Ambition_LAO1 小时前
不同版本的 Selenium 和 WebDriver 的 API 兼容性问题
开发语言·python
xs_20121 小时前
Python selenium库学习使用实操二
python·学习·selenium
worxfr1 小时前
Python Selenium常用语法汇总(包含XPath语法)
开发语言·python·selenium·xpath
MicrosoftReactor2 小时前
技术速递|Python in Visual Studio Code 2024年9月发布
vscode·python·jupyter
想七想八不如114082 小时前
【OpenCV】 Python 图像处理 入门
图像处理·python·opencv