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 exception on outside of boundaries subclip #2319

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add background radius to text clips

### Changed <!-- for changes in existing functionality -->
- Subclipping outside of clip boundaries now raise an exception

### Deprecated <!-- for soon-to-be removed features -->

Expand Down
8 changes: 8 additions & 0 deletions moviepy/Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ def subclipped(self, start_time=0, end_time=None):
end_time = self.duration + end_time

if end_time is not None:
# Allow a slight tolerance to account for rounding errors
if (self.duration is not None) and (end_time - self.duration > 0.00000001):
raise ValueError(
"end_time (%.02f) " % end_time
+ "should be smaller or equal to the clip's "
+ "duration (%.02f)." % self.duration
)

new_clip.duration = end_time - start_time
new_clip.end = new_clip.start + new_clip.duration

Expand Down
12 changes: 6 additions & 6 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,12 @@ def parse(self):
# for default streams, set their numbers globally, so it's
# easy to get without iterating all
if self._current_stream["default"]:
self.result[f"default_{stream_type_lower}_input_number"] = (
input_number
)
self.result[f"default_{stream_type_lower}_stream_number"] = (
stream_number
)
self.result[
f"default_{stream_type_lower}_input_number"
] = input_number
self.result[
f"default_{stream_type_lower}_stream_number"
] = stream_number

# exit chapter
if self._current_chapter:
Expand Down
1 change: 1 addition & 0 deletions tests/test_Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def test_clip_copy(copy_func):
(3, 3, None, ValueError), # start_time == duration
(3, 1, -1, 1), # negative end_time
(None, 1, -1, ValueError), # negative end_time for clip without duration
(1, 0, 2, ValueError), # end_time after video end should raise exception
),
)
def test_clip_subclip(duration, start_time, end_time, expected_duration):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,16 @@ def test_afterimage(util):

def test_add():
clip = VideoFileClip("media/fire2.mp4")
new_clip = clip[0:1] + clip[2:3.2]
assert new_clip.duration == 2.2
assert np.array_equal(new_clip[1.1], clip[2.1])
new_clip = clip[0:1] + clip[1.5:2]
assert new_clip.duration == 1.5
assert np.array_equal(new_clip[1.1], clip[1.6])


def test_slice_tuples():
clip = VideoFileClip("media/fire2.mp4")
new_clip = clip[0:1, 2:3.2]
assert new_clip.duration == 2.2
assert np.array_equal(new_clip[1.1], clip[2.1])
new_clip = clip[0:1, 1.5:2]
assert new_clip.duration == 1.5
assert np.array_equal(new_clip[1.1], clip[1.6])


def test_slice_mirror():
Expand Down
7 changes: 3 additions & 4 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ def test_issue_470(util):
audio_clip = AudioFileClip("media/crunching.mp3")

# end_time is out of bounds
subclip = audio_clip.subclipped(start_time=6, end_time=9)

with pytest.raises(IOError):
with pytest.raises(ValueError):
subclip = audio_clip.subclipped(start_time=6, end_time=9)
subclip.write_audiofile(wav_filename, write_logfile=True)

# but this one should work..
Expand Down Expand Up @@ -334,7 +333,7 @@ def test_issue_636():

def test_issue_655():
video_file = "media/fire2.mp4"
for subclip in [(0, 2), (1, 2), (2, 3)]:
for subclip in [(0, 2), (1, 2), (2, 2.10)]:
with VideoFileClip(video_file) as v:
with v.subclipped(1, 2) as _:
pass
Expand Down
Loading