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

Added encoding optional parameter to subtitle file loading feature #1043

Merged
merged 6 commits into from
Apr 9, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support removed for Python versions 2.7, 3.4 & 3.5 [#1103, #1106]

### Added <!-- for new features -->
- Optional `encoding` parameter in `SubtitlesClip` [#1043]

### Changed <!-- for changes in existing functionality -->

Expand Down
13 changes: 9 additions & 4 deletions moviepy/video/tools/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class SubtitlesClip(VideoClip):

subtitles
Either the name of a file, or a list
encoding
Optional, specifies srt file encoding.
Any standard Python encoding is allowed (listed at https://docs.python.org/3.8/library/codecs.html#standard-encodings)

Examples
=========
Expand All @@ -28,18 +31,19 @@ class SubtitlesClip(VideoClip):
>>> from moviepy.video.io.VideoFileClip import VideoFileClip
>>> generator = lambda txt: TextClip(txt, font='Georgia-Regular', fontsize=24, color='white')
>>> sub = SubtitlesClip("subtitles.srt", generator)
>>> sub = SubtitlesClip("subtitles.srt", generator, encoding='utf-8')
>>> myvideo = VideoFileClip("myvideo.avi")
>>> final = CompositeVideoClip([clip, subtitles])
>>> final.write_videofile("final.mp4", fps=myvideo.fps)

"""

def __init__(self, subtitles, make_textclip=None):
def __init__(self, subtitles, make_textclip=None, encoding=None):

VideoClip.__init__(self, has_constant_size=False)

if isinstance(subtitles, str):
subtitles = file_to_subtitles(subtitles)
subtitles = file_to_subtitles(subtitles, encoding=encoding)

# subtitles = [(map(cvsecs, tt),txt) for tt, txt in subtitles]
self.subtitles = subtitles
Expand Down Expand Up @@ -144,18 +148,19 @@ def write_srt(self, filename):
f.write(str(self))


def file_to_subtitles(filename):
def file_to_subtitles(filename, encoding=None):
""" Converts a srt file into subtitles.

The returned list is of the form ``[((ta,tb),'some text'),...]``
and can be fed to SubtitlesClip.

Only works for '.srt' format for the moment.
"""

times_texts = []
current_times = None
current_text = ""
with open(filename, "r") as f:
with open(filename, "r", encoding=encoding) as f:
for line in f:
times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)
if times:
Expand Down