使用python+ffmpeg批量转换格式


需求: 
给定一个文件夹路径,遍历该文件夹内的所有文件以及子文件夹内的文件,当所有后缀名为wav格式的文件转换为ogg格式的文件.



  1. import os # 获取目录下的所有文件列表
  2. import fnmatch # 文件格式筛选模块,筛选指定格式文件
  3. #遍历
  4. def dirlist(path, allfile):
  5. filelist = os.listdir(path)
  6. for filename in filelist:
  7. filepath = os.path.join(path, filename)
  8. if os.path.isdir(filepath):
  9. dirlist(filepath, allfile)
  10. elif fnmatch.fnmatch(filepath,'*.wav'):#判断文件格式
  11. allfile.append(filepath)
  12. #allfile.append('\n')
  13. print('*'*40,filepath,'\n')
  14. return allfile

  15. #格式转换
  16. def RunScript(fileList) :
  17. print('hello world start:')
  18. readf = open("E:\\py\\readfilename.txt", 'w+') #输出所有读入的文件
  19. writef = open("E:\\py\\writefilename.txt", 'w+') #输出所有创建并写入的文件
  20. code = "ffmpeg -i "
  21. codeMid = " -acodec libvorbis "
  22. for filename in fileList:
  23. input = filename
  24. print('*'*40,'\n','Begin input = ',input,'\n')
  25. subname = input.split('.')
  26. output = subname[0] + ".ogg"
  27. finishcode = code + input + codeMid + output
  28. os.system(finishcode)
  29. print('End output = ',output,'\n')
  30. print(input,file=readf)
  31. print(output,file=writef)
  32. print('hello world end')
  33. #主程序运行
  34. if __name__ =='__main__':
  35. fff = open("E:\\py\\allfile.txt", 'w+')
  36. fileDir = r'G:\SVNworking\trunc\exe\resources\media\audio'
  37. allfile = []
  38. dirlist(fileDir,allfile)
  39. for name in allfile:
  40. print(name,file=fff)
  41. RunScript(allfile)





posted @ 2017-07-07 18:38  水蒸蛋不好吃  阅读(6703)  评论(0编辑  收藏  举报