Skip to content

Commit

Permalink
Audio error handling made failsafe (#377)
Browse files Browse the repository at this point in the history
* audio error handling made failsafe, as for video

* fixed case when accessing time out of the estimated valid range

* Added test video for issue #246

* remove change not directly related to PR
  • Loading branch information
gyglim authored and bearney74 committed Apr 4, 2017
1 parent c611107 commit 43cd9db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
26 changes: 16 additions & 10 deletions moviepy/audio/io/readers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import subprocess as sp
import re
import warnings

import numpy as np
from moviepy.tools import cvsecs

from moviepy.video.io.ffmpeg_reader import ffmpeg_parse_infos
from moviepy.config import get_setting
from moviepy.compat import PY3, DEVNULL
Expand Down Expand Up @@ -162,8 +160,13 @@ def get_frame(self, tt):

# elements of t that are actually in the range of the
# audio file.

in_time = (tt>=0) & (tt < self.duration)

# Check that the requested time is in the valid range
if not in_time.any():
raise IOError("Error in file %s, "%(self.filename)+
"Accessing time t=%.02f-%.02f seconds, "%(tt[0], tt[-1])+
"with clip duration=%d seconds, "%self.duration)

# The np.round in the next line is super-important.
# Removing it results in artifacts in the noise.
Expand All @@ -184,15 +187,18 @@ def get_frame(self, tt):
indices = frames - self.buffer_startframe
result[in_time] = self.buffer[indices]
return result

except IndexError as error:
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)+
warnings.warn("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))
"but len(buffer)=%d\n"%(len(self.buffer))+ str(error),
UserWarning)

# repeat the last frame instead
indices[indices>=len(self.buffer)] = len(self.buffer) -1
result[in_time] = self.buffer[indices]
return result

else:

Expand Down
5 changes: 4 additions & 1 deletion tests/download_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def download():

download_url("https://github.com/earney/moviepy_media/raw/master/tests/images/pigs_in_a_polka.gif",
"media/pigs_in_a_polka.gif")

download_url("https://data.vision.ee.ethz.ch/cvl/video2gif/kAKZeIzs0Ag.mp4",
"media/video_with_failing_audio.mp4")

download_url("https://github.com/earney/moviepy_media/raw/master/tests/videos/fire2.mp4",
"media/fire2.mp4")
"media/fire2.mp4")
6 changes: 6 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def test_issue_470():
subclip = audio_clip.subclip(t_start=6, t_end=8)
subclip.write_audiofile(os.path.join(TMP_DIR, 'issue_470.wav'), write_logfile=True)

def test_issue_246():
def test_audio_reader():
video = VideoFileClip('media/video_with_failing_audio.mp4')
subclip = video.subclip(270)
subclip.write_audiofile(os.path.join(TMP_DIR, 'issue_246.wav'),
write_logfile=True)

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

0 comments on commit 43cd9db

Please sign in to comment.