Files
2026-03-31 12:57:14 +07:00

60 lines
1.9 KiB
Python

import os
import subprocess
import glob
import sys
def trim_mkv_files(input_folder, trim_duration=0.375):
# Ensure FFmpeg is installed and ffmpeg is in PATH
ffmpeg_path = "ffmpeg" # Adjust path if ffmpeg is not in PATH
# Create output folder if it doesn't exist
output_folder = os.path.join(input_folder, "trimmed")
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Find all MKV files in the input folder
mkv_files = glob.glob(os.path.join(input_folder, "*.mkv"))
if not mkv_files:
print("No MKV files found in the specified folder.")
return
for mkv_file in mkv_files:
# Get the base filename and create output filename
base_name = os.path.basename(mkv_file)
output_file = os.path.join(output_folder, f"{base_name}")
# Construct ffmpeg command to trim first second using stream copy
command = [
ffmpeg_path,
"-i", mkv_file,
"-ss", str(trim_duration),
"-c", "copy",
"-map", "0",
output_file
]
try:
# Execute the command
result = subprocess.run(command, capture_output=True, text=True, check=True)
print(f"Successfully trimmed {base_name} -> {output_file}")
except subprocess.CalledProcessError as e:
print(f"Error processing {base_name}: {e.stderr}")
except FileNotFoundError:
print("Error: ffmpeg not found. Ensure FFmpeg is installed and in PATH.")
break
if __name__ == "__main__":
# Check if folder path is provided as command-line argument
if len(sys.argv) != 2:
print("Usage: python trim_video.py /path/to/folder")
sys.exit(1)
input_folder = sys.argv[1]
# Validate folder path
if not os.path.isdir(input_folder):
print(f"Error: '{input_folder}' is not a valid folder path.")
sys.exit(1)
trim_mkv_files(input_folder)