欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python ffmpeg任意提取視頻幀的方法

 更新時間:2020年02月21日 15:50:04   作者:anoyi  
這篇文章主要介紹了python ffmpeg任意提取視頻幀的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

 環(huán)境準(zhǔn)備

1、安裝 FFmpeg

音/視頻工具 FFmpeg 簡易安裝文檔

2、安裝 ffmpeg-python

pip3 install ffmpeg-python

3、【可選】安裝 opencv-python

pip3 install opencv-python

4、【可選】安裝 numpy

pip3 install numpy

視頻幀提取

準(zhǔn)備視頻素材

抖音視頻素材下載:https://anoyi.com/dy/top

基于視頻幀數(shù)提取任意一幀

import ffmpeg
import numpy
import cv2
import sys
import random


def read_frame_as_jpeg(in_file, frame_num):
  """
  指定幀數(shù)讀取任意幀
  """
  out, err = (
    ffmpeg.input(in_file)
       .filter('select', 'gte(n,{})'.format(frame_num))
       .output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
       .run(capture_stdout=True)
  )
  return out


def get_video_info(in_file):
  """
  獲取視頻基本信息
  """
  try:
    probe = ffmpeg.probe(in_file)
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    if video_stream is None:
      print('No video stream found', file=sys.stderr)
      sys.exit(1)
    return video_stream
  except ffmpeg.Error as err:
    print(str(err.stderr, encoding='utf8'))
    sys.exit(1)


if __name__ == '__main__':
  file_path = '/Users/admin/Downloads/拜無憂.mp4'
  video_info = get_video_info(file_path)
  total_frames = int(video_info['nb_frames'])
  print('總幀數(shù):' + str(total_frames))
  random_frame = random.randint(1, total_frames)
  print('隨機(jī)幀:' + str(random_frame))
  out = read_frame_as_jpeg(file_path, random_frame)
  image_array = numpy.asarray(bytearray(out), dtype="uint8")
  image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
  cv2.imshow('frame', image)
  cv2.waitKey()

基于時間提取任意一幀

import ffmpeg
import numpy
import cv2
import sys
import random


def read_frame_by_time(in_file, time):
  """
  指定時間節(jié)點讀取任意幀
  """
  out, err = (
    ffmpeg.input(in_file, ss=time)
       .output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
       .run(capture_stdout=True)
  )
  return out


def get_video_info(in_file):
  """
  獲取視頻基本信息
  """
  try:
    probe = ffmpeg.probe(in_file)
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    if video_stream is None:
      print('No video stream found', file=sys.stderr)
      sys.exit(1)
    return video_stream
  except ffmpeg.Error as err:
    print(str(err.stderr, encoding='utf8'))
    sys.exit(1)

if __name__ == '__main__':
  file_path = '/Users/admin/Downloads/拜無憂.mp4'
  video_info = get_video_info(file_path)
  total_duration = video_info['duration']
  print('總時間:' + total_duration + 's')
  random_time = random.randint(1, int(float(total_duration)) - 1) + random.random()
  print('隨機(jī)時間:' + str(random_time) + 's')
  out = read_frame_by_time(file_path, random_time)
  image_array = numpy.asarray(bytearray(out), dtype="uint8")
  image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
  cv2.imshow('frame', image)
  cv2.waitKey()

相關(guān)資料
https://github.com/kkroening/ffmpeg-python/tree/master/examples

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python利用opencv如何實現(xiàn)答題卡自動判卷

    python利用opencv如何實現(xiàn)答題卡自動判卷

    由于工作需要,最近在研究關(guān)于如何通過程序識別答題卡的客觀題的答案,所以下面這篇文章主要介紹了python利用opencv如何實現(xiàn)答題卡自動判卷的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Pycharm中配置遠(yuǎn)程Docker運行環(huán)境的教程圖解

    Pycharm中配置遠(yuǎn)程Docker運行環(huán)境的教程圖解

    這篇文章主要介紹了Pycharm中配置遠(yuǎn)程Docker運行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Django框架中視圖的用法

    Django框架中視圖的用法

    這篇文章介紹了Django框架中視圖的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • TensorFlow模型保存/載入的兩種方法

    TensorFlow模型保存/載入的兩種方法

    這篇文章主要為大家詳細(xì)介紹了TensorFlow 模型保存/載入的兩種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • python中加背景音樂如何操作

    python中加背景音樂如何操作

    在本篇文章里小編給大家整理了關(guān)于在python中加背景音樂的方法,需要的朋友們可以參考下。
    2020-07-07
  • django orm模糊查詢、正則匹配多個值方式

    django orm模糊查詢、正則匹配多個值方式

    這篇文章主要介紹了django orm模糊查詢、正則匹配多個值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 解決numpy數(shù)組互換兩行及賦值的問題

    解決numpy數(shù)組互換兩行及賦值的問題

    這篇文章主要介紹了解決numpy數(shù)組互換兩行及賦值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Python3.2模擬實現(xiàn)webqq登錄

    Python3.2模擬實現(xiàn)webqq登錄

    這篇文章主要介紹了Python模擬實現(xiàn)webqq登錄的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Python中應(yīng)用protobuf的示例詳解

    Python中應(yīng)用protobuf的示例詳解

    這篇文章主要來和大家聊一聊?protobuf,它是一個數(shù)據(jù)序列化和反序列化協(xié)議,因此它和?json?的定位是一樣的。文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2023-02-02
  • Python實現(xiàn)時鐘顯示效果思路詳解

    Python實現(xiàn)時鐘顯示效果思路詳解

    這篇文章主要介紹了Python實現(xiàn)時鐘顯示,需要的朋友可以參考下
    2018-04-04

最新評論