#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