def gen_subtitle(wavFile, samplesLocation, srtFile, lang):
    srt_file = pysrt.SubRipFile()
    print('loading wav file...')
    song = AudioSegment.from_file(wavFile, format='wav')
    dBFS = song.dBFS
    nonsilence = detect_nonsilent(song, min_silence_len=500, silence_thresh=(dBFS - 16))
    file_count = len(nonsilence)
    print('Nonsilence chunk length {}'.format(str(file_count)))
    print('Start spliting file...')
    chunks = split_on_silence(song, min_silence_len=500, silence_thresh=(dBFS - 16), keep_silence=250)
    print(('Spliting done...' + str(len(chunks))))
    for (i, chunk) in enumerate(chunks):
        silence_chunk = AudioSegment.silent(duration=1000)
        audio_chunk = ((silence_chunk + chunk) + silence_chunk)
        normalized_chunk = match_target_amplitude(audio_chunk, (- 20.0))
        starttime = nonsilence[i][0]
        endtime = nonsilence[i][1]
        print('\n>>{} of {}, Exporting {}chunk{}.wav start: {} end: {}'.format(i, file_count, samplesLocation, i, starttime, endtime))
        chunk_file_path = '{}chunk{}.wav'.format(samplesLocation, str(i))
        normalized_chunk.export(chunk_file_path, bitrate='192k', format='wav')
        time.sleep(2)
        print('Going to generete the dialogs of file {}'.format(chunk_file_path))
        dialogs = recognize(chunk_file_path, lang)
        print('{} file dialog is: {}'.format(chunk_file_path, dialogs))
        start_time = get_timestamp(starttime)
        end_time = get_timestamp(endtime)
        sub = pysrt.SubRipItem((i + 1), start=start_time, end=end_time, text='{} {}'.format(str((i + 1)), dialogs))
        srt_file.append(sub)
    srt_file.save(srtFile)
def gen_subtitle(wavFile, samplesLocation, srtFile, lang):
    srt_file = pysrt.SubRipFile()
    print('loading wav file...')
    song = AudioSegment.from_file(wavFile, format='wav')
    dBFS = song.dBFS
    nonsilence = detect_nonsilent(song, min_silence_len=500, silence_thresh=(dBFS - 16))
    file_count = len(nonsilence)
    print('Nonsilence chunk length {}'.format(str(file_count)))
    print('Start spliting file...')
    chunks = split_on_silence(song, min_silence_len=500, silence_thresh=(dBFS - 16), keep_silence=250)
    print(('Spliting done...' + str(len(chunks))))
    for (i, chunk) in enumerate(chunks):
        silence_chunk = AudioSegment.silent(duration=1000)
        audio_chunk = ((silence_chunk + chunk) + silence_chunk)
        normalized_chunk = match_target_amplitude(audio_chunk, (- 20.0))
        starttime = nonsilence[i][0]
        endtime = nonsilence[i][1]
        print('\n>>{} of {}, Exporting {}chunk{}.wav start: {} end: {}'.format(i, file_count, samplesLocation, i, starttime, endtime))
        chunk_file_path = '{}chunk{}.wav'.format(samplesLocation, str(i))
        normalized_chunk.export(chunk_file_path, bitrate='192k', format='wav')
        time.sleep(2)
        print('Going to generete the dialogs of file {}'.format(chunk_file_path))
        dialogs = recognize(chunk_file_path, lang)
        print('{} file dialog is: {}'.format(chunk_file_path, dialogs))
        start_time = get_timestamp(starttime)
        end_time = get_timestamp(endtime)
        sub = pysrt.SubRipItem((i + 1), start=start_time, end=end_time, text='{} {}'.format(str((i + 1)), dialogs))
        srt_file.append(sub)
    srt_file.save(srtFile)