我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

python3多线程批量去除电视剧的片头片尾

简介
  • 下载的电视剧经常遇到片头和片尾,有的片头还有广告,比较厌烦,所以笔者就写了个脚本,使用python3,ffmpeg批量多线程去除了片头和片尾,这里针对的是单部多集电视剧的片头、片尾,片头片尾的时间比较相似
  • 针对多部多集的电视剧,可以通过excel,或者构建字典的方式解决。
    代码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author:Aiker Zhao
@file:cut_media_all.py
@time:下午11:35
"""
import os
import re
import subprocess
from decimal import Decimal
from multiprocessing import Pool

path = r"/volume1/movie/201903/t1/"
new_path = r'/volume1/movie/201903/t2/'
if not os.path.exists(new_path):
    os.mkdir(new_path)
else:
    print(new_path + 'is ok!')

# 获取视频的 duration 时长 长 宽
def get_video_length(file):
    process = subprocess.Popen(['ffmpeg', '-i', file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout, stderr = process.communicate()
    print(stdout)
    pattern_duration = re.compile("Duration:\s(\d+?):(\d+?):(\d+\.\d+?),")
    pattern_size = re.compile(",\s(\d{3,4})x(\d{3,4}),")
    matches = re.search(pattern_duration, stdout.decode('utf-8'))
    size = re.search(pattern_size, stdout.decode('utf-8'))
    if size:
        size = size.groups()
        print(size)
    if matches:
        matches = matches.groups()
        print(matches)
        hours = Decimal(matches[0])
        minutes = Decimal(matches[1])
        seconds = Decimal(matches[2])  # 处理为十进制,避免小数点报错
        total = 0
        total += 60 * 60 * hours
        total += 60 * minutes
        total += seconds
        width = size[0]
        height = size[1]
        return {
            'total': total,
            'width': width,
            'height': height
        }

def cutVideo(startPoint, file, endPoint, newFile):
    command = ['ffmpeg', '-ss', startPoint, '-i', file, '-acodec', 'copy', '-vcodec', 'copy', '-t',
               endPoint, newFile]
    subprocess.call(command)

def millisecToAssFormat(t):  # 取时间
    s = t % 60
    m = t // 60
    if t < 3600:
        h = 00
    else:
        h = t // 3600
    return '%02d:%02d:%02d' % (h, m, s)

def main(file):
    # for file in os.listdir(path):
    #     print(file)
    videoInfo = get_video_length(file)  # 视频信息
    print(videoInfo)
    if videoInfo:
        duration = videoInfo.get('total')  # 时长 秒
        startPoint = 71  # 剪辑有片头片尾的视频 cut掉前71s后120s
        startPoint = millisecToAssFormat(startPoint)
        endPoint = duration - 120  # 120秒
        endPoint = millisecToAssFormat(endPoint)
        new_File = os.path.join(new_path, file)  # 创建生成的文件路径+文件名
        print(new_File, endPoint)
        cutVideo(startPoint, file, endPoint, new_File)

if __name__ == '__main__':
    # main()
    file = [file for file in os.listdir(path) if os.path.isfile(file) == True]
    pool = Pool()
    pool.map(main, file)
    pool.close()
    pool.join()
启动命令
  • 由于电视剧集数比较多,我这里使用了后台运行
nohup python cut_media_all.py > filelog.txt 2>&1 &
  • 多线程对帧处理
frame= 7888 fps=0.0 q=-1.0 size=   69120kB time=00:05:12.96 bitrate=1809.3kbits/s speed= 623x    
frame= 9849 fps=0.0 q=-1.0 size=   77312kB time=00:06:30.25 bitrate=1622.9kbits/s speed= 746x    
frame=18599 fps=18494 q=-1.0 size=  134656kB time=00:12:21.41 bitrate=1487.8kbits/s speed= 737x    
frame=19418 fps=16222 q=-1.0 size=  138752kB time=00:12:53.01 bitrate=1470.4kbits/s speed= 646x    
frame=23712 fps=15748 q=-1.0 size=  171008kB time=00:15:45.94 bitrate=1481.0kbits/s speed= 628x    
frame=26403 fps=15527 q=-1.0 size=  205312kB time=00:17:32.41 bitrate=1598.1kbits/s speed= 619x    
frame=32330 fps=16119 q=-1.0 size=  231168kB time=00:21:30.64 bitrate=1467.3kbits/s speed= 643x    
frame=35540 fps=11426 q=-1.0 size=  253696kB time=00:23:39.07 bitrate=1464.5kbits/s speed= 456x    
frame=33771 fps=10233 q=-1.0 size=  257792kB time=00:22:27.15 bitrate=1567.6kbits/s speed= 408x    
frame=53980 fps=14772 q=-1.0 size=  400896kB time=00:35:56.65 bitrate=1522.8kbits/s speed= 590x    
frame=47614 fps=12529 q=-1.0 size=  354816kB time=00:31:40.86 bitrate=1529.1kbits/s speed= 500x    
frame=63413 fps=15833 q=-1.0 Lsize=  491071kB time=00:42:13.99 bitrate=1587.6kbits/s speed= 633x    
  • 批量处理到指定的目录,通过预览已经看不到了片头
    python3多线程批量去除电视剧的片头片尾

文章标题:python3多线程批量去除电视剧的片头片尾
网址分享:http://mswzjz.cn/article/pjphps.html

其他资讯