Batch conversion format using Python+ffmpeg

Source: Internet
Author: User


Requirement: Given a folder path, traverse all files within the folder as well as subfolders within the file, when all files with suffix WAV format are converted to OGG format files.


  
  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)



Null

Batch conversion format using Python+ffmpeg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.