最简单的基于FFmpeg的直播系统开发移动端例子:IOS 视频解码器

简介: 本文记录IOS平台下基于FFmpeg的视频解码器。该示例C语言的源代码来自于《最简单的基于FFMPEG+SDL的视频播放器》。相关的概念就不再重复记录了。

本文记录IOS平台下基于FFmpeg的视频解码器。该示例C语言的源代码来自于《最简单的基于FFMPEG+SDL的视频播放器》。相关的概念就不再重复记录了。

源代码
项目的目录结构如图所示。

C代码位于ViewController.m文件中,内容如下所示。
/**

  • 最简单的基于FFmpeg的视频解码器-IOS
  • Simplest FFmpeg IOS Decoder
    *
  • 雷霄骅 Lei Xiaohua
  • leixiaohua1020@126.com
  • 中国传媒大学/数字电视技术
  • Communication University of China / Digital TV Technology
  • http://blog.csdn.net/leixiaohua1020
    *
  • 本程序是IOS平台下最简单的基于FFmpeg的视频解码器。
  • 它可以将输入的视频数据解码成YUV像素数据。
    *
  • This software is the simplest decoder based on FFmpeg in IOS.
  • It can decode video stream to raw YUV data.
    *

*/

import "ViewController.h"

include

include

include

include

@interface ViewController ()
@end
@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
  • (IBAction)clickDecodeButton:(id)sender {
    AVFormatContext *pFormatCtx;
    int i, videoindex;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;
    AVFrame pFrame,pFrameYUV;
    uint8_t *out_buffer;
    AVPacket *packet;
    int y_size;
    int ret, got_picture;
    struct SwsContext *img_convert_ctx;
    FILE *fp_yuv;
    int frame_cnt;
    clock_t time_start, time_finish;
    double time_duration = 0.0;

    char input_str_full[500]={0};
    char output_str_full[500]={0};
    char info[1000]={0};

    NSString *input_str= [NSString stringWithFormat:@"resource.bundle/%@",self.inputurl.text];
    NSString *output_str= [NSString stringWithFormat:@"resource.bundle/%@",self.outputurl.text];

    NSString *input_nsstr=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:input_str];
    NSString *output_nsstr=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:output_str];

    sprintf(input_str_full,"%s",[input_nsstr UTF8String]);
    sprintf(output_str_full,"%s",[output_nsstr UTF8String]);

    printf("Input Path:%sn",input_str_full);
    printf("Output Path:%sn",output_str_full);

    av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();

    if(avformat_open_input(&pFormatCtx,input_str_full,NULL,NULL)!=0){

    printf("Couldn't open input stream.\n");
    return ;

    }
    if(avformat_find_stream_info(pFormatCtx,NULL)<0){

    printf("Couldn't find stream information.\n");
    return;

    }
    videoindex=-1;
    for(i=0; inb_streams; i++)

    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
        videoindex=i;
        break;
    }

    if(videoindex==-1){

    printf("Couldn't find a video stream.\n");
    return;

    }
    pCodecCtx=pFormatCtx->streams[videoindex]->codec;
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL){

    printf("Couldn't find Codec.\n");
    return;

    }
    if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){

    printf("Couldn't open codec.\n");
    return;

    }

    pFrame=av_frame_alloc();
    pFrameYUV=av_frame_alloc();
    out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height,1));
    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,

                     AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);

    packet=(AVPacket *)av_malloc(sizeof(AVPacket));

    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,

                                 pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
    

    sprintf(info, "[Input ]%sn", [input_str UTF8String]);
    sprintf(info, "%s[Output ]%sn",info,[output_str UTF8String]);
    sprintf(info, "%s[Format ]%sn",info, pFormatCtx->iformat->name);
    sprintf(info, "%s[Codec ]%sn",info, pCodecCtx->codec->name);
    sprintf(info, "%s[Resolution]%dx%dn",info, pCodecCtx->width,pCodecCtx->height);

    fp_yuv=fopen(output_str_full,"wb+");
    if(fp_yuv==NULL){

    printf("Cannot open output file.\n");
    return;

    }

    frame_cnt=0;
    time_start = clock();

    while(av_read_frame(pFormatCtx, packet)>=0){

    if(packet->stream_index==videoindex){
        ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
        if(ret < 0){
            printf("Decode Error.\n");
            return;
        }
        if(got_picture){
            sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                      pFrameYUV->data, pFrameYUV->linesize);
            
            y_size=pCodecCtx->width*pCodecCtx->height;
            fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
            fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
            fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
            //Output info
            char pictype_str[10]={0};
            switch(pFrame->pict_type){
                case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
                case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
                case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
                default:sprintf(pictype_str,"Other");break;
            }
            printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
            frame_cnt++;
        }
    }
    av_free_packet(packet);

    }
    //flush decoder
    //FIX: Flush Frames remained in Codec
    while (1) {

    ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
    if (ret < 0)
        break;
    if (!got_picture)
        break;
    sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
              pFrameYUV->data, pFrameYUV->linesize);
    int y_size=pCodecCtx->width*pCodecCtx->height;
    fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
    fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
    fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
    //Output info
    char pictype_str[10]={0};
    switch(pFrame->pict_type){
        case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
        case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
        case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
        default:sprintf(pictype_str,"Other");break;
    }
    printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
    frame_cnt++;

    }
    time_finish = clock();
    time_duration=(double)(time_finish - time_start);

    sprintf(info, "%s[Time ]%fusn",info,time_duration);
    sprintf(info, "%s[Count ]%dn",info,frame_cnt);

    sws_freeContext(img_convert_ctx);

    fclose(fp_yuv);

    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    NSString * info_ns = [NSString stringWithFormat:@"%s", info];
    self.infomation.text=info_ns;

}

@end

运行结果
App在手机上运行后的结果如下图所示。单击“Decode”,将会把位于resource.bundle中的“sintel.mov”文件解码为“sintel.yuv”文件并存储于相同的目录下。

本文转载自网络,感谢原作者的分享,转载仅为分享干货知识,如有侵权欢迎联系作者进行删除处理。

相关文章
|
17天前
|
Web App开发 开发框架 前端开发
移动端window.open跳转链接时,iOS没有反应的问题
【10月更文挑战第9天】在移动端使用 `window.open` 跳转链接时,iOS 可能无响应,原因是 iOS 的安全策略和弹出窗口阻止功能。解决方法包括:确保在用户交互后触发 `window.open`,将目标设置为 `_self`,使用锚点链接模拟跳转,或利用专门的移动端框架。需综合考虑这些方案以优化用户体验。
281 61
|
2月前
|
编解码 Linux
CentOS安装ffmpeg并转码视频为mp4
CentOS安装ffmpeg并转码视频为mp4
127 0
|
1月前
|
XML Java Android开发
FFmpeg开发笔记(五十二)移动端的国产视频播放器GSYVideoPlayer
GSYVideoPlayer是一款国产移动端视频播放器,支持弹幕、滤镜、广告等功能,采用IJKPlayer、Media3(EXOPlayer)、MediaPlayer及AliPlayer多种内核。截至2024年8月,其GitHub星标数达2万。集成时需使用新版Android Studio,并按特定步骤配置依赖与权限。提供了NormalGSYVideoPlayer、GSYADVideoPlayer及ListGSYVideoPlayer三种控件,支持HLS、RTMP等多种直播链接。
82 18
FFmpeg开发笔记(五十二)移动端的国产视频播放器GSYVideoPlayer
|
3天前
|
Java 数据安全/隐私保护
Java ffmpeg 实现视频加文字/图片水印功能
【10月更文挑战第22天】在 Java 中使用 FFmpeg 实现视频加文字或图片水印功能,需先安装 FFmpeg 并添加依赖(如 JavaCV)。通过构建 FFmpeg 命令行参数,使用 `drawtext` 滤镜添加文字水印,或使用 `overlay` 滤镜添加图片水印。示例代码展示了如何使用 JavaCV 实现文字水印。
|
4月前
|
Python
Python使用ffmpeg下载m3u8拼接为视频
Python使用ffmpeg下载m3u8拼接为视频
208 1
|
8天前
|
计算机视觉 Python
FFMPEG学习笔记(一): 提取视频的纯音频及无声视频
本文介绍了如何使用FFmpeg工具从视频中提取纯音频和无声视频。提供了具体的命令行操作,例如使用`ffmpeg -i input.mp4 -vn -c:a libmp3lame output.mp3`来提取音频,以及`ffmpeg -i input.mp4 -c:v copy -an output.mp4`来提取无声视频。此外,还包含了一个Python脚本,用于批量处理视频文件,自动提取音频和生成无声视频。
15 1
|
1月前
|
Linux 开发工具 Android开发
FFmpeg开发笔记(五十三)移动端的国产直播录制工具EasyPusher
EasyPusher是一款国产RTSP直播录制推流客户端工具,支持Windows、Linux、Android及iOS等系统。尽管其GitHub仓库(安卓版:https://github.com/EasyDarwin/EasyPusher-Android)已多年未更新,但通过一系列改造,如升级SDK版本、迁移到AndroidX、指定本地NDK版本及更新Gradle版本等,仍可在最新Android Studio上运行。以下是针对Android Studio Dolphin版本的具体改造步骤。
47 3
FFmpeg开发笔记(五十三)移动端的国产直播录制工具EasyPusher
|
15天前
|
Linux 视频直播
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
本文介绍了如何使用EasyPusher-Android实现RTSP直播流程。首先对比了RTSP、RTMP、SRT和RIST四种流媒体协议,并以RTSP为例,详细说明了使用EasyPusher-Android向流媒体服务器进行RTSP直播推流的方法。文中还提供了OBS Studio配置RTSP插件及ZLMediaKit云服务器部署的相关信息,通过修改EasyPusher-Android源码使其支持通用RTSP地址,最终验证了直播功能的成功实现。
33 0
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
|
1月前
|
编解码 API 数据安全/隐私保护
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
【9月更文挑战第21天】本文介绍了如何使用FFmpeg和EasyPusher实现移动端RTSP直播。首先概述了EasyPusher的功能及其API,接着详细描述了安装FFmpeg、获取EasyPusher库、初始化对象、打开输入流、配置推送参数及读取推送帧的具体步骤,并提醒开发者注意网络环境、编码参数和权限管理等问题,以确保直播质量与稳定性。
|
10天前
FFmpeg学习笔记(二):多线程rtsp推流和ffplay拉流操作,并储存为多路avi格式的视频
这篇博客主要介绍了如何使用FFmpeg进行多线程RTSP推流和ffplay拉流操作,以及如何将视频流保存为多路AVI格式的视频文件。
106 0