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

add a subtitle test #495

Merged
10 commits merged into from Mar 14, 2017
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
5 changes: 2 additions & 3 deletions moviepy/video/tools/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ def __init__(self, subtitles, make_textclip=None):
if isinstance( subtitles, str):
subtitles = file_to_subtitles(subtitles)

subtitles = [(map(cvsecs, tt),txt) for tt, txt in subtitles]
#subtitles = [(map(cvsecs, tt),txt) for tt, txt in subtitles]
self.subtitles = subtitles
self.textclips = dict()

if make_textclip is None:

make_textclip = lambda txt: TextClip(txt, font='Georgia-Bold',
fontsize=24, color='white',
stroke_color='black', stroke_width=0.5)
Expand Down Expand Up @@ -157,7 +156,7 @@ def file_to_subtitles(filename):
for line in lines:
times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)
if times != []:
current_times = map(cvsecs, times)
current_times = list(map(cvsecs, times))
elif line.strip() == '':
times_texts.append((current_times, current_text.strip('\n')))
current_times, current_text = None, ""
Expand Down
4 changes: 3 additions & 1 deletion tests/download_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def download_youtube_video(youtube_id, filename):
# FYI.. travis-ci doesn't like youtube-dl
download_url(youtube_id, filename)


def download():
if not os.path.exists("media"):
os.mkdir("media")
Expand All @@ -31,3 +30,6 @@ def download():

download_url("https://github.com/earney/moviepy_media/raw/master/tests/sounds/crunching.mp3",
"media/crunching.mp3")

download_url("https://raw.githubusercontent.com/earney/moviepy_media/master/tests/subtitles/subtitles1.srt",
"media/subtitles1.srt")
13 changes: 7 additions & 6 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
from moviepy.editor import *
import moviepy.video.tools.cuts as cuts

import os
import sys
sys.path.append("tests")
import download_media
from test_helper import PYTHON_VERSION, TMP_DIR, TRAVIS

def test_download_media(capsys):
with capsys.disabled():
download_media.download()

def test_matplotlib():
#for now, python 3.5 installs a version of matplotlib that complains
#about $DISPLAY variable, so lets just ignore for now.
if sys.version_info < (3,4):
if PYTHON_VERSION in ('2.7', '3.3'):
return

if sys.version_info.major == 3 and sys.version_info.minor == 5:
#for now, python 3.5 installs a version of matplotlib that complains
#about $DISPLAY variable, so lets just ignore for now.
if PYTHON_VERSION == '3.5' and TRAVIS:
return

import matplotlib.pyplot as plt
Expand All @@ -37,8 +39,7 @@ def make_frame(t):
return mplfig_to_npimage(fig)

animation = VideoClip(make_frame, duration=duration)
animation.write_gif('/tmp/matplotlib.gif', fps=20)

animation.write_gif(os.path.join(TMP_DIR, 'matplotlib.gif'), fps=20)

if __name__ == '__main__':
pytest.main()
7 changes: 7 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
import sys

TRAVIS=os.getenv("TRAVIS_PYTHON_VERSION") is not None
PYTHON_VERSION = "%s.%s" % (sys.version_info.major, sys.version_info.minor)
TMP_DIR="/tmp"

17 changes: 9 additions & 8 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

from moviepy.editor import *

# must have to work on travis-ci
import sys
sys.path.append("tests")
import download_media
from test_helper import PYTHON_VERSION, TMP_DIR, TRAVIS

def test_download_media(capsys):
with capsys.disabled():
Expand All @@ -28,7 +28,7 @@ def test_issue_285():

merged_clip = concatenate_videoclips([clip_1, clip_2, clip_3])
assert merged_clip.duration == 30

def test_issue_354():
clip = ImageClip("media/python_logo.png")

Expand All @@ -43,15 +43,16 @@ def test_issue_354():
def test_issue_359():
video = ColorClip((800, 600), color=(255,0,0)).set_duration(5)
video.fps=30
video.write_gif(filename="/tmp/issue_359.gif", tempfiles=True)
video.write_gif(filename=os.path.join(TMP_DIR, "issue_359.gif"),
tempfiles=True)

def test_issue_368():
import sys
if sys.version_info < (3,4): #matplotlib only supported in python >= 3.4
if PYTHON_VERSION in ('2.7', '3.3'): #matplotlib only supported in python >= 3.4
return

# $DISPLAY not set error in (travis-ci) python 3.5, so ignore 3.5 for now
if sys.version_info.major == 3 and sys.version_info.minor ==5:
#travis, python 3.5 matplotlib version has problems..
if PYTHON_VERSION == '3.5' and TRAVIS:
return

import numpy as np
Expand Down Expand Up @@ -85,7 +86,7 @@ def make_frame(t):
return mplfig_to_npimage(fig)

animation = VideoClip(make_frame,duration=2)
animation.write_gif("/tmp/svm.gif",fps=20)
animation.write_gif(os.path.join(TMP_DIR, "svm.gif"),fps=20)

def test_issue_407():
red = ColorClip((800, 600), color=(255,0,0)).set_duration(5)
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_issue_470():

#but this one should work..
subclip = audio_clip.subclip(t_start=6, t_end=8)
subclip.write_audiofile('/tmp/issue_470.wav', write_logfile=True)
subclip.write_audiofile(os.path.join(TMP_DIR, 'issue_470.wav'), write_logfile=True)


if __name__ == '__main__':
Expand Down
35 changes: 35 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
from moviepy.editor import *
import moviepy.video.tools.cuts as cuts

import os
import sys
sys.path.append("tests")
import download_media
from test_helper import TRAVIS, TMP_DIR


def test_download_media(capsys):
with capsys.disabled():
Expand All @@ -14,6 +17,38 @@ def test_cuts1():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").resize(0.2)
cuts.find_video_period(clip) == pytest.approx(0.966666666667, 0.0001)

def test_subtitles():
from moviepy.video.tools.subtitles import SubtitlesClip

red = ColorClip((800, 600), color=(255,0,0)).set_duration(10)
green = ColorClip((800, 600), color=(0,255,0)).set_duration(10)
blue = ColorClip((800, 600), color=(0,0,255)).set_duration(10)
myvideo = concatenate_videoclips([red,green,blue])
assert myvideo.duration == 30

#travis does not like TextClip.. so return for now..
#but allow regular users to still run the test below
if TRAVIS:
return

generator = lambda txt: TextClip(txt, font='Georgia-Regular',
size=(800,600), fontsize=24,
method='caption', align='South',
color='white')

subtitles = SubtitlesClip("media/subtitles1.srt", generator)
final = CompositeVideoClip([myvideo, subtitles])
final.to_videofile(os.path.join(TMP_DIR, "subtitles1.mp4"), fps=30)

data = [([0.0, 4.0], 'Red!'), ([5.0, 9.0], 'More Red!'),
([10.0, 14.0], 'Green!'), ([15.0, 19.0], 'More Green!'),
([20.0, 24.0], 'Blue'), ([25.0, 29.0], 'More Blue!')]

assert subtitles.subtitles == data

subtitles = SubtitlesClip(data, generator)
assert subtitles.subtitles == data


if __name__ == '__main__':
pytest.main()