audio_cutter.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:rr_tools 作者: mitmedialab 项目源码 文件源码
def cut_audio(line):
    ''' cuts the audio file at the specified start and stop times,
    and then exports to the desiginated output folder
    line format: [AUDIO_FILE_NAME, START, STOP]
    '''
    audio_file_name = line[0]
    file_type = audio_file_name[-4:]

    if file_type == '.wav':
        audio = AudioSegment.from_wav(args.in_dir + audio_file_name)
        cut = audio[cvt_time(line[1]):cvt_time(line[2])]
        #create name for cut audio file
        file_num = get_num(audio_file_name)
        cut_name = audio_file_name[0:-4] + '_cut_%d' % file_num + '.wav'
        #export to output folder
        cut.export(args.out_dir + cut_name, format="wav")
        print('---> ' + cut_name + '\n')
    elif file_type == '.mp3':
        audio = AudioSegment.from_mp3(args.in_dir + audio_file_name)
        cut = audio[cvt_time(line[1]):cvt_time(line[2])]
        #create name for cut audio file
        file_num = get_num(audio_file_name)
        cut_name = audio_file_name[0:-4] + '_cut_%d' % file_num + '.mp3'
        #export to output folder
        cut.export(args.out_dir + cut_name, format="mp3")
        print('---> ' + cut_name + '\n')
    else:
        #error, incompatible file type
        print('**** ' + audio_file_name + ' caused an error')
        print('**** ' + file_type + ' incompatible file type')
        print('**** skipping file\n')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号