# Initialize file paths
self.mpc_hc_path = ""
self.video_path = ""
self.audio_path = ""
# GUI Layout
self.create_widgets()
def create_widgets(self):
padding_options = {'padx': 10, 'pady': 5}
# MPC-HC Executable Selection
self.mpc_label = tk.Label(self.master, text="MPC-HC Executable:")
self.mpc_label.grid(row=0, column=0, sticky='e', **padding_options)
self.mpc_entry = tk.Entry(self.master, width=50)
self.mpc_entry.grid(row=0, column=1, **padding_options)
self.mpc_button = tk.Button(self.master, text="Browse", command=self.browse_mpc_hc)
self.mpc_button.grid(row=0, column=2, **padding_options)
# Video Selection
self.video_label = tk.Label(self.master, text="Select Video:")
self.video_label.grid(row=1, column=0, sticky='e', **padding_options)
self.video_entry = tk.Entry(self.master, width=50)
self.video_entry.grid(row=1, column=1, **padding_options)
self.video_button = tk.Button(self.master, text="Browse", command=self.browse_video)
self.video_button.grid(row=1, column=2, **padding_options)
# Audio Selection (Allows both audio and video files)
self.audio_label = tk.Label(self.master, text="Select Audio or Video (Optional):")
self.audio_label.grid(row=2, column=0, sticky='e', **padding_options)
self.audio_entry = tk.Entry(self.master, width=50)
self.audio_entry.grid(row=2, column=1, **padding_options)
self.audio_button = tk.Button(self.master, text="Browse", command=self.browse_audio)
self.audio_button.grid(row=2, column=2, **padding_options)
# Play Button
self.play_button = tk.Button(self.master, text="Play", command=self.play_media, bg="green", fg="white", width=20)
self.play_button.grid(row=3, column=1, pady=20)
def browse_mpc_hc(self):
"""
Opens a file dialog to select the MPC-HC executable.
"""
path = filedialog.askopenfilename(title="Select MPC-HC Executable",
filetypes=[("MPC-HC Executable", "mpc-hc.exe"), ("All Files", "*.*")])
if path:
if os.path.isfile(path) and path.lower().endswith("mpc-hc.exe"):
self.mpc_hc_path = path
self.mpc_entry.delete(0, tk.END)
self.mpc_entry.insert(0, path)
else:
messagebox.showerror("Invalid Selection", "The selected file is not mpc-hc.exe. Please try again.")
def browse_video(self):
"""
Opens a file dialog to select a video file.
"""
# Define a broad range of video file extensions
video_extensions = [
"*.mp4", "*.mkv", "*.avi", "*.mov", "*.wmv", "*.flv",
"*.mpeg", "*.mpg", "*.mts", "*.m2ts", "*.ts", "*.webm",
"*.3gp", "*.vob"
]
filetypes = [("Video Files", " ".join(video_extensions)), ("All Files", "*.*")]
path = filedialog.askopenfilename(title="Select Video File", filetypes=filetypes)
if path:
self.video_path = path
self.video_entry.delete(0, tk.END)
self.video_entry.insert(0, path)
def browse_audio(self):
"""
Opens a file dialog to select an audio or video file for external audio.
"""
# Define a combination of audio and video file extensions for flexibility
audio_video_extensions = [
"*.mp3", "*.aac", "*.flac", "*.wav", "*.m4a", "*.ogg", # Audio formats
"*.mp4", "*.mkv", "*.avi", "*.mov", "*.wmv", "*.flv",
"*.mpeg", "*.mpg", "*.mts", "*.m2ts", "*.ts", "*.webm",
"*.3gp", "*.vob" # Video formats that can contain audio
]
filetypes = [("Audio or Video Files", " ".join(audio_video_extensions)), ("All Files", "*.*")]
path = filedialog.askopenfilename(title="Select Audio or Video File", filetypes=filetypes)
if path:
self.audio_path = path
self.audio_entry.delete(0, tk.END)
self.audio_entry.insert(0, path)
def validate_paths(self):
"""
Validates the existence of the MPC-HC executable and media files.
"""
if not self.mpc_hc_path:
messagebox.showerror("Error", "Please select the MPC-HC executable.")
return False
if not os.path.isfile(self.mpc_hc_path):
messagebox.showerror("Error", f"MPC-HC executable not found:\n{self.mpc_hc_path}")
return False
if not self.video_path:
messagebox.showerror("Error", "Please select a video file to play.")
return False
if not os.path.isfile(self.video_path):
messagebox.showerror("Error", f"Video file not found:\n{self.video_path}")
return False
if self.audio_path and not os.path.isfile(self.audio_path):
messagebox.showerror("Error", f"Audio or Video file not found:\n{self.audio_path}")
return False
return True
def play_media(self):
"""
Constructs and executes the command to play the media using MPC-HC.
"""
if not self.validate_paths():
return
try:
# Construct command
cmd = [f'"{self.mpc_hc_path}"', f'"{self.video_path}"']
if self.audio_path:
# Use the /AudioFile option to load external audio
cmd.extend(['/AudioFile', f'"{self.audio_path}"'])
# Join the command list into a single string
cmd_str = ' '.join(cmd)
# Debug: print the command
print(f"Executing command: {cmd_str}")
# Start the process
subprocess.Popen(cmd_str, shell=True)
# Optionally, reset the entries after playing
# self.reset_entries()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while trying to play media:\n{e}")
def reset_entries(self):
"""
Resets the entry fields after playback.
"""
self.video_entry.delete(0, tk.END)
self.audio_entry.delete(0, tk.END)
# Optionally, clear the MPC-HC path if needed
# self.mpc_entry.delete(0, tk.END)
# self.mpc_hc_path = ""
The error you're encountering is due to the use of an unrecognized command-line switch /AudioFile with MPC-HC (Media Player Classic - Home Cinema). According to the MPC-HC documentation, the correct switch for loading an additional audio file is /dub.
Here's a step-by-step guide to resolving the issue and ensuring your application supports various audio formats like FLAC, DSD, OGG, and Opus:
1. Update the Command-Line Switch
Current Command:
C:/Program Files (x86)/K-Lite Codec Pack/MPC-HC64/mpc-hc64.exe C:/Users/picky/Downloads/d1b9d78c.mp4 /AudioFile E:/media/Songs/Favorite Tracks 2/02 - Calvin Harris, Sam Smith - Desire.flac
Issue: /AudioFile is not a recognized switch in MPC-HC.
Solution: Replace /AudioFile with /dub, which is the correct switch for loading an additional audio file.
Updated Command:
C:/Program Files (x86)/K-Lite Codec Pack/MPC-HC64/mpc-hc64.exe "C:/Users/picky/Downloads/d1b9d78c.mp4" /dub "E:/media/Songs/Favorite Tracks 2/02 - Calvin Harris, Sam Smith - Desire.flac"
2. Modify Your Python Script
In your Python script, specifically in the play_media method, replace the /AudioFile switch with /dub. Here's the updated function:
def play_media(self):
"""
Constructs and executes the command to play the media using MPC-HC.
"""
if not self.validate_paths():
return
try:
# Construct command
cmd = [f'"{self.mpc_hc_path}"', f'"{self.video_path}"']
if self.audio_path:
# Use the /dub option to load external audio
cmd.extend(['/dub', f'"{self.audio_path}"'])
# Join the command list into a single string
cmd_str = ' '.join(cmd)
# Debug: print the command
print(f"Executing command: {cmd_str}")
# Start the process
subprocess.Popen(cmd_str, shell=True)
# Optionally, reset the entries after playing
# self.reset_entries()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while trying to play media:\n{e}")
3. Ensure Codec Support
To handle various audio formats like FLAC, DSD, OGG, and Opus, ensure that your K-Lite Codec Pack installation includes support for these codecs. K-Lite typically includes comprehensive codec support, but it's good to verify:
-
Open K-Lite Codec Pack Settings:
- Navigate to the K-Lite Codec Pack's configuration tool.
-
Check Audio Codecs:
- Ensure that the following codecs are enabled:
- FLAC: For FLAC files.
- DSD: If supported, for DSD files.
- Opus: For Opus files.
- Ogg Vorbis: For OGG files.
-
Install Additional Codecs If Necessary:
- If any codec is missing, consider reinstalling K-Lite Codec Pack with all necessary components or manually adding the required codecs.
4. Test the Updated Application
After making the above changes:
-
Run Your Application:
- Launch the GUI and select the MPC-HC executable, video file, and external audio file.
-
Click Play:
- The media should now play with the external audio file without errors.
-
Verify Audio Synchronization:
- Ensure the external audio is in sync with the video. If there are synchronization issues, you might need to adjust using the
/dubdelay switch.
5. Optional Enhancements
6. Final Updated Code Snippet
Here's the complete updated play_media method within your existing script:
def play_media(self):
"""
Constructs and executes the command to play the media using MPC-HC.
"""
if not self.validate_paths():
return
try:
# Construct command
cmd = [f'"{self.mpc_hc_path}"', f'"{self.video_path}"']
if self.audio_path:
# Use the /dub option to load external audio
cmd.extend(['/dub', f'"{self.audio_path}"'])
# Join the command list into a single string
cmd_str = ' '.join(cmd)
# Debug: print the command
print(f"Executing command: {cmd_str}")
# Start the process
subprocess.Popen(cmd_str, shell=True)
# Optionally, reset the entries after playing
# self.reset_entries()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while trying to play media:\n{e}")
7. Reference
For more information on MPC-HC command-line switches, refer to the MPC-HC Official Documentation.
By replacing the incorrect /AudioFile switch with /dub and ensuring that the necessary codecs are installed, your application should function as expected, supporting various audio formats seamlessly.