Skip to content

Commit

Permalink
Issue 470, reading past audio file EOF (#476)
Browse files Browse the repository at this point in the history
* create better error message when we read past the end of an audio file

* add a working example

* download crunching.mp3
  • Loading branch information
bearney74 committed Mar 13, 2017
1 parent 4faeef3 commit c0602f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion moviepy/audio/io/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ def get_frame(self, tt):
result[in_time] = self.buffer[indices]
return result
except IndexError as error:
raise IOError("Error in file %s, "%(self.filename)+
if indices.max() > len(self.buffer):
raise IOError("Error reading file '%s', " % self.filename +
"trying to access beyond the end of the file")
else:
raise IOError("Error in file %s, "%(self.filename)+
"At time t=%.02f-%.02f seconds, "%(tt[0], tt[-1])+
"indices wanted: %d-%d, "%(indices.min(), indices.max())+
"but len(buffer)=%d\n"%(len(self.buffer))+ str(error))
Expand Down
3 changes: 3 additions & 0 deletions tests/download_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ def download():

download_url("https://github.com/earney/moviepy_media/raw/master/tests/videos/big_buck_bunny_432_433.webm",
"media/big_buck_bunny_432_433.webm")

download_url("https://github.com/earney/moviepy_media/raw/master/tests/sounds/crunching.mp3",
"media/crunching.mp3")
14 changes: 14 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,19 @@ def test_issue_467():
#caused an error, NameError: global name 'copy' is not defined
clip = clip.fx(vfx.blink, d_on=1, d_off=1)

def test_issue_470():
audio_clip = AudioFileClip('media/crunching.mp3')

# t_end is out of bounds
subclip = audio_clip.subclip(t_start=6, t_end=9)

with pytest.raises(IOError, message="Expecting IOError"):
subclip.write_audiofile('/tmp/issue_470.wav', write_logfile=True)

#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)


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

0 comments on commit c0602f4

Please sign in to comment.