zoukankan      html  css  js  c++  java
  • 使用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(' ')
    13. print('*'*40,filepath,' ')
    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,' ','Begin input = ',input,' ')
    25. subname = input.split('.')
    26. output = subname[0] + ".ogg"
    27. finishcode = code + input + codeMid + output
    28. os.system(finishcode)
    29. print('End output = ',output,' ')
    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 runcexe esourcesmediaaudio'
    37. allfile = []
    38. dirlist(fileDir,allfile)
    39. for name in allfile:
    40. print(name,file=fff)
    41. RunScript(allfile)





  • 相关阅读:
    GUI编程
    网络编程
    线程池详解_3
    多线程详解_2
    注解和反射_1
    多线程详解_1
    javaEE简要总结
    MarkDown使用教程简单介绍
    com.sun.xxx.utils不存在问题的解决
    解决npm install安装慢的问题
  • 原文地址:https://www.cnblogs.com/skyhuangdan/p/7133682.html
Copyright © 2011-2022 走看看