From 754c533192b002add69f21bf2eeeb160ab9d8d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Fri, 25 Sep 2020 16:49:45 +0200 Subject: [PATCH] Initial commit. --- .prettierrc | 5 +++++ README.md | 8 +++++++ mkv_sub_fix/__init__.py | 1 + mkv_sub_fix/mkv_sub_fix.py | 27 ++++++++++++++++++++++++ pyproject.toml | 15 ++++++++++++++ tests/__init__.py | 0 tests/unit/__init__.py | 0 tests/unit/test_open_existing_file.py | 30 +++++++++++++++++++++++++++ 8 files changed, 86 insertions(+) create mode 100644 .prettierrc create mode 100644 README.md create mode 100644 mkv_sub_fix/__init__.py create mode 100644 mkv_sub_fix/mkv_sub_fix.py create mode 100644 pyproject.toml create mode 100644 tests/__init__.py create mode 100644 tests/unit/__init__.py create mode 100644 tests/unit/test_open_existing_file.py diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..e97ea1b --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "tabWidth": 2, + "useTabs": false, + "singleQuote": true +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..4ec62c1 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# mkv-sub-fix + +Easily assign language tags and descriptions to subtitle tracks in the Matroska container files. + +# How it works + +1. After running the program, it shows you a list of all subtitle tracks. +2. You go through each track, assigning it a language tag and optionally a description. diff --git a/mkv_sub_fix/__init__.py b/mkv_sub_fix/__init__.py new file mode 100644 index 0000000..267e0ae --- /dev/null +++ b/mkv_sub_fix/__init__.py @@ -0,0 +1 @@ +from .mkv_sub_fix import * \ No newline at end of file diff --git a/mkv_sub_fix/mkv_sub_fix.py b/mkv_sub_fix/mkv_sub_fix.py new file mode 100644 index 0000000..98a2d07 --- /dev/null +++ b/mkv_sub_fix/mkv_sub_fix.py @@ -0,0 +1,27 @@ +import subprocess +import os +import json + + +def isMkvpropeditAvailable(): + if subprocess.run("mkvpropedit"): + print("mkvpropedit exists.") + + +def getListOfSubtitles(filename): + if not os.path.exists(filename): + raise FileNotFoundError + result = subprocess.run( + ["mkvmerge", "-i", filename, "-F", "json"], capture_output=True + ) + if result.returncode == 0: + info = json.loads(result.stdout) + return { + track["id"]: track["properties"]["language"] + for track in info["tracks"] + if track["type"] == "subtitles" + } + + +if __name__ == "__main__": + getListOfSubtitles("/home/lukas/Downloads/test5.mkv") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..ecdb1ab --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "mkv-sub-fix" +version = "0.1.0" +description = "Easily assign language tags to subtitles in .mkv files." +authors = ["Lukas Kucharczyk"] +license = "GPL-3.0-only" + +[tool.poetry.dependencies] +python = "^3.8" + +[tool.poetry.dev-dependencies] + +[build-system] +requires = ["poetry>=0.12"] +build-backend = "poetry.masonry.api" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_open_existing_file.py b/tests/unit/test_open_existing_file.py new file mode 100644 index 0000000..a0c70c4 --- /dev/null +++ b/tests/unit/test_open_existing_file.py @@ -0,0 +1,30 @@ +from mkv_sub_fix import * +import unittest + + +class TestFileOpening(unittest.TestCase): + def testOpenExistingFile(self): + getListOfSubtitles("/home/lukas/Downloads/test5.mkv") + + def testOpeningNonExistingFile(self): + with self.assertRaises(FileNotFoundError): + getListOfSubtitles("/nonexistent") + + def testReturnListofSubtitles(self): + self.assertEqual( + getListOfSubtitles("/home/lukas/Downloads/test5.mkv"), + { + 2: "eng", + 3: "hun", + 4: "ger", + 5: "fre", + 6: "spa", + 7: "ita", + 9: "jpn", + 10: "und", + }, + ) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file