ZBLOG

ffmpeg 解码调用

在使用 FFmpeg 进行解码时,可以通过 C API 来调用相关功能。下面是一个简单的例子,展示如何使用 FFmpeg 解码视频文件。

准备工作

首先,确保你已经安装了 FFmpeg,并且有相应的开发库(例如 libavcodeclibavformatlibavutil)。

示例代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libavformat/avformat.h>
  4. #include <libavcodec/avcodec.h>
  5. int main(int argc, char *argv[]) {
  6. const char *filename = "input.mp4"; // 输入文件名
  7. AVFormatContext *formatContext = NULL;
  8. int videoStreamIndex;
  9. // 注册所有格式和编解码器
  10. av_register_all();
  11. // 打开视频文件
  12. if (avformat_open_input(&formatContext, filename, NULL, NULL) != 0) {
  13. fprintf(stderr, "Could not open source file %s\n", filename);
  14. return -1;
  15. }
  16. // 检查输入流信息
  17. if (avformat_find_stream_info(formatContext, NULL) < 0) {
  18. fprintf(stderr, "Could not find stream information\n");
  19. return -1;
  20. }
  21. // 查找视频流
  22. videoStreamIndex = -1;
  23. for (int i = 0; i < formatContext->nb_streams; i++) {
  24. if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  25. videoStreamIndex = i;
  26. break;
  27. }
  28. }
  29. if (videoStreamIndex == -1) {
  30. fprintf(stderr, "Could not find a video stream\n");
  31. return -1;
  32. }
  33. AVCodecParameters *codecParameters = formatContext->streams[videoStreamIndex]->codecpar;
  34. // 找到解码器
  35. AVCodec *codec = avcodec_find_decoder(codecParameters->codec_id);
  36. if (!codec) {
  37. fprintf(stderr, "Unsupported codec!\n");
  38. return -1;
  39. }
  40. // 创建解码上下文
  41. AVCodecContext *codecContext = avcodec_alloc_context3(codec);
  42. if (!codecContext) {
  43. fprintf(stderr, "Could not allocate codec context\n");
  44. return -1;
  45. }
  46. // 将参数复制到解码上下文中
  47. if (avcodec_parameters_to_context(codecContext, codecParameters) < 0) {
  48. fprintf(stderr, "Could not copy codec parameters to context\n");
  49. return -1;
  50. }
  51. // 打开解码器
  52. if (avcodec_open2(codecContext, codec, NULL) < 0) {
  53. fprintf(stderr, "Could not open codec\n");
  54. return -1;
  55. }
  56. // 解码循环
  57. AVPacket packet;
  58. AVFrame *frame = av_frame_alloc();
  59. while (av_read_frame(formatContext, &packet) >= 0) {
  60. if (packet.stream_index == videoStreamIndex) {
  61. // 解码帧数据
  62. int response = avcodec_send_packet(codecContext, &packet);
  63. if (response >= 0) {
  64. while (response >= 0) {
  65. response = avcodec_receive_frame(codecContext, frame);
  66. if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)
  67. break;
  68. else if (response < 0) {
  69. fprintf(stderr,"Error during decoding\n");
  70. break;
  71. }
  72. printf("Decoded frame: %d\n", codecContext->frame_number);
  73. }
  74. }
  75. }
  76. av_packet_unref(&packet); // 释放包内存
  77. }
  78. // 清理资源
  79. av_frame_free(&frame);
  80. avcodec_free_context(&codecContext);
  81. avformat_close_input(&formatContext);
  82. return 0;
  83. }

编译与运行

确保你链接了 FFmpeg 库。在 Linux 上,你可以用以下命令编译:

  1. gcc decode_example.c -o decode_example -lavformat -lavfilter -lavdevice -lavutil -lavcodecs

然后执行程序:

  1. ./decode_example

注意事项

  • 确保你的 FFmpeg 安装包含 libav* 库。
  • 根据需要修改输入文件名。
  • 此示例只处理了基本的解码过程,实际应用中可能需要更复杂的错误处理和资源管理。

以上就是使用 FFmpeg 解码视频文件的一个简单示例,希望能对你有所帮助!如果有其他问题,请随时问我。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=6151

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?