溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python3多線程批量去除電視劇的片頭片尾

發(fā)布時間:2020-08-11 18:07:34 來源:網(wǎng)絡 閱讀:1261 作者:喵來個魚 欄目:編程語言
簡介
  • 下載的電視劇經(jīng)常遇到片頭和片尾,有的片頭還有廣告,比較厭煩,所以筆者就寫了個腳本,使用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])  # 處理為十進制,避免小數(shù)點報錯
        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)  # 創(chuàng)建生成的文件路徑+文件名
        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()
啟動命令
  • 由于電視劇集數(shù)比較多,我這里使用了后臺運行
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    
  • 批量處理到指定的目錄,通過預覽已經(jīng)看不到了片頭
    python3多線程批量去除電視劇的片頭片尾
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI