使用python+ffmpeg批量轉換格式

來源:互聯網
上載者:User

標籤:bis   bsp   lib   cpp   尾碼   orb   finish   ==   lang   


需求: 給定一個檔案夾路徑,遍曆該檔案夾內的所有檔案以及子檔案夾內的檔案,當所有尾碼名為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)



null

使用python+ffmpeg批量轉換格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.