python3.6和ffmpeg结合剪去电视剧的头尾

编程入门 行业动态 更新时间:2024-10-11 15:24:01

python3.6和ffmpeg结合剪去电视剧的<a href=https://www.elefans.com/category/jswz/34/1326186.html style=头尾"/>

python3.6和ffmpeg结合剪去电视剧的头尾

看电视剧时片头片尾特别耽误时间,如果能剪切掉就完美啦!这点事情能难道处女座程序员吗,说干就干,用python和ffmpeg小试牛刀!不废话,直接上代码!

#截取视频,去掉头尾
# -*- coding: UTF-8 -*-import os
import math
import re
import sys#请根据您的情况自行修改todo位置的代码def cut_movie(src_path,des_path):#todo 片头长度header_time_str='00:02:27'#header_time_str = '00:00:00'header_time=header_time_str.split(":")header_time=int(header_time[0])*3600+int(header_time[1])*60+int(header_time[2])print ("header_time",header_time)#todo 片尾长度#footer_time_str='00:01:58'footer_time_str = '00:00:00'footer_time=footer_time_str.split(":")footer_time=int(footer_time[0])*3600+int(footer_time[1])*60+int(footer_time[2])print ("footer_time",footer_time)#tmp = os.popen('''ffprobe "/Volumes/TOSHIBA/\[纪晓岚1\]\[40集\]\[2001\]/01.mkv"  2>&1''')tmp = os.popen('''ffprobe "{}"  2>&1'''.format(src_path))tmp=tmp.read()#print ("------",tmp)matchObj=re.search(r"Duration: (.*?),",tmp,re.M|re.I)if not matchObj:returntime_l=matchObj.group(1)print("time_l",time_l)#movie_len_str='00:46:51.05'movie_len_str=time_lmovie_len_str=movie_len_str[:-3]print (movie_len_str)movie_len = movie_len_str.split(":")if int(movie_len[1])<39:#小于44分钟,可能没有片尾片头信息print (int(movie_len[1]))returnmovie_len=int(movie_len[0])*3600+int(movie_len[1])*60+int(movie_len[2])print ("movie_len",movie_len)#包含头的时间#now_time_len=movie_len-header_time-footer_timenow_time_len = movie_len - footer_timeprint ("now_time_len",now_time_len)h=math.floor( now_time_len/3600)print ("h",h)m=math.floor((now_time_len-(3600*h))/60)print (m)s=now_time_len-(3600*h)-(60*m)print (s)if h<10:h="0{}".format(h)if m<10 :m = "0{}".format(m)if s<10:s = "0{}".format(s)end_time_len="{}:{}:{}".format(h,m,s)print(end_time_len)cmd="""ffmpeg -i "{}" -vcodec copy -acodec copy -ss {} -to {} "{}"  -y """.format(src_path,header_time_str,end_time_len,des_path)os.system(cmd)print(cmd)if __name__ == '__main__':#todo 要裁剪的目录for root,paths,fnames in os.walk("/Volumes/TOSHIBA/[纪晓-岚4][42集][2009]"):#包含"head"的目录会被裁剪if re.search(r"head",root,re.I|re.M):#print (root,paths,fnames)root_str=os.path.split(root)#print(len(fnames))#exit(0)print (root_str)for p in fnames:s_path=os.path.join(root,p)print (s_path)e_path=os.path.join(root_str[1],p)print (e_path)if not os.path.exists(root_str[1]):os.makedirs(root_str[1])#todo 保存文件在命令行所在目录,注意不要和视屏源在同一目录下避免被覆盖cut_movie(s_path,e_path)#exit(0)

使用时注意修改成自己的片头和片尾长度,已经自己的视频目录和存储目录

更多推荐

python3.6和ffmpeg结合剪去电视剧的头尾

本文发布于:2024-02-11 16:07:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1681918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:头尾   剪去   电视剧   ffmpeg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!