1、重命名,Python中文路径各种错误,所以需要先将所有文件的路径名全都改成中文。用的是MAC系统,所以WIN下的命令行批处理没法解决,所以用C来完成
// Created by Carl on 16. // Copyright (c) 2016年 Carl. All rights reserved. // #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> using namespace std; void getFileList() { string sourceDir = "/Users/karl/Work/database/rawdata/children_CN/"; string targetDir = "/Users/karl/Work/database/rawdata/children/"; DIR *dir; struct dirent *ptr; int i = 0; if ((dir=opendir(sourceDir.c_str())) == NULL) { perror("Open dir error..."); exit(1); } while ((ptr=readdir(dir)) != NULL) { if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent continue; else if(ptr->d_type == 8) { printf("%s %s\n",(sourceDir + ptr->d_name).c_str(),(targetDir + to_string(i) + ".wav").c_str()); if(rename((sourceDir + ptr->d_name).c_str(), (targetDir + to_string(i++) + ".wav").c_str())<0) cout<<"error"<<endl; else cout<<"ok"<<endl; } } return; } int main() { getFileList(); return 1; }
2、然后再使用FFMPEG那篇文章写的Python代码,将所有音频文件转成统一格式
#coding=utf-8 #!/usr/bin/env python ‘‘‘CREATED:2016-03-08 Use example of ffmpeg ‘‘‘ import argparse import sys import os import string import subprocess as sp #Full path of ffmpeg FFMPEG_BIN = "/Users/karl/Documents/python/audio/tool/ffmpeg" #Full path of sourceDir sourceDir = "/Users/karl/Work/database/rawdata/male/" #Full path of targetDir targetDir = "/Users/karl/Work/database/age/male/" #Channel setting 1 for mono ac = 1 #Sample frequency sf = 16000 #Extension setting ext = ‘wav‘ def convert(sourceDir, targetDir, ac, sf, ext): i = 0 if not os.path.exists(targetDir): os.mkdir(targetDir) files = os.listdir(sourceDir) for f in files: if f.endswith(‘.wav‘): command = [ FFMPEG_BIN, ‘-i‘, os.path.join(sourceDir, f), ‘-ac‘, str(ac), ‘-ar‘, str(sf), os.path.join(targetDir, str(i) + "." + ext)] i += 1 print command pipe = sp.Popen(command, stdout = sp.PIPE, bufsize = 10**8) if __name__ == ‘__main__‘: convert(sourceDir, targetDir, ac, sf, ext)
时间: 2024-11-18 09:44:11