日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

python ffmpeg模块,python执行ffmpeg

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python ffmpeg模块,python执行ffmpeg 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python執行ffmpeg命令

能拿到ffmpeg正常輸出

ffmpeg拋出異常時可以拿到異常信息

返回ffmpeg處理進度

以下代碼依賴的pexpect,progressbar需要安裝下

import pexpect

import subprocess

import progressbar

import logging

def exec_progress(command, video_duration_seconds):

"""

執行ffmpeg命令,并根據ffmpeg輸出中的"time=xxx"匹配進度, ffmpeg執行失敗時拋出FfmpegException

:param command: ffmpeg命令

:param video_duration_seconds: 視頻總時長

"""

thread = pexpect.spawn(command)

cpl = thread.compile_pattern_list([pexpect.EOF,

"frame=.*time=([\d:\.]*)(.*)",

'(.+)'])

progress = progressbar.ProgressBar(max_value=video_duration_seconds).start()

output_list = []

while True:

i = thread.expect_list(cpl, timeout=None)

if i == 0:

progress.finish()

break

elif i == 1:

seconds = duration_to_seconds(thread.match.group(1))

progress.update(seconds)

elif i == 2:

logging.debug(thread.match.group(0))

output_list.append(thread.match.group(0))

pass

thread.close()

if thread.exitstatus:

raise FfmpegException(thread.exitstatus, command, "\n".join(output_list))

def exec_output(command):

"""

執行ffmpeg命令并返回所有輸出,如果執行失敗,拋出FfmpegException

:param command: ffmpeg命令

:return: ffmpeg標準輸出

"""

try:

process = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)

return process

except subprocess.CalledProcessError as err:

raise FfmpegException(err.returncode, err.cmd, err.output)

# 這個方法應該單獨抽到別的模塊

def duration_to_seconds(duration):

time_arr = duration[0:duration.find(".")].split(":")

if len(time_arr) == 3:

return int(time_arr[0]) * 3600 + int(time_arr[1]) * 60 + int(time_arr[2])

logging.error("unrecognized duration %s", duration)

return 0

class FfmpegException(Exception):

"""

使用ffmpeg報錯時拋出

"""

def __init__(self, returncode, cmd, output=None):

self.returncode = returncode

self.cmd = cmd

self.output = output

def __str__(self):

return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)

簡單說明下

exec_progress執行耗時較長需要實時拿到處理進度的場景,如壓縮,裁剪等

exec_output執行耗時較短直接拿到輸出結果的場景,如獲取視頻信息等

總結

以上是生活随笔為你收集整理的python ffmpeg模块,python执行ffmpeg的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。