random_tag.py 文件源码

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

项目:audio-tagging-toolkit 作者: hipstas 项目源码 文件源码
def tags_to_wav(media_path,out_dir,tag_pairs):
    basename=os.path.splitext(os.path.basename(media_path))[0]
    wav_source=True
    if media_path.lower()[-4:] not in ('.mp3','.wav'):     # Creates a temporary WAV
        wav_source=False                         # if input is MP4
        temp_filename=media_path.split('/')[-1]+'_temp.wav'
        audio_path='/var/tmp/'+temp_filename   # Pathname for temp WAV
        subprocess.call(['ffmpeg', '-y', '-i', media_path, audio_path]) # '-y' option overwrites existing file if present
    else:
        audio_path=media_path
    try:
        if audio_path[-4:].lower()=='.mp3':
            song = AudioSegment.from_mp3(audio_path)
        else:
            song = AudioSegment.from_wav(audio_path)

    except Exception as inst:
        print(inst)
        sys.exit(2)

    for pair in tag_pairs:
        start = pair[0]
        duration = pair[1]-pair[0]
        clip_pathname=os.path.join(out_dir,basename+"_start_"+str(start)+"_dur_"+str(duration)+".wav")
        start_msec = float(start) * 1000.0
        duration_msec = float(duration) * 1000
        if not os.path.exists(clip_pathname):
            clip_data = song[start_msec:start_msec+duration_msec]
            clip_data=clip_data.set_channels(1)
            clip_data.export(clip_pathname, format="wav")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号