Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
fix and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
felix committed Aug 23, 2019
1 parent 1ef6669 commit 9d45567
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
15 changes: 10 additions & 5 deletions gnes/preprocessor/io_utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def capture_audio(filename: str = 'pipe:',
sample_rate: int = 16000,
**kwargs) -> List['np.ndarray']:

capture_stdin = (filename == 'pipe:')
if capture_stdin and video_data is None:
raise ValueError(
"the buffered video data for stdin should not be empty")

stream = ffmpeg.input(filename)
stream = stream.output(
'pipe:',
Expand All @@ -56,8 +61,8 @@ def get_chunk_times(filename: str = 'pipe:',
video_data: bytes = None,
silence_threshold: float = DEFAULT_SILENCE_THRESHOLD,
silence_duration: float = DEFAULT_SILENCE_DURATION,
start_time=None,
end_time=None):
start_time: float = None,
end_time: float = None):

silence_start_re = re.compile(
' silence_start: (?P<start>[0-9]+(\.?[0-9]*))$')
Expand Down Expand Up @@ -97,7 +102,7 @@ def get_chunk_times(filename: str = 'pipe:',
chunk_ends.append(float(silence_start_match.group('start')))
if len(chunk_starts) == 0:
# Started with non-silence.
chunk_starts.append(start_time or 0.)
chunk_starts.append(start_time)
elif silence_end_match:
chunk_starts.append(float(silence_end_match.group('end')))
elif total_duration_match:
Expand All @@ -121,8 +126,8 @@ def split_audio(filename: str = 'pipe:',
video_data: bytes = None,
silence_threshold=DEFAULT_SILENCE_THRESHOLD,
silence_duration=DEFAULT_SILENCE_DURATION,
start_time=None,
end_time=None,
start_time: float = None,
end_time: float = None,
verbose=False):
chunk_times = get_chunk_times(
filename,
Expand Down
19 changes: 15 additions & 4 deletions gnes/preprocessor/io_utils/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,26 @@ def capture_frames(filename: str = 'pipe:',
pix_fmt: str = 'rgb24',
fps: int = -1,
scale: str = None,
start_time: float = None,
end_time: float = None,
**kwargs) -> List['np.ndarray']:
capture_stdin = (filename == 'pipe:')
if capture_stdin and video_data is None:
raise ValueError(
"the video data buffered from stdin should not be empty")
"the buffered video data for stdin should not be empty")

# discard corrupted frames
# https://stackoverflow.com/questions/45983605/ffmpeg-jpg-frame-capture-how-to-discard-corrupted-frames
stream = ffmpeg.input(filename, err_detect="aggressive", fflags="discardcorrupt")
input_kwargs = {
'err_detect': 'aggressive',
'fflags': 'discardcorrupt' # discard corrupted frames
}
if start_time is not None:
input_kwargs['ss'] = start_time
else:
start_time = 0.
if end_time is not None:
input_kwargs['t'] = end_time - start_time

stream = ffmpeg.input(filename, **input_kwargs)
if fps > 0:
stream = stream.filter('fps', fps=fps, round='up')

Expand Down

0 comments on commit 9d45567

Please sign in to comment.