您好,登錄后才能下訂單哦!
#!/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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。