• 【FFMPEG应用篇】MP4转YUV存储


    1. extern "C" {
    2. #include
    3. #include
    4. #include
    5. #include
    6. }
    7. int main() {
    8. av_register_all();
    9. AVFormatContext* formatContext = avformat_alloc_context();
    10. if (avformat_open_input(&formatContext, "4k.mp4", NULL, NULL) != 0) {
    11. fprintf(stderr, "Error opening input file.\n");
    12. return -1;
    13. }
    14. if (avformat_find_stream_info(formatContext, NULL) < 0) {
    15. fprintf(stderr, "Error finding stream information.\n");
    16. return -1;
    17. }
    18. av_dump_format(formatContext, 0, "4k.mp4", 0);
    19. AVCodec* codec = NULL;
    20. AVCodecContext* codecContext = NULL;
    21. int videoStreamIndex = -1;
    22. for (int i = 0; i < formatContext->nb_streams; i++) {
    23. if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
    24. videoStreamIndex = i;
    25. codec = avcodec_find_decoder(formatContext->streams[i]->codecpar->codec_id);
    26. codecContext = avcodec_alloc_context3(codec);
    27. avcodec_parameters_to_context(codecContext, formatContext->streams[i]->codecpar);
    28. avcodec_open2(codecContext, codec, NULL);
    29. break;
    30. }
    31. }
    32. AVPacket packet;
    33. av_init_packet(&packet);
    34. AVFrame* frame = av_frame_alloc();
    35. AVFrame* frameRGB = av_frame_alloc();
    36. struct SwsContext* swsContext = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt, codecContext->width, codecContext->height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);
    37. while (av_read_frame(formatContext, &packet) >= 0) {
    38. if (packet.stream_index == videoStreamIndex) {
    39. int response = avcodec_send_packet(codecContext, &packet);
    40. if (response < 0) {
    41. fprintf(stderr, "Error decoding frame.\n");
    42. return -1;
    43. }
    44. response = avcodec_receive_frame(codecContext, frame);
    45. if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
    46. continue;
    47. }
    48. else if (response < 0) {
    49. fprintf(stderr, "Error decoding frame.\n");
    50. return -1;
    51. }
    52. av_image_alloc(frameRGB->data, frameRGB->linesize, codecContext->width, codecContext->height, AV_PIX_FMT_YUV420P, 1);
    53. sws_scale(swsContext, (uint8_t const* const*)frame->data, frame->linesize, 0, codecContext->height, frameRGB->data, frameRGB->linesize);
    54. // Save frame as YUV file
    55. FILE* yuvFile = fopen("4k.yuv", "wb");
    56. if (yuvFile) {
    57. fwrite(frameRGB->data[0], 1, codecContext->width * codecContext->height, yuvFile); // Y
    58. fwrite(frameRGB->data[1], 1, codecContext->width * codecContext->height / 4, yuvFile); // U
    59. fwrite(frameRGB->data[2], 1, codecContext->width * codecContext->height / 4, yuvFile); // V
    60. fclose(yuvFile);
    61. }
    62. av_frame_unref(frame);
    63. av_frame_unref(frameRGB);
    64. }
    65. av_packet_unref(&packet);
    66. }
    67. av_frame_free(&frame);
    68. av_frame_free(&frameRGB);
    69. avcodec_free_context(&codecContext);
    70. avformat_close_input(&formatContext);
    71. avformat_free_context(formatContext);
    72. sws_freeContext(swsContext);
    73. return 0;
    74. }

  • 相关阅读:
    WebGPU学习路径与资源分享
    回归测试?
    应用程序管理工具
    【现代信号处理第六次作业】
    再也不怕面试官拷打Go数据结构了!-Go语言map详解
    C# 代码合集
    Java—Set
    元宇宙在技术大爆炸时代迎来链游新世界
    LeetCode生成匹配的括号
    如何在树莓派上安装cpolar内网穿透
  • 原文地址:https://blog.csdn.net/qq_40179458/article/details/139168750