python ffmpeg任意提取视频帧的方法

 更新时间:2020年02月21日 15:50:04   作者:anoyi  
这篇文章主要介绍了python ffmpeg任意提取视频帧的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

 环境准备

1、安装 FFmpeg

音/视频工具 FFmpeg 简易安装文档

2、安装 ffmpeg-python

pip3 install ffmpeg-python

3、【可选】安装 opencv-python

pip3 install opencv-python

4、【可选】安装 numpy

pip3 install numpy

视频帧提取

准备视频素材

抖音视频素材下载:https://anoyi.com/dy/top

基于视频帧数提取任意一帧

import ffmpeg
import numpy
import cv2
import sys
import random


def read_frame_as_jpeg(in_file, frame_num):
  """
  指定帧数读取任意帧
  """
  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('总帧数:' + str(total_frames))
  random_frame = random.randint(1, total_frames)
  print('随机帧:' + 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):
  """
  指定时间节点读取任意帧
  """
  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('随机时间:' + 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()

相关资料
https://github.com/kkroening/ffmpeg-python/tree/master/examples

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python从文件中读取数据的方法步骤

    Python从文件中读取数据的方法步骤

    这篇文章主要介绍了Python从文件中读取数据的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • pandas 条件搜索返回列表的方法

    pandas 条件搜索返回列表的方法

    今天小编就为大家分享一篇pandas 条件搜索返回列表的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Pycharm及python安装详细步骤及PyCharm配置整理(推荐)

    Pycharm及python安装详细步骤及PyCharm配置整理(推荐)

    这篇文章主要介绍了Pycharm及python安装详细步骤以及PyCharm配置整理,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Python字符串拼接的4种方法实例

    Python字符串拼接的4种方法实例

    字符串是所有编程语言中都有的基本变量的类型 ,程序员基本每天都在和字符串打交道,下面这篇文章主要给大家介绍了关于Python字符串拼接的4种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Python中将图像转换为PDF的方法实现

    Python中将图像转换为PDF的方法实现

    本文主要介绍了Python中将图像转换为PDF的方法实现,主要使用img2pdf和PyPDF2软件包,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • Python中2种常用数据可视化库Bokeh和Altair使用示例详解

    Python中2种常用数据可视化库Bokeh和Altair使用示例详解

    本文对Python中两个常用的数据可视化库 Bokeh 和 Altair 进行了比较和探讨,通过对它们的特点、优缺点以及使用示例的详细分析,读者可以更好地了解这两个库的功能和适用场景,从而更好地选择合适的库来进行数据可视化工作,感兴趣的朋友跟随小编一起看看吧
    2024-04-04
  • PyCharm无法引用自身项目解决方式

    PyCharm无法引用自身项目解决方式

    今天小编就为大家分享一篇PyCharm无法引用自身项目解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 一文带你全面理解Python中的self

    一文带你全面理解Python中的self

    对于初学Python的同学来说,在class中经常看到self。那么,到底self是个啥?这篇文章小编就来带大家深入了解一下,希望对大家有所帮助
    2023-03-03
  • 利用python脚本提取Abaqus场输出数据的代码

    利用python脚本提取Abaqus场输出数据的代码

    这篇文章主要介绍了利用python脚本提取Abaqus场输出数据,利用python脚本对Abaqus进行数据提取时,要对python脚本做前步的导入处理,本文通过实例代码详细讲解需要的朋友可以参考下
    2022-11-11
  • 使用pandas实现csv/excel sheet互相转换的方法

    使用pandas实现csv/excel sheet互相转换的方法

    今天小编就为大家分享一篇使用pandas实现csv/excel sheet互相转换的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12

最新评论