Compare commits
2 Commits
330f1edf42
...
642f6d90db
| Author | SHA1 | Date | |
|---|---|---|---|
| 642f6d90db | |||
| 98b757a9b1 |
@ -15,6 +15,7 @@ This repository is structured into several key directories:
|
|||||||
- `video_autoreduce_rename.py`: A script for automated renaming of video files post resolution reduction.
|
- `video_autoreduce_rename.py`: A script for automated renaming of video files post resolution reduction.
|
||||||
- `video_manage_audio.py`: A script for removing audio from video files.
|
- `video_manage_audio.py`: A script for removing audio from video files.
|
||||||
- `video_manage_subtitles.py`: A script for removing subtitles from video files.
|
- `video_manage_subtitles.py`: A script for removing subtitles from video files.
|
||||||
|
- `video_repackage_mkv.py`: Recursively repackage non-MKV video files into Matroska containers.
|
||||||
|
|
||||||
- **notes/**: A collection of markdown files containing notes on various topics, including:
|
- **notes/**: A collection of markdown files containing notes on various topics, including:
|
||||||
- `brother.md`: Brother printer setup and troubleshooting on Linux.
|
- `brother.md`: Brother printer setup and troubleshooting on Linux.
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import argcomplete
|
import argcomplete
|
||||||
import colorama
|
import colorama
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@ -17,39 +18,153 @@ cgreen = colorama.Fore.GREEN
|
|||||||
cred = colorama.Fore.RED
|
cred = colorama.Fore.RED
|
||||||
|
|
||||||
|
|
||||||
def process_audio(file_path, track, command):
|
def get_audio_metadata(file_path):
|
||||||
"""
|
"""
|
||||||
Modify audio tracks of a video file based on the given command.
|
Uses ffprobe to extract audio stream information.
|
||||||
- 'remove': Remove the specified audio track while keeping everything else.
|
Returns a list of dicts with track details.
|
||||||
- 'keep': Keep only the specified audio track and remove all others.
|
|
||||||
The function preserves video, subtitles, and metadata.
|
|
||||||
"""
|
"""
|
||||||
|
abs_file_path = os.path.abspath(file_path)
|
||||||
|
ffprobe_command = [
|
||||||
|
"ffprobe",
|
||||||
|
"-v",
|
||||||
|
"error",
|
||||||
|
"-select_streams",
|
||||||
|
"a",
|
||||||
|
"-show_entries",
|
||||||
|
"stream=index,codec_name:stream_tags=language,title",
|
||||||
|
"-of",
|
||||||
|
"json",
|
||||||
|
abs_file_path,
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
ffprobe_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.returncode != 0:
|
||||||
|
return None
|
||||||
|
metadata = json.loads(result.stdout)
|
||||||
|
return metadata.get("streams", [])
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def list_audio_tracks(file_path):
|
||||||
|
"""Displays all audio tracks in a video file."""
|
||||||
|
print(f"{ccyan}Analyzing tracks for: {file_path}{creset}")
|
||||||
|
streams = get_audio_metadata(file_path)
|
||||||
|
|
||||||
|
if streams is None:
|
||||||
|
print(f"{cred} Failed to read file metadata.{creset}\n")
|
||||||
|
return
|
||||||
|
if not streams:
|
||||||
|
print(f"{cyellow} No audio tracks found in this file.{creset}\n")
|
||||||
|
return
|
||||||
|
|
||||||
|
for audio_idx, stream in enumerate(streams):
|
||||||
|
abs_idx = stream.get("index")
|
||||||
|
codec = stream.get("codec_name", "unknown")
|
||||||
|
tags = stream.get("tags", {})
|
||||||
|
lang = tags.get("language", "und")
|
||||||
|
title = tags.get("title", "No Title")
|
||||||
|
|
||||||
|
print(
|
||||||
|
f" {cgreen}[Audio Track {audio_idx}]{creset} "
|
||||||
|
f"Absolute Stream Index: {abs_idx} | "
|
||||||
|
f"Codec: {codec} | Lang: {lang} | Title: {title}"
|
||||||
|
)
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_track_target(streams, target):
|
||||||
|
"""
|
||||||
|
Resolves a track target (either an integer index string like '1'
|
||||||
|
or a language code string like 'eng') to a relative audio track index.
|
||||||
|
Returns None if no match is found.
|
||||||
|
"""
|
||||||
|
# Case 1: Target is an explicit number (e.g., "0", "1")
|
||||||
|
if target.isdigit():
|
||||||
|
idx = int(target)
|
||||||
|
if 0 <= idx < len(streams):
|
||||||
|
return idx
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Case 2: Target is a language code (e.g., "eng", "rus")
|
||||||
|
for audio_idx, stream in enumerate(streams):
|
||||||
|
lang = stream.get("tags", {}).get("language", "").lower()
|
||||||
|
if lang == target.lower():
|
||||||
|
return audio_idx
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def process_audio(file_path, track_target, command):
|
||||||
|
"""Modify or list audio tracks of a video file."""
|
||||||
|
if command == "list":
|
||||||
|
list_audio_tracks(file_path)
|
||||||
|
return
|
||||||
|
|
||||||
|
streams = get_audio_metadata(file_path)
|
||||||
|
if not streams:
|
||||||
|
print(f"{cred}Skipping {file_path}: Could not parse audio streams.{creset}\n")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Dynamic target resolution (converts 'eng' or '1' to the proper track index)
|
||||||
|
resolved_track = resolve_track_target(streams, track_target)
|
||||||
|
|
||||||
|
if resolved_track is None:
|
||||||
|
print(
|
||||||
|
f"{cyellow}Skipping {file_path}: Target audio track/language '{track_target}' not found.{creset}\n"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
print(f"{cgreen}Processing file: {file_path}{creset}")
|
print(f"{cgreen}Processing file: {file_path}{creset}")
|
||||||
|
print(
|
||||||
|
f"-> Target resolved to Audio Track {resolved_track} based on '{track_target}'"
|
||||||
|
)
|
||||||
|
|
||||||
output_file = f"{os.path.splitext(file_path)[0]}_{command}_audio{os.path.splitext(file_path)[1]}"
|
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":
|
if command == "remove":
|
||||||
# Remove only the specified audio track while keeping all other streams
|
# Remove only the specified audio track while keeping all other streams
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
"ffmpeg", "-i", file_path, "-map", "0", "-map", f"-0:a:{track}",
|
"ffmpeg",
|
||||||
"-c", "copy", output_file
|
"-i",
|
||||||
|
file_path,
|
||||||
|
"-map",
|
||||||
|
"0",
|
||||||
|
"-map",
|
||||||
|
f"-0:a:{resolved_track}",
|
||||||
|
"-c",
|
||||||
|
"copy",
|
||||||
|
output_file,
|
||||||
]
|
]
|
||||||
elif command == "keep":
|
elif command == "keep":
|
||||||
# Keep only the specified audio track while preserving video, subtitles, and metadata
|
# Keep only the specified audio track while preserving video, subtitles, and metadata
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
"ffmpeg", "-i", file_path, "-map", "0:v", "-map", f"0:a:{track}",
|
"ffmpeg",
|
||||||
"-map", "0:s?", "-map", "0:t?", "-c", "copy", output_file
|
"-i",
|
||||||
|
file_path,
|
||||||
|
"-map",
|
||||||
|
"0:v",
|
||||||
|
"-map",
|
||||||
|
f"0:a:{resolved_track}",
|
||||||
|
"-map",
|
||||||
|
"0:s?",
|
||||||
|
"-map",
|
||||||
|
"0:t?",
|
||||||
|
"-c",
|
||||||
|
"copy",
|
||||||
|
output_file,
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
print(f"{cred}Invalid command: {command}{creset}")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Execute the ffmpeg command and capture output
|
result = subprocess.run(
|
||||||
result = subprocess.run(ffmpeg_command,
|
ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
||||||
stdout=subprocess.PIPE,
|
)
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
text=True)
|
|
||||||
|
|
||||||
if result.returncode == 0:
|
if result.returncode == 0:
|
||||||
print(
|
print(
|
||||||
@ -61,20 +176,17 @@ def process_audio(file_path, track, command):
|
|||||||
)
|
)
|
||||||
print(f"{cred}Error output:\n{result.stderr}{creset}\n")
|
print(f"{cred}Error output:\n{result.stderr}{creset}\n")
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
except Exception as e:
|
||||||
print(f"{cred}Error processing audio: {e}{creset}\n")
|
print(f"{cred}Error processing audio: {e}{creset}\n")
|
||||||
|
|
||||||
|
|
||||||
def process_directory(dir_path, track, command):
|
def process_directory(dir_path, track_target, command):
|
||||||
"""
|
"""Recursively processes all video files in the specified directory."""
|
||||||
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 root, _, files in os.walk(dir_path):
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.endswith((".mp4", ".mkv", ".avi", ".mov")):
|
if file.endswith((".mp4", ".mkv", ".avi", ".mov")):
|
||||||
file_path = os.path.join(root, file)
|
file_path = os.path.join(root, file)
|
||||||
process_audio(file_path, track, command)
|
process_audio(file_path, track_target, command)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -82,37 +194,31 @@ def main():
|
|||||||
Main function to parse command-line arguments and initiate the audio processing.
|
Main function to parse command-line arguments and initiate the audio processing.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Create argument parser
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Manage audio tracks in video files.")
|
description="Manage or list audio tracks in video files dynamically."
|
||||||
|
)
|
||||||
|
|
||||||
# Define command line arguments
|
parser.add_argument(
|
||||||
# Add a positional argument for the command
|
"command", choices=["remove", "keep", "list"], help="Command to run"
|
||||||
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(
|
||||||
parser.add_argument("-t",
|
"-t",
|
||||||
"--track",
|
"--track",
|
||||||
type=int,
|
type=str,
|
||||||
default=0,
|
default="0",
|
||||||
help="Audio track index (default is 0).")
|
help="Audio track index (e.g., 0, 1) OR language ISO code (e.g., eng, rus). Default is '0'.",
|
||||||
parser.add_argument("-f",
|
)
|
||||||
"--file",
|
parser.add_argument("-f", "--file", type=str, help="Path to a specific video file.")
|
||||||
type=str,
|
|
||||||
help="Path to a specific video file.")
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--dir",
|
"--dir",
|
||||||
type=str,
|
type=str,
|
||||||
default=os.getcwd(),
|
default=os.getcwd(),
|
||||||
help="Directory to process (default is current directory).")
|
help="Directory to process (default is current directory).",
|
||||||
|
)
|
||||||
|
|
||||||
# Enable autocomplete for command-line arguments
|
|
||||||
argcomplete.autocomplete(parser)
|
argcomplete.autocomplete(parser)
|
||||||
|
|
||||||
# Parse command line arguments
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Process a single file if provided, otherwise process a directory
|
# Process a single file if provided, otherwise process a directory
|
||||||
|
|||||||
139
scripts/video_repackage_mkv.py
Executable file
139
scripts/video_repackage_mkv.py
Executable file
@ -0,0 +1,139 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""A utility script to repackage video files into MKV containers using FFmpeg.
|
||||||
|
|
||||||
|
This script recursively scans a specified directory for non-MKV video files
|
||||||
|
and uses FFmpeg to repackage them into '.mkv' containers. It explicitly maps
|
||||||
|
all streams (video, audio, subtitles, data) and preserves all metadata without
|
||||||
|
re-encoding. It includes options to automatically delete the source files upon
|
||||||
|
successful conversion.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Sequence
|
||||||
|
|
||||||
|
# Define the targeted non-MKV extensions (case-insensitive handling below)
|
||||||
|
TARGET_EXTENSIONS = {".mp4", ".m4v", ".flv", ".avi", ".mov", ".wmv"}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args(args: Sequence[str] | None = None) -> argparse.Namespace:
|
||||||
|
"""Parse command-line arguments for the repackaging utility.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
args: Optional sequence of strings to parse. If None, sys.argv is used.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
An argparse.Namespace object containing the parsed arguments.
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Recursively repackage non-MKV video files into MKV containers, preserving all streams and metadata."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-i",
|
||||||
|
"--input",
|
||||||
|
type=Path,
|
||||||
|
default=Path.cwd(),
|
||||||
|
help="Path to the root directory to scan for video files (default: current working directory).",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-d",
|
||||||
|
"--delete",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Delete the original video files upon successful conversion.",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Enable argcomplete support if the module is available
|
||||||
|
try:
|
||||||
|
import argcomplete
|
||||||
|
|
||||||
|
argcomplete.autocomplete(parser)
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return parser.parse_args(args)
|
||||||
|
|
||||||
|
|
||||||
|
def repackage_to_mkv(input_dir: Path, delete_source: bool) -> None:
|
||||||
|
"""Recursively scan the input directory and repackage non-MKV videos to MKV.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input_dir: The root Path directory to scan.
|
||||||
|
delete_source: If True, deletes the original video file after a
|
||||||
|
successful FFmpeg operation.
|
||||||
|
"""
|
||||||
|
if not input_dir.is_dir():
|
||||||
|
print(f"Error: The directory '{input_dir}' does not exist.")
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"Scanning '{input_dir}' recursively for video files...")
|
||||||
|
|
||||||
|
video_files = [
|
||||||
|
p
|
||||||
|
for p in input_dir.rglob("*")
|
||||||
|
if p.is_file() and p.suffix.lower() in TARGET_EXTENSIONS
|
||||||
|
]
|
||||||
|
|
||||||
|
if not video_files:
|
||||||
|
print("No matching video files found.")
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"Found {len(video_files)} files to process.\n")
|
||||||
|
|
||||||
|
for file_path in video_files:
|
||||||
|
output_path = file_path.with_suffix(".mkv")
|
||||||
|
|
||||||
|
if output_path.exists():
|
||||||
|
print(
|
||||||
|
f"Warning: Destination {output_path.name} already exists. Skipping to avoid accidental overwrite."
|
||||||
|
)
|
||||||
|
print("-" * 40)
|
||||||
|
continue
|
||||||
|
|
||||||
|
cmd = [
|
||||||
|
"ffmpeg",
|
||||||
|
"-i",
|
||||||
|
str(file_path),
|
||||||
|
"-map",
|
||||||
|
"0",
|
||||||
|
"-c",
|
||||||
|
"copy",
|
||||||
|
"-map_metadata",
|
||||||
|
"0",
|
||||||
|
str(output_path),
|
||||||
|
"-loglevel",
|
||||||
|
"error",
|
||||||
|
"-y",
|
||||||
|
]
|
||||||
|
|
||||||
|
# Display the relative path from the input directory so it's easier to track progress
|
||||||
|
relative_display = file_path.relative_to(input_dir)
|
||||||
|
print(f"Processing: {relative_display} -> {output_path.name}...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.run(cmd, check=True)
|
||||||
|
print(f"Successfully converted: {file_path.name}")
|
||||||
|
|
||||||
|
if delete_source:
|
||||||
|
file_path.unlink()
|
||||||
|
print(f"Deleted original file: {file_path.name}")
|
||||||
|
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
print(f"Failed to convert: {file_path.name}")
|
||||||
|
except OSError as e:
|
||||||
|
print(f"System error processing {file_path.name}: {e}")
|
||||||
|
|
||||||
|
print("-" * 40)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Main execution entry point."""
|
||||||
|
args = parse_args()
|
||||||
|
repackage_to_mkv(input_dir=args.input, delete_source=args.delete)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in New Issue
Block a user