Python+moviepy實現(xiàn)音頻/視頻提取器
在這篇博客中,我們將深入探討一個使用Python和wxPython構建的音頻/視頻提取器應用程序。這個應用程序允許用戶從視頻文件中提取音頻,或者從音頻文件中截取特定時間段。讓我們逐步分析這個程序的功能和實現(xiàn)。
C:\pythoncode\new\MP3towav.py
全部代碼
import wx import os import subprocess from datetime import datetime import json from moviepy.editor import VideoFileClip, AudioFileClip class AudioVideoExtractor(wx.Frame): def __init__(self): super().__init__(parent=None, title='Audio/Video Extractor') self.panel = wx.Panel(self) self.create_widgets() self.load_settings() def create_widgets(self): # File selection self.file_picker = wx.FilePickerCtrl(self.panel, message="Choose an audio or video file") # Time range self.start_time = wx.TextCtrl(self.panel, value="00:00:00") self.end_time = wx.TextCtrl(self.panel, value="00:00:00") # Output format self.formats = ['mp3', 'wav', 'aac'] self.format_choice = wx.Choice(self.panel, choices=self.formats) # Output directory self.dir_picker = wx.DirPickerCtrl(self.panel, message="Choose output directory") # Export button self.export_btn = wx.Button(self.panel, label="Export") self.export_btn.Bind(wx.EVT_BUTTON, self.on_export) # Open button self.open_btn = wx.Button(self.panel, label="Open in PotPlayer") self.open_btn.Bind(wx.EVT_BUTTON, self.on_open) # Layout sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(wx.StaticText(self.panel, label="Select Audio or Video File:"), 0, wx.ALL, 5) sizer.Add(self.file_picker, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(wx.StaticText(self.panel, label="Start Time (HH:MM:SS):"), 0, wx.ALL, 5) sizer.Add(self.start_time, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(wx.StaticText(self.panel, label="End Time (HH:MM:SS):"), 0, wx.ALL, 5) sizer.Add(self.end_time, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(wx.StaticText(self.panel, label="Output Format:"), 0, wx.ALL, 5) sizer.Add(self.format_choice, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(wx.StaticText(self.panel, label="Output Directory:"), 0, wx.ALL, 5) sizer.Add(self.dir_picker, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(self.export_btn, 0, wx.ALL|wx.CENTER, 5) sizer.Add(self.open_btn, 0, wx.ALL|wx.CENTER, 5) self.panel.SetSizer(sizer) def on_export(self, event): input_path = self.file_picker.GetPath() start_time = self.start_time.GetValue() end_time = self.end_time.GetValue() output_format = self.formats[self.format_choice.GetSelection()] output_dir = self.dir_picker.GetPath() if not all([input_path, start_time, end_time, output_format, output_dir]): wx.MessageBox("Please fill in all fields", "Error", wx.OK | wx.ICON_ERROR) return try: # Check if the input file is video or audio file_extension = os.path.splitext(input_path)[1].lower() if file_extension in ['.mp4', '.avi', '.mov', '.flv']: # Add more video extensions if needed clip = VideoFileClip(input_path).audio elif file_extension in ['.mp3', '.wav', '.aac', '.flac']: # Add more audio extensions if needed clip = AudioFileClip(input_path) else: raise ValueError("Unsupported file format") start = self.time_to_seconds(start_time) end = self.time_to_seconds(end_time) clip = clip.subclip(start, end) timestamp = datetime.now().strftime("%Y%m%d") counter = 1 while True: output_filename = f"{timestamp}_{counter:03d}.{output_format}" output_path = os.path.join(output_dir, output_filename) if not os.path.exists(output_path): break counter += 1 clip.write_audiofile(output_path) clip.close() self.last_exported_file = output_path wx.MessageBox(f"Audio exported to {output_path}", "Success", wx.OK | wx.ICON_INFORMATION) self.save_settings() except Exception as e: wx.MessageBox(f"Error: {str(e)}", "Error", wx.OK | wx.ICON_ERROR) def on_open(self, event): if hasattr(self, 'last_exported_file') and os.path.exists(self.last_exported_file): try: subprocess.Popen(['C:\\Program Files\\DAUM\\PotPlayer\\PotPlayerMini64.exe', self.last_exported_file]) except Exception as e: wx.MessageBox(f"Error opening file: {str(e)}", "Error", wx.OK | wx.ICON_ERROR) else: wx.MessageBox("No file has been exported yet", "Error", wx.OK | wx.ICON_ERROR) def time_to_seconds(self, time_str): h, m, s = map(int, time_str.split(':')) return h * 3600 + m * 60 + s def save_settings(self): settings = { 'last_file': self.file_picker.GetPath(), 'start_time': self.start_time.GetValue(), 'end_time': self.end_time.GetValue(), 'format': self.format_choice.GetSelection(), 'output_dir': self.dir_picker.GetPath() } with open('settings.json', 'w') as f: json.dump(settings, f) def load_settings(self): if os.path.exists('settings.json'): with open('settings.json', 'r') as f: settings = json.load(f) self.file_picker.SetPath(settings.get('last_file', '')) self.start_time.SetValue(settings.get('start_time', '00:00:00')) self.end_time.SetValue(settings.get('end_time', '00:00:00')) self.format_choice.SetSelection(settings.get('format', 0)) self.dir_picker.SetPath(settings.get('output_dir', '')) if __name__ == '__main__': app = wx.App() frame = AudioVideoExtractor() frame.Show() app.MainLoop()
1. 概述
這個應用程序的主要功能包括:
- 從視頻文件中提取音頻
- 從音頻文件中截取特定時間段
- 設置輸出音頻格式
- 自定義輸出文件名和路徑
- 使用PotPlayer播放導出的音頻文件
- 保存和加載用戶設置
2. 導入必要的庫
import wx import os import subprocess from datetime import datetime import json from moviepy.editor import VideoFileClip, AudioFileClip
這些庫為我們提供了以下功能:
- wx: 用于創(chuàng)建圖形用戶界面
- os: 用于文件和路徑操作
- subprocess: 用于啟動外部程序(PotPlayer)
- datetime: 用于生成時間戳
- json: 用于保存和加載設置
- moviepy.editor: 用于處理音頻和視頻文件
3. 主應用程序類
class AudioVideoExtractor(wx.Frame): def __init__(self): super().__init__(parent=None, title='Audio/Video Extractor') self.panel = wx.Panel(self) self.create_widgets() self.load_settings()
這個類繼承自wx.Frame,是我們應用程序的主窗口。在初始化方法中,我們創(chuàng)建了一個面板,調用方法來創(chuàng)建UI組件,并加載之前保存的設置。
4. 創(chuàng)建UI組件
def create_widgets(self): # 文件選擇器 self.file_picker = wx.FilePickerCtrl(self.panel, message="Choose an audio or video file") # 時間范圍輸入 self.start_time = wx.TextCtrl(self.panel, value="00:00:00") self.end_time = wx.TextCtrl(self.panel, value="00:00:00") # 輸出格式選擇 self.formats = ['mp3', 'wav', 'aac'] self.format_choice = wx.Choice(self.panel, choices=self.formats) # 輸出目錄選擇 self.dir_picker = wx.DirPickerCtrl(self.panel, message="Choose output directory") # 導出按鈕 self.export_btn = wx.Button(self.panel, label="Export") self.export_btn.Bind(wx.EVT_BUTTON, self.on_export) # 打開按鈕 self.open_btn = wx.Button(self.panel, label="Open in PotPlayer") self.open_btn.Bind(wx.EVT_BUTTON, self.on_open) # 布局設置 # ...
這個方法創(chuàng)建了所有的UI組件,包括文件選擇器、時間輸入框、格式選擇下拉框、目錄選擇器和按鈕。它還設置了組件的布局。
5. 導出功能
def on_export(self, event): # 獲取用戶輸入 input_path = self.file_picker.GetPath() start_time = self.start_time.GetValue() end_time = self.end_time.GetValue() output_format = self.formats[self.format_choice.GetSelection()] output_dir = self.dir_picker.GetPath() # 檢查輸入是否完整 if not all([input_path, start_time, end_time, output_format, output_dir]): wx.MessageBox("Please fill in all fields", "Error", wx.OK | wx.ICON_ERROR) return try: # 檢查輸入文件類型 file_extension = os.path.splitext(input_path)[1].lower() if file_extension in ['.mp4', '.avi', '.mov', '.flv']: clip = VideoFileClip(input_path).audio elif file_extension in ['.mp3', '.wav', '.aac', '.flac']: clip = AudioFileClip(input_path) else: raise ValueError("Unsupported file format") # 處理音頻 start = self.time_to_seconds(start_time) end = self.time_to_seconds(end_time) clip = clip.subclip(start, end) # 生成輸出文件名 timestamp = datetime.now().strftime("%Y%m%d") counter = 1 while True: output_filename = f"{timestamp}_{counter:03d}.{output_format}" output_path = os.path.join(output_dir, output_filename) if not os.path.exists(output_path): break counter += 1 # 導出音頻 clip.write_audiofile(output_path) clip.close() self.last_exported_file = output_path wx.MessageBox(f"Audio exported to {output_path}", "Success", wx.OK | wx.ICON_INFORMATION) self.save_settings() except Exception as e: wx.MessageBox(f"Error: {str(e)}", "Error", wx.OK | wx.ICON_ERROR)
這個方法是程序的核心,它處理音頻/視頻的導出過程:
- 獲取用戶輸入的所有必要信息。
- 檢查輸入文件的類型(音頻或視頻)。
- 根據用戶指定的時間范圍截取音頻。
- 生成一個唯一的輸出文件名。
- 導出音頻文件。
- 保存用戶設置以便下次使用。
6. 在PotPlayer中打開文件
def on_open(self, event): if hasattr(self, 'last_exported_file') and os.path.exists(self.last_exported_file): try: subprocess.Popen(['potplayer.exe', self.last_exported_file]) except Exception as e: wx.MessageBox(f"Error opening file: {str(e)}", "Error", wx.OK | wx.ICON_ERROR) else: wx.MessageBox("No file has been exported yet", "Error", wx.OK | wx.ICON_ERROR)
這個方法允許用戶直接在PotPlayer中打開最近導出的文件。
7. 設置的保存和加載
def save_settings(self): settings = { 'last_file': self.file_picker.GetPath(), 'start_time': self.start_time.GetValue(), 'end_time': self.end_time.GetValue(), 'format': self.format_choice.GetSelection(), 'output_dir': self.dir_picker.GetPath() } with open('settings.json', 'w') as f: json.dump(settings, f) ???????def load_settings(self): if os.path.exists('settings.json'): with open('settings.json', 'r') as f: settings = json.load(f) self.file_picker.SetPath(settings.get('last_file', '')) self.start_time.SetValue(settings.get('start_time', '00:00:00')) self.end_time.SetValue(settings.get('end_time', '00:00:00')) self.format_choice.SetSelection(settings.get('format', 0)) self.dir_picker.SetPath(settings.get('output_dir', ''))
這些方法允許程序保存用戶的設置并在下次啟動時加載它們,提高了用戶體驗。
8. 主程序入口
if __name__ == '__main__': app = wx.App() frame = AudioVideoExtractor() frame.Show() app.MainLoop()
這是程序的入口點,它創(chuàng)建了wxPython應用程序實例,實例化了我們的AudioVideoExtractor類,并啟動主事件循環(huán)。
9.運行結果
結論
這個音頻/視頻提取器是一個功能強大yet易用的工具,展示了如何使用Python和wxPython創(chuàng)建實用的桌面應用程序。它結合了文件I/O、音頻/視頻處理、GUI編程和設置管理等多個方面,是一個很好的學習案例。
以上就是Python+moviepy實現(xiàn)音頻/視頻提取器的詳細內容,更多關于Python音頻視頻提取器的資料請關注腳本之家其它相關文章!
相關文章
Python?Pandas實現(xiàn)將嵌套JSON數(shù)據轉換DataFrame
對于復雜的JSON數(shù)據進行分析時,通常的做法是將JSON數(shù)據結構轉換為Pandas?DataFrame,所以本文就來看看將嵌套JSON數(shù)據轉換為Pandas?DataFrame的具體方法吧2024-01-01Python腳本開發(fā)漏洞的批量搜索與利用(GlassFish?任意文件讀取)
這篇文章主要介紹了Python?開發(fā)漏洞的批量搜索與利用(GlassFish?任意文件讀取),主要包括python開發(fā)學習的意義及測試漏洞是否存在的步驟,需要的朋友可以參考下2022-05-05Django windows使用Apache實現(xiàn)部署流程解析
這篇文章主要介紹了Django windows使用Apache實現(xiàn)部署流程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10