2026-04-22 21:01:43 -04:00

118 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""
Audio Extractor - Extract and manage audio tracks from video files using ffmpeg
"""
import argparse
import sys
from pathlib import Path
from audio_extractor.cli import AudioExtractorCLI
def main():
parser = argparse.ArgumentParser(
description="Extract and manage audio tracks from video files",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Extract audio from a single video file
python main.py --extract "video.mp4" -o ./audio_output
# Extract audio from all videos in a folder
python main.py --extract "./videos" -o ./audio_output
# Add audio tracks to video (future feature)
python main.py --add "./audio_files" -i "./video_files" -o ./output
"""
)
# Create subcommands for better organization
subparsers = parser.add_subparsers(dest="command", help="Command to execute")
# Extract command
extract_parser = subparsers.add_parser("extract", help="Extract audio from video files")
extract_parser.add_argument(
"target",
type=str,
help="Path to video file or folder containing video files"
)
extract_parser.add_argument(
"-o", "--output",
type=str,
default="./audio_output",
help="Output folder for extracted audio files (default: ./audio_output)"
)
# Add command (future feature)
add_parser = subparsers.add_parser("add", help="Add audio tracks to video files")
add_parser.add_argument(
"target",
type=str,
help="Path to folder containing audio files to add"
)
add_parser.add_argument(
"-i", "--input",
type=str,
required=True,
help="Path to folder containing video files"
)
add_parser.add_argument(
"-o", "--output",
type=str,
default="./video_output",
help="Output folder for processed video files (default: ./video_output)"
)
add_parser.add_argument(
"--title",
type=str,
default=None,
help="Title/name for the added audio tracks"
)
# Also support old-style --extract flag for backwards compatibility
parser.add_argument(
"--extract",
type=str,
default=None,
metavar="TARGET",
help="(Legacy) Extract audio from video file or folder"
)
parser.add_argument(
"-o", "--output",
type=str,
dest="output",
default="./audio_output",
help="Output folder path"
)
args = parser.parse_args()
# Initialize CLI
cli = AudioExtractorCLI()
# Handle legacy --extract flag
if args.extract:
args.command = "extract"
args.target = args.extract
if not args.command:
parser.print_help()
sys.exit(1)
try:
if args.command == "extract":
cli.extract_audio(args.target, args.output)
elif args.command == "add":
cli.add_audio_tracks(args.target, args.input, args.output, args.title)
else:
parser.print_help()
sys.exit(1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()