feat: Enhanced functionalities of video_manage_audio.py to support removing and keeping tracks.
- Refactored audio processing logic to handle both 'remove' and 'keep' commands. - Added a command-line argument to specify whether to remove a specific audio track or keep only a selected audio track while preserving other streams. - Improved handling of video file processing in directories with recursive function. - Streamlined error handling for more informative output during ffmpeg execution. - Updated argument parsing with command options and enhanced autocomplete support.
This commit is contained in:
parent
4c76cbfe76
commit
53f483b574
@ -1,11 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
# video_manage_audio.py
|
||||
|
||||
import os
|
||||
import argparse
|
||||
import argcomplete
|
||||
import colorama
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
# Initialize colorama for colored output in the terminal
|
||||
colorama.init()
|
||||
|
||||
creset = colorama.Fore.RESET
|
||||
@ -15,54 +17,86 @@ cgreen = colorama.Fore.GREEN
|
||||
cred = colorama.Fore.RED
|
||||
|
||||
|
||||
# Function to remove audio track from a video using ffmpeg
|
||||
def remove_audio_track(file_path, track):
|
||||
def process_audio(file_path, track, command):
|
||||
"""
|
||||
Modify audio tracks of a video file based on the given command.
|
||||
- 'remove': Remove the specified audio track while keeping everything else.
|
||||
- 'keep': Keep only the specified audio track and remove all others.
|
||||
The function preserves video, subtitles, and metadata.
|
||||
"""
|
||||
print(f"{cgreen}Processing file: {file_path}{creset}")
|
||||
output_file = f"{os.path.splitext(file_path)[0]}_no_audio{os.path.splitext(file_path)[1]}"
|
||||
# ffmpeg command to remove the specified audio track and keep other streams
|
||||
command = [
|
||||
"ffmpeg", "-i", file_path, "-map", "0", "-map", f"-0:a:{track}", "-c",
|
||||
"copy", output_file
|
||||
output_file = f"{os.path.splitext(file_path)[0]}_{command}_audio{os.path.splitext(file_path)[1]}"
|
||||
|
||||
# Construct ffmpeg command based on user choice
|
||||
if command == "remove":
|
||||
# Remove only the specified audio track while keeping all other streams
|
||||
ffmpeg_command = [
|
||||
"ffmpeg", "-i", file_path, "-map", "0", "-map", f"-0:a:{track}",
|
||||
"-c", "copy", output_file
|
||||
]
|
||||
elif command == "keep":
|
||||
# Keep only the specified audio track while preserving video, subtitles, and metadata
|
||||
ffmpeg_command = [
|
||||
"ffmpeg", "-i", file_path, "-map", "0:v", "-map", f"0:a:{track}",
|
||||
"-map", "0:s?", "-map", "0:t?", "-c", "copy", output_file
|
||||
]
|
||||
else:
|
||||
print(f"{cred}Invalid command: {command}{creset}")
|
||||
return
|
||||
|
||||
try:
|
||||
result = subprocess.run(command,
|
||||
# Execute the ffmpeg command and capture output
|
||||
result = subprocess.run(ffmpeg_command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True)
|
||||
|
||||
if result.returncode == 0:
|
||||
print(
|
||||
f"{ccyan}Audio track {track} removed. Output saved to {output_file}{creset}\n"
|
||||
f"{ccyan}Audio processing complete. Output saved to {output_file}{creset}\n"
|
||||
)
|
||||
else:
|
||||
print(
|
||||
f"{cred}Command failed with return code {result.returncode}{creset}\n"
|
||||
)
|
||||
print(f"{cred}Error output:", result.stderr, f"{creset}\n")
|
||||
print(f"{cred}Error output:\n{result.stderr}{creset}\n")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"{cred}Error removing audio track: {e}{creset}\n")
|
||||
print(f"{cred}Error processing audio: {e}{creset}\n")
|
||||
|
||||
|
||||
# Function to recursively process videos in a directory
|
||||
def process_directory(dir_path, track):
|
||||
def process_directory(dir_path, track, command):
|
||||
"""
|
||||
Recursively processes all video files in the specified directory.
|
||||
Applies the chosen audio modification (remove or keep) to each file.
|
||||
"""
|
||||
for root, _, files in os.walk(dir_path):
|
||||
for file in files:
|
||||
if file.endswith((".mp4", ".mkv", ".avi",
|
||||
".mov")): # Add more formats as needed
|
||||
if file.endswith((".mp4", ".mkv", ".avi", ".mov")):
|
||||
file_path = os.path.join(root, file)
|
||||
remove_audio_track(file_path, track)
|
||||
process_audio(file_path, track, command)
|
||||
|
||||
|
||||
def main():
|
||||
# Set up argument parser
|
||||
"""
|
||||
Main function to parse command-line arguments and initiate the audio processing.
|
||||
"""
|
||||
|
||||
# Create argument parser
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Remove audio track from video files.")
|
||||
description="Manage audio tracks in video files.")
|
||||
|
||||
# Define command line arguments
|
||||
# Add a positional argument for the command
|
||||
parser.add_argument('command',
|
||||
choices=['remove', 'keep'],
|
||||
help='Command to run')
|
||||
|
||||
# Add other arguments with both short and long options, including defaults
|
||||
parser.add_argument("--track",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Audio track index to remove (default is 0).")
|
||||
help="Audio track index (default is 0).")
|
||||
parser.add_argument("--file",
|
||||
type=str,
|
||||
help="Path to a specific video file.")
|
||||
@ -72,23 +106,23 @@ def main():
|
||||
default=os.getcwd(),
|
||||
help="Directory to process (default is current directory).")
|
||||
|
||||
# Enable autocomplete for argparse
|
||||
# Enable autocomplete for command-line arguments
|
||||
argcomplete.autocomplete(parser)
|
||||
|
||||
# Parse command line arguments
|
||||
args = parser.parse_args()
|
||||
|
||||
# Process single file if provided
|
||||
# Process a single file if provided, otherwise process a directory
|
||||
if args.file:
|
||||
if os.path.isfile(args.file):
|
||||
remove_audio_track(args.file, args.track)
|
||||
process_audio(args.file, args.track, args.command)
|
||||
else:
|
||||
print(f"File {args.file} does not exist.")
|
||||
# Otherwise, process all files in the specified directory
|
||||
print(f"{cred}File {args.file} does not exist.{creset}")
|
||||
else:
|
||||
if os.path.isdir(args.dir):
|
||||
process_directory(args.dir, args.track)
|
||||
process_directory(args.dir, args.track, args.command)
|
||||
else:
|
||||
print(f"Directory {args.dir} does not exist.")
|
||||
print(f"{cred}Directory {args.dir} does not exist.{creset}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Loading…
Reference in New Issue
Block a user