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

Commit

Permalink
refactor(preprocessor): use io utils in audio and gif
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmyshin committed Aug 28, 2019
1 parent 9a791c2 commit 0c6f485
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 3 additions & 5 deletions gnes/preprocessor/audio/audio_vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
import numpy as np

from ..base import BaseAudioPreprocessor
from ..helper import get_video_length_from_raw, get_audio
from ...proto import array2blob
from ..io_utils.audio import split_audio


class AudioVanilla(BaseAudioPreprocessor):

def __init__(self, audio_interval: int,
def __init__(self,
sample_rate: int, *args, **kwargs):
super().__init__(*args, **kwargs)
self.audio_interval = audio_interval
self.sample_rate = sample_rate

def apply(self, doc: 'gnes_pb2.Document') -> None:
super().apply(doc)

if doc.raw_bytes:
audio = get_audio(doc.raw_bytes, self.sample_rate,
self.audio_interval, get_video_length_from_raw(doc.raw_bytes))
audio = split_audio(input_data=doc.raw_bytes, sample_rate=self.sample_rate)
if len(audio) >= 1:
for ci, chunks in enumerate(audio):
c = doc.chunks.add()
Expand Down
6 changes: 4 additions & 2 deletions gnes/preprocessor/io_utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def split_audio(input_fn: str = 'pipe:',
silence_duration=DEFAULT_SILENCE_DURATION,
start_time: float = None,
end_time: float = None,
sample_rate: int = 16000,
verbose=False):
_check_input(input_fn, input_data)
chunk_times = get_chunk_times(
Expand All @@ -157,7 +158,7 @@ def split_audio(input_fn: str = 'pipe:',
start_time=start_time,
end_time=end_time)
audio_chunks = list()
print("chunk_times", chunk_times)
# print("chunk_times", chunk_times)
for i, (start_time, end_time) in enumerate(chunk_times):
time = end_time - start_time
if time < 0:
Expand All @@ -169,7 +170,8 @@ def split_audio(input_fn: str = 'pipe:',
}

output_kwargs = {
'format': 'wav'
'format': 'wav',
'ar': sample_rate
}

cmd_args = compile_args(
Expand Down
6 changes: 4 additions & 2 deletions gnes/preprocessor/video/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np

from ..base import BaseVideoPreprocessor, RawChunkPreprocessor
from ..helper import split_video_frames, phash_descriptor, get_gif
from ..helper import split_video_frames, phash_descriptor
from ...proto import gnes_pb2, array2blob, blob2array
from ..io_utils import video as video_util

Expand Down Expand Up @@ -187,4 +187,6 @@ def apply(self, doc: 'gnes_pb2.Document') -> None:
class GifChunkPreprocessor(RawChunkPreprocessor, BaseVideoPreprocessor):
@staticmethod
def _parse_chunk(chunk: 'gnes_pb2.Chunk', *args, **kwargs):
return get_gif(blob2array(chunk.blob))
from ..io_utils import gif as gif_util

return gif_util.encode_gif(blob2array(chunk.blob), fps=10)

0 comments on commit 0c6f485

Please sign in to comment.