python3多线程批量去除电视剧的片头片尾
admin
2023-07-10 07:44:31
0次
简介
  • 下载的电视剧经常遇到片头和片尾,有的片头还有广告,比较厌烦,所以笔者就写了个脚本,使用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多线程批量去除电视剧的片头片尾

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...