当前位置: 首页 > news >正文

FFmpeg播放视频

VS2017+FFmpeg6.2.r113110+SDL2.30.5

1.下载

ShiftMediaProject/FFmpeg

2.下载SDL2

3.新建VC++控制台应用

3.配置include和lib

 4.把FFmpeg和SDL的dll 复制到工程Debug目录下,并设置调试命令

5.复制一下mp4视频到工程Debug目录下(复制一份到*.vcxproj同一目录,用于调试)

6.编写代码

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <chrono>
#include <thread>extern "C" {
#include "libavcodec\avcodec.h"
#include "libavformat\avformat.h"
#include "libavutil\avutil.h"
#include "SDL.h"
}SDL_Window *window;
SDL_Texture* texture;
SDL_Renderer* renderer;
SDL_Rect rect;
int width;
int height;void display(AVFrame* frame);
void iniSdl();#undef main
int main()
{int ret = -1;AVFormatContext* ctx = avformat_alloc_context();ret = avformat_open_input(&ctx, "my.mp4", NULL, NULL);ret = avformat_find_stream_info(ctx, NULL);int videoindex = -1;for (int i = 0; i < ctx->nb_streams; i++){if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){videoindex = i;break;}}const AVCodec* codec = avcodec_find_decoder(ctx->streams[videoindex]->codecpar->codec_id);AVCodecContext* avcc = avcodec_alloc_context3(codec);ret = avcodec_parameters_to_context(avcc, ctx->streams[videoindex]->codecpar);ret = avcodec_open2(avcc, codec, NULL);AVRational time_base = ctx->streams[videoindex]->time_base;auto steady_start = std::chrono::steady_clock::now();width = avcc->width;height = avcc->height;rect.x = 0;rect.y = 0;rect.h = height;rect.w = width;iniSdl();AVFrame* frame = av_frame_alloc();AVPacket* packet = av_packet_alloc();while (true){ret = av_read_frame(ctx, packet);if (ret >=0){if (packet->stream_index == videoindex){ret = avcodec_send_packet(avcc, packet);while (ret >= 0){ret = avcodec_receive_frame(avcc, frame);if (ret >= 0){double tm = 1000.0*frame->pts * av_q2d(time_base);auto steady_end = std::chrono::steady_clock::now();std::chrono::duration<double, std::milli> elapsed = steady_end - steady_start;int span2 = tm - elapsed.count();std::cout << tm << "-" << elapsed.count() << "\n";std::this_thread::sleep_for(std::chrono::milliseconds(span2));display(frame);av_frame_unref(frame);}else{av_frame_unref(frame);break;}}}			}else{av_packet_unref(packet);break;}}SDL_DestroyWindow(window);std::cout << "finish\n";
}void display(AVFrame* frame)
{SDL_UpdateYUVTexture(texture, &rect, frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2]);SDL_RenderClear(renderer);SDL_RenderCopy(renderer, texture, NULL, NULL);SDL_RenderPresent(renderer);
}void iniSdl()
{SDL_Init(SDL_INIT_VIDEO);window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width/2, height/2, SDL_WINDOW_RESIZABLE);renderer = SDL_CreateRenderer(window, -1, 0);texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, width, height);
}

rtsp 

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <chrono>
#include <thread>extern "C" {
#include "libavcodec\avcodec.h"
#include "libavformat\avformat.h"
#include "libavutil\avutil.h"
#include "SDL.h"
}SDL_Window *window;
SDL_Texture* texture;
SDL_Renderer* renderer;
SDL_Rect rect;
int width;
int height;void display(AVFrame* frame);
void iniSdl();#undef main
int main()
{// 加载socket库以及网络加密协议相关的库,为后续使用网络相关提供支持avformat_network_init();int ret = -1;AVFormatContext* ctx = avformat_alloc_context();ret = avformat_open_input(&ctx, "rtsp://admin:admin@192.168.43.110:554/stream1", NULL, NULL);ret = avformat_find_stream_info(ctx, NULL);int videoindex = -1;for (int i = 0; i < ctx->nb_streams; i++){if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){videoindex = i;break;}}const AVCodec* codec = avcodec_find_decoder(ctx->streams[videoindex]->codecpar->codec_id);AVCodecContext* avcc = avcodec_alloc_context3(codec);ret = avcodec_parameters_to_context(avcc, ctx->streams[videoindex]->codecpar);ret = avcodec_open2(avcc, codec, NULL);AVRational time_base = ctx->streams[videoindex]->time_base;auto steady_start = std::chrono::steady_clock::now();width = avcc->width;height = avcc->height;rect.x = 0;rect.y = 0;rect.h = height;rect.w = width;iniSdl();AVFrame* frame = av_frame_alloc();AVPacket* packet = av_packet_alloc();while (true){ret = av_read_frame(ctx, packet);if (ret >=0){if (packet->stream_index == videoindex){ret = avcodec_send_packet(avcc, packet);while (ret >= 0){ret = avcodec_receive_frame(avcc, frame);if (ret >= 0){/*double tm = 1000.0*frame->pts * av_q2d(time_base);auto steady_end = std::chrono::steady_clock::now();std::chrono::duration<double, std::milli> elapsed = steady_end - steady_start;int span2 = tm - elapsed.count();std::cout << tm << "-" << elapsed.count() << "\n";std::this_thread::sleep_for(std::chrono::milliseconds(span2));*/display(frame);av_frame_unref(frame);}else{av_frame_unref(frame);break;}}}			}else{av_packet_unref(packet);break;}}SDL_DestroyWindow(window);std::cout << "finish\n";
}void display(AVFrame* frame)
{SDL_UpdateYUVTexture(texture, &rect, frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2]);SDL_RenderClear(renderer);SDL_RenderCopy(renderer, texture, NULL, NULL);SDL_RenderPresent(renderer);
}void iniSdl()
{SDL_Init(SDL_INIT_VIDEO);window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_RESIZABLE);renderer = SDL_CreateRenderer(window, -1, 0);texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, width, height);
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 图扑低代码数字孪生 Web SCADA 智慧钢厂
  • 数据挖掘新技能:Python爬虫编程指南
  • StarRocks on AWS Graviton3,实现 50% 以上性价比提升
  • Linux 操作指令
  • OPC UA边缘计算耦合器BL205工业通信的最佳解决方案
  • 区块链技术在溯源领域的应用
  • 板级调试小助手(4)基于C语言的自定义脚本解析器
  • 【Git远程操作】理解分布式管理 | 创建远程仓库
  • 3、宠物商店智能合约实战(truffle智能合约项目实战)
  • GPT-4o大语言模型优化、本地私有化部署、从0-1搭建、智能体构建
  • Python--pyecharts 入门笔记
  • 《基于 CDC、Spark Streaming、Kafka 实现患者指标采集》
  • LeetCode 3112.访问消失节点的最少时间:单源最短路的Dijkstra算法
  • Nginx详解(超级详细)
  • Mac Electron 应用如何进行签名(signature)和公证(notarization)?
  • 深入了解以太坊
  • [deviceone开发]-do_Webview的基本示例
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Android Studio:GIT提交项目到远程仓库
  • Angularjs之国际化
  • Debian下无root权限使用Python访问Oracle
  • happypack两次报错的问题
  • IOS评论框不贴底(ios12新bug)
  • Iterator 和 for...of 循环
  • Laravel Telescope:优雅的应用调试工具
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • vue脚手架vue-cli
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 嵌入式文件系统
  • 如何在 Tornado 中实现 Middleware
  • 说说动画卡顿的解决方案
  • 我的业余项目总结
  • 原生Ajax
  • #AngularJS#$sce.trustAsResourceUrl
  • #Datawhale AI夏令营第4期#AIGC方向 文生图 Task2
  • #如何使用 Qt 5.6 在 Android 上启用 NFC
  • (1)svelte 教程:hello world
  • (3)医疗图像处理:MRI磁共振成像-快速采集--(杨正汉)
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (C++二叉树05) 合并二叉树 二叉搜索树中的搜索 验证二叉搜索树
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (PySpark)RDD实验实战——取最大数出现的次数
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)ssm码农论坛 毕业设计 231126
  • (力扣)1314.矩阵区域和
  • (三)mysql_MYSQL(三)
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • (转)C#调用WebService 基础
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转)jQuery 基础
  • (转)mysql使用Navicat 导出和导入数据库