Skip to content

Commit

Permalink
feat: implement fix_subtitles_path function and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Jan 22, 2025
1 parent 5e21000 commit db38792
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/transcribe_anything/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,15 @@ def fix_subtitles_path(_path: str) -> str:
# On Windows, ffmpeg 5 requires the path to be escaped.
# For example, "C:\Users\user\file.srt" should be "C\\:/\Users/\user/\file.srt".
# See https://stackoverflow.com/questions/60440793/how-can-i-use-windows-absolute-paths-with-the-movie-filter-on-ffmpeg
path = Path(_path)
# get the C:\ part
drive = path.drive
# get the \Users\user\file.srt part
try:
path = path.relative_to(drive)
except ValueError:
pass
drive_fixed = str(drive).replace(":", "\\\\:")
new_token = "/\\"
old_token = "\\"
path_fixed = str(path).replace(old_token, new_token)
out_path = drive_fixed + path_fixed
return out_path
# Replace backslashes with '/\' and add an extra backslash at the start of the drive letter
p = Path(_path).absolute()
# Handle drive letter specially
drive = p.drive
parts = p.parts
out: str = drive.replace(":", "") + "\\\\:"
for part in parts[1:]:
out += "/\\" + part
return out


def get_video_name_from_url(url: str) -> str:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_fix_subtitles_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Tests transcribe_anything
"""

import os
import platform
import unittest

from transcribe_anything.api import fix_subtitles_path

HERE = os.path.abspath(os.path.dirname(__file__))

_IS_WINDOWS = platform.system() == "Windows"


class FixSubtitlesTester(unittest.TestCase):
"""Tester for transcribe anything."""

@unittest.skipIf(not _IS_WINDOWS, "Test only works on Windows")
def test_local_file(self) -> None:
"""Check that the command works on a local file."""
path = r"C:\Users\Toshiba\Pictures\test vcp\shopi.mp4"
fixed_path = fix_subtitles_path(path)
expected_path = r"C\\:/\Users/\Toshiba/\Pictures/\test vcp/\shopi.mp4"
self.assertEqual(fixed_path, expected_path)


if __name__ == "__main__":
unittest.main()

0 comments on commit db38792

Please sign in to comment.