当前位置:   article > 正文

在VS2017下FFmpeg+SDL编写最简单的视频播放器

在VS2017下FFmpeg+SDL编写最简单的视频播放器

1.下载ShiftMediaProject/FFmpeg

2.下载SDL2

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

3.配置include和lib

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

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

6.编写代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. extern "C" {
  5. #include "libavcodec\avcodec.h"
  6. #include "libavformat\avformat.h"
  7. #include "libavutil\avutil.h"
  8. #include "SDL.h"
  9. }
  10. SDL_Window *window;
  11. SDL_Texture* texture;
  12. SDL_Renderer* renderer;
  13. SDL_Rect rect;
  14. int width;
  15. int height;
  16. void display(AVFrame* frame);
  17. void iniSdl();
  18. #undef main
  19. int main()
  20. {
  21. int ret = -1;
  22. AVFormatContext* ctx = avformat_alloc_context();
  23. ret = avformat_open_input(&ctx, "my.mp4", NULL, NULL);
  24. ret = avformat_find_stream_info(ctx, NULL);
  25. int videoindex = -1;
  26. for (int i = 0; i < ctx->nb_streams; i++)
  27. {
  28. if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  29. {
  30. videoindex = i;
  31. break;
  32. }
  33. }
  34. const AVCodec* codec = avcodec_find_decoder(ctx->streams[videoindex]->codecpar->codec_id);
  35. AVCodecContext* avcc = avcodec_alloc_context3(codec);
  36. ret = avcodec_parameters_to_context(avcc, ctx->streams[videoindex]->codecpar);
  37. ret = avcodec_open2(avcc, codec, NULL);
  38. width = avcc->width;
  39. height = avcc->height;
  40. rect.x = 0;
  41. rect.y = 0;
  42. rect.h = height;
  43. rect.w = width;
  44. iniSdl();
  45. AVFrame* frame = av_frame_alloc();
  46. AVPacket* packet = av_packet_alloc();
  47. while (true)
  48. {
  49. ret = av_read_frame(ctx, packet);
  50. if (ret >=0)
  51. {
  52. if (packet->stream_index == videoindex)
  53. {
  54. ret = avcodec_send_packet(avcc, packet);
  55. while (ret >= 0)
  56. {
  57. ret = avcodec_receive_frame(avcc, frame);
  58. if (ret >= 0)
  59. {
  60. display(frame);
  61. av_frame_unref(frame);
  62. }
  63. else
  64. {
  65. av_frame_unref(frame);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. else
  72. {
  73. av_packet_unref(packet);
  74. break;
  75. }
  76. }
  77. SDL_DestroyWindow(window);
  78. std::cout << "finish\n";
  79. }
  80. void display(AVFrame* frame)
  81. {
  82. SDL_UpdateYUVTexture(texture, &rect, frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2]);
  83. SDL_RenderClear(renderer);
  84. SDL_RenderCopy(renderer, texture, NULL, NULL);
  85. SDL_RenderPresent(renderer);
  86. }
  87. void iniSdl()
  88. {
  89. SDL_Init(SDL_INIT_VIDEO);
  90. window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_RESIZABLE);
  91. renderer = SDL_CreateRenderer(window, -1, 0);
  92. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, width, height);
  93. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/837790
推荐阅读
相关标签
  

闽ICP备14008679号