Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing subtitle encode #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions mkv/attacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def parse_args():
return parser.parse_args()


def check_charset_utf(subtitle_abs_path):
def check_charset_iso88591(subtitle_abs_path):
result = subprocess.check_output(["file", "-i", subtitle_abs_path])
if b"utf-8" in result or b"us-ascii" in result:
if b"iso-8859-1" in result:
return True
return False

Expand All @@ -32,12 +32,16 @@ def unrar_file(_file, dir_path):
Archive(file_path_absolute).extractall(dir_path)


def _is_compress_file(_file):
return _file.endswith(".rar") or _file.endswith(".zip")


def attach(args):
for dirname, dirnames, _ in os.walk(args.mkvsource):
for subdirname in dirnames:
dir_path = os.path.join(dirname, subdirname)
for _file in os.listdir(dir_path):
if _file.endswith(".rar"):
if _is_compress_file(_file):
unrar_file(_file, dir_path)
if _file.endswith(".mkv"):
try:
Expand All @@ -46,8 +50,8 @@ def attach(args):
subtitle_path = mkv_path_absolute.replace(
".mkv", ".srt"
)
if check_charset_utf(subtitle_path):
print("converting subtitle to iso-8859-1")
if check_charset_iso88591(subtitle_path):
print("converting subtitle to utf")
iconv = "iconv -t UTF-8 -f ISO-8859-1 {} > {}.tmp"
iconv = iconv.format(subtitle_path, subtitle_path)
os.system(iconv)
Expand All @@ -63,7 +67,7 @@ def attach(args):
dir_path,
file_converted
)
cp_command = "cp " + converted_absolute_path + " " + args.hdddest
cp_command = "pv -p " + converted_absolute_path + " > " + args.hdddest + "/" + file_converted
print("copying to hdd")
os.system(cp_command)
except Exception as e:
Expand Down
6 changes: 3 additions & 3 deletions tests/attacher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def setUp(self):
def _join_absolute_path(self, path):
return os.path.join(self.current_dir, path)

def test_should_check_utf_8_success(self):
abs_path = self._join_absolute_path('tests/resource_tests/utf.srt')
self.assertTrue(attacher.check_charset_utf(abs_path))
def test_should_check_iso88591_success(self):
abs_path = self._join_absolute_path('tests/resource_tests/iso88591.srt')
self.assertTrue(attacher.check_charset_iso88591(abs_path))

def test_should_check_existing_subtitle(self):
abs_path = self._join_absolute_path(
Expand Down
1 change: 1 addition & 0 deletions tests/resource_tests/iso88591.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mkv-subtitle
1 change: 0 additions & 1 deletion tests/resource_tests/utf.srt

This file was deleted.