Skip to content

Instantly share code, notes, and snippets.

@tolgahanakgun
Created March 11, 2025 22:02
Show Gist options
  • Save tolgahanakgun/35124e5538b3623a843cbf6686d921e6 to your computer and use it in GitHub Desktop.
Save tolgahanakgun/35124e5538b3623a843cbf6686d921e6 to your computer and use it in GitHub Desktop.
H264 video transcoder
import os
import subprocess
import time
# This script is intended to transcode *.mkv files.
# It uses docker image from https://github.com/lisamelton/other_video_transcoding .
# It transcodes the videos inside the rootdir to max quality H264 mkv files.
# The docker container utilizes intel quick sync hardware encoder.
# This can be run inside a tmux session overnight.
# there must be directory called "ripped" under the rootdir to store the transcoded files
rootdir = "/storage/data/filebrowser/media/series/[A] Animeler"
# Loop through all files in the directory
for filename in os.listdir(rootdir):
# Check if the file ends with .mkv
if not filename.endswith(".mkv"):
continue
# atore the execution start time
before_run = int(time.time())
# the cpu number can be limited with --cpuset-cpus parameter
command = ["sudo", "docker", "run", # "--cpuset-cpus", "0",
"--rm", "--device", "/dev/dri:/dev/dri",
"-v", os.fsencode(f"{rootdir}:/src"),
"-v", os.fsencode(f"{rootdir}/ripped:/out"),
"-w", "/out", "git.skj.dev/container/other-transcode:qsv",
"--add-audio", "all", "--add-subtitle", "all", "--10-bit",
os.fsencode(f"/src/{filename}")]
proc = subprocess.Popen(
command, universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while proc.poll() is None:
print(proc.stderr.readline().strip())
# check the execution time
# if the execution time is more than 5 sec then sleep 10 min
# the gpu needs some time to cool down in fanless setup
if (int(time.time()) - before_run) > 5:
time.sleep(600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment