Skip to content

Instantly share code, notes, and snippets.

@basperheim
Last active February 24, 2024 08:03
Show Gist options
  • Save basperheim/52f311da8e2b779b8bc6fd3c8c260bcd to your computer and use it in GitHub Desktop.
Save basperheim/52f311da8e2b779b8bc6fd3c8c260bcd to your computer and use it in GitHub Desktop.
Reduce size of MKV files in current dir using FFmpeg and Python
import os
import subprocess
DRY_RUN = False
# Function to find all mkv files in the current directory
def find_mkv_files():
mkv_files = []
for file in os.listdir('.'):
if file.endswith('.mkv'):
mkv_files.append(file)
return mkv_files
# Function to execute FFmpeg command on each mkv file
def process_files(mkv_files):
for input_file in mkv_files:
if '.reduced.' in input_file:
print("\n\x1b[33mSkipping:", input_file, "\x1b[37m")
continue
else:
print("\n\x1b[32mEncoding:", input_file, "\x1b[37m")
output_file = os.path.splitext(input_file)[0] + '.reduced.mkv'
ffmpeg_command = [
'ffmpeg',
'-y',
'-hide_banner',
'-i', input_file,
'-c:v', 'libx264',
'-preset', 'slow',
'-crf', '22',
'-c:a', 'copy',
'-c:s', 'copy',
'-threads', '2',
output_file
]
if not DRY_RUN:
# Execute FFmpeg command as a subprocess
subprocess.run(ffmpeg_command)
else:
print(ffmpeg_command)
# Find all mkv files in the current directory
mkv_files = find_mkv_files()
if mkv_files:
# Process each mkv file
process_files(mkv_files)
print("Conversion completed successfully.")
else:
print("No .mkv files found in the current directory.")
@basperheim
Copy link
Author

Initial commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment