python 批量遞增重命名ts視頻文件

#python 3.6
#author:Kevin
import os
path=input('')#程序運行後輸入要重命名的文件所在文件夾路徑,如:e://video
filelist=os.listdir(path)#本文件夾下所有文件生成列表,打印結果:filelist=['0.ts','1.ts',...]
i=-1
for file in filelist:
  filetype=os.path.splitext(file)[1]
  If filetype=='.ts':
    filename=os.path.splitext(file)[0]
    n=int(filename)
    If n<=9:
      i+=1
      s=str(i)
      s_zfill=s.zfill(2) #str.zfill(width)字符串寬度,右對齊,寬度不足0補齊。
      oldname=os.path.join(path,file)#打印結果,例:e://video//0.ts
      filename=os.path.splitext(file)[0]
#打印結果:0
      filetype=os.path.splitext(file)[1]
#打印結果:.ts
      newname=os.path.join(path,s_zfill+filetype)
#打印結果:e://video//00.ts
      os.rename(oldname,newname)
      print(oldname,'...',newname)
#打印結果:e://video//0.ts...e://video//00.ts
                       e://video//1.ts...e://video//01.ts
                       e://video//2.ts...e://video//02.ts

#ps:ts視頻文件一般從0.ts開始,裏面有三個非ts文件,先排除後再重命名,此段代碼只重命名前10個。

合併ts視頻文件:

1.打開cmd

2.copy /b e:\\video\\*.ts e:\\video\\new.ts

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章