FFmpeg c++ 如何截取视频片段

时间: 2023-11-27 09:03:19 浏览: 41
在 C++ 中使用 FFmpeg 截取视频片段,可以按照以下步骤进行: 1. 引入 FFmpeg 头文件 ```c++ extern "C" { #include <libavutil/timestamp.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> } ``` 2. 打开视频文件 ```c++ AVFormatContext *pFormatCtx = nullptr; if (avformat_open_input(&pFormatCtx, filename, nullptr, nullptr) < 0) { // 打开文件失败 return; } if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) { // 获取文件信息失败 return; } ``` 3. 查找视频流 ```c++ int videoStream = -1; for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { // 没有找到视频流 return; } ``` 4. 获取视频编解码器 ```c++ AVCodecParameters *pCodecParams = pFormatCtx->streams[videoStream]->codecpar; AVCodec *pCodec = avcodec_find_decoder(pCodecParams->codec_id); if (pCodec == nullptr) { // 没有找到对应的编解码器 return; } AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec); if (pCodecCtx == nullptr) { // 分配编解码器上下文失败 return; } if (avcodec_parameters_to_context(pCodecCtx, pCodecParams) < 0) { // 设置编解码器上下文参数失败 return; } if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { // 打开编解码器失败 return; } ``` 5. 创建解码器帧和转换器 ```c++ AVFrame *pFrame = av_frame_alloc(); if (pFrame == nullptr) { // 分配解码器帧失败 return; } AVFrame *pFrameRGB = av_frame_alloc(); if (pFrameRGB == nullptr) { // 分配转换器帧失败 return; } int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t)); if (buffer == nullptr) { // 分配转换器缓冲区失败 return; } av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); struct SwsContext *swsCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr); if (swsCtx == nullptr) { // 创建转换器失败 return; } ``` 6. 定位到开始截取的位置 ```c++ int64_t startTimestamp = startTimeInSeconds * AV_TIME_BASE; if (av_seek_frame(pFormatCtx, videoStream, startTimestamp, AVSEEK_FLAG_BACKWARD) < 0) { // 定位到指定时间戳失败 return; } ``` 7. 读取视频帧并进行截取 ```c++ int gotFrame = 0; AVPacket packet; while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { avcodec_decode_video2(pCodecCtx, pFrame, &gotFrame, &packet); if (gotFrame) { int64_t pts = av_frame_get_best_effort_timestamp(pFrame); if (pts >= endTimestamp) { break; } sws_scale(swsCtx, (uint8_t const *const *) pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); // 处理截取后的视频帧 } } av_packet_unref(&packet); } ``` 8. 释放资源 ```c++ sws_freeContext(swsCtx); av_frame_free(&pFrameRGB); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); ``` 其中,`startTimeInSeconds` 和 `endTimeInSeconds` 分别为要截取的视频片段的开始时间和结束时间,以秒为单位。`AV_TIME_BASE` 定义了时间戳的时间基,一般为 1 秒钟的微秒数(1000000)。`AVSEEK_FLAG_BACKWARD` 表示定位到离指定时间戳最近的关键帧之前的位置。

相关推荐

最新推荐

recommend-type

python调用系统ffmpeg实现视频截图、http发送

主要为大家详细介绍了python调用系统ffmpeg实现视频截图、http发送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++实现视频流转换为图片方式

今天小编就为大家分享一篇C++实现视频流转换为图片方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Java使用FFmpeg处理视频文件的方法教程

主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

使用Java和ffmpeg把音频和视频合成视频的操作方法

主要介绍了使用Java和ffmpeg把音频和视频合成视频,本文通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

java使用FFmpeg合成视频和音频并获取视频中的音频等操作(实例代码详解)

主要介绍了java使用FFmpeg合成视频和音频并获取视频中的音频等操作,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。