当前位置: 首页> 游戏> 手游 > 自建网站免费教程_网站外包制作_网络优化的流程_下载班级优化大师并安装

自建网站免费教程_网站外包制作_网络优化的流程_下载班级优化大师并安装

时间:2025/7/9 15:04:55来源:https://blog.csdn.net/u013090854/article/details/144553445 浏览次数:0次
自建网站免费教程_网站外包制作_网络优化的流程_下载班级优化大师并安装

本章使用 ffmpeg 实现观影中经常会用到的功能,截图。
以下给出两种方式。

课程需具备的基础能力:Python 

1. 使用 subprocess 调用 FFmpeg 命令

import subprocess
def extract_frame(video_path, output_image_path, timestamp="00:00:05")"""从视频中截取指定时间点的帧作为图片:param video_path: 输入视频文件路径:param output_image_path: 输出图片文件路径:param timestamp: 截取时间点,格式为 'HH:MM:SS', 默认为 00:00:05"""try:# 调用 ffmpeg 命令command = ['ffmpeg',"-i", video_path,		# 输入视频文件"-ss", timestamp,		# 截取的时间点"-vframes", "1",		# 截取一帧output_image_path,		# 输出图片文件路径"-y"]suprocess.run(command, check=True)print(f"成功截取帧: {output_image_path}")except subprocess.CalledProcessError as e:print(f"错误:{e}")// 使用
video_file = "example.mp4"
output_image = "screenshot.jpg"
timestamp = "00:01:00" # 截取 1 分钟时的帧
extract_frame(video_file, output_image, timestamp)

2. 使用 ffmpeg-python

示例代码:

import ffmpegdef extract_frame_ffmpeg(video_path, output_image_path, timestamp=5.0):"""使用 ffmpeg-python 从视频中截取指定时间点的帧并保存为图片。:param video_path: 输入视频文件路径:param output_image_path: 输出图片文件路径:param timestamp: 截取的时间点,单位秒"""try:# 使用 ffmpeg-python 提取帧ffmpeg.input(video_path, ss=timestamp).output(output_image_path, vframes=1).run(overwrite_output=True)print(f"成功截取帧并保存到 {output_image_path}")except ffmpeg.Error as e:print(f"错误: {e.stderr.decode()}")# 使用示例
video_file = "example.mp4"
output_image = "screenshot2.jpg"
timestamp = 60.0  # 截取 60 秒时的帧
extract_frame_ffmpeg(video_file, output_image, timestamp)

解释

  1. ffmpeg.input(video_path, ss=timestamp)

    • input() 函数用来指定输入视频路径。
    • ss 参数指定截取的时间戳,可以是秒数(例如 60.0)或者 HH:MM:SS 格式。
  2. output(output_image_path, vframes=1)

    • output() 用于指定输出文件路径。
    • vframes=1 表示我们只提取视频中的一帧。
  3. run(overwrite_output=True)

    • run() 执行 FFmpeg 命令,overwrite_output=True 表示如果目标文件已存在,会被覆盖。
关键字:自建网站免费教程_网站外包制作_网络优化的流程_下载班级优化大师并安装

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: