FFmpeg筆記(七)-- 視頻轉(zhuǎn)為多張圖片

1.初始化格式上下文。
    AVFormatContext *formatCtx = avformat_alloc_context();
    if (avformat_open_input(&formatCtx, [videoPath UTF8String], NULL, NULL) < 0) {
        NSLog(@"不能打開輸入文件");
        return nil;
    }
    
    if (avformat_find_stream_info(formatCtx, NULL) < 0) {
        NSLog(@"沒有流信息");
        return nil;
    }
2.獲取視頻流。
    int stream_index = av_find_best_stream(formatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    if (stream_index < 0) {
        NSLog(@"沒有發(fā)現(xiàn)視頻流");
        return nil;
    }
    AVStream *videoStream = formatCtx->streams[stream_index];
3.獲取解碼器趟据、解碼上下文。
    AVCodecContext *codeCtx = avcodec_alloc_context3(NULL);
    if (!codeCtx) {
        NSLog(@"沒有發(fā)現(xiàn)解碼上下文");
        return nil;
    }

    if ((avcodec_parameters_to_context(codeCtx, videoStream->codecpar)) < 0) {
        NSLog(@"復(fù)制解碼參數(shù)失敗");
        return nil;
    }
    avcodec_open2(codeCtx, codec, NULL);
4.解碼出視頻幀术健。
    AVPacket *packet = av_packet_alloc();
    AVFrame *frame = av_frame_alloc();
    int frameCount = 0;
    NSMutableArray *imgArray = [NSMutableArray array];
    while (av_read_frame(formatCtx, packet) >= 0) {
        if (packet->stream_index == stream_index) {
            int send_result = avcodec_send_packet(codeCtx, packet);
            int receive_result = 0;
            if (send_result == AVERROR(EAGAIN)) {
                while (receive_result != AVERROR(EAGAIN)) {
                    receive_result = avcodec_receive_frame(codeCtx, frame);
                    if (receive_result == 0) {
                        if (onlyIFrame && frame->pict_type != AV_PICTURE_TYPE_I) {
                            continue;
                        }

                        NSString *imagePath = [DYMediaTools creatFile:[NSString stringWithFormat:@"img%@",@(frameCount++)] ofType:@"jpg"];
                        int r = frameToJPG(frame, [imagePath UTF8String]);
                        if (r == 0){
                            [imgArray addObject:[UIImage imageWithContentsOfFile:imagePath]];
                        }
                    }
                }
            } else {
                receive_result = avcodec_receive_frame(codeCtx, frame);
                if (receive_result == AVERROR(EAGAIN)) {
                    continue;
                } else if (receive_result == 0) {
                    if (onlyIFrame && frame->pict_type != AV_PICTURE_TYPE_I) {
                        continue;
                    }
        
                    NSString *imagePath = [DYMediaTools creatFile:[NSString stringWithFormat:@"img%@",@(frameCount++)] ofType:@"jpg"];
                    int r = frameToJPG(frame, [imagePath UTF8String]);
                    if (r == 0){
                        [imgArray addObject:[UIImage imageWithContentsOfFile:imagePath]];
                    }
                }
            }
        }
        av_packet_unref(packet);
    }
5.將視頻幀編碼為jpg汹碱。
int frameToJPG(AVFrame *videoFrame,const char *imageName) {
    
    AVFormatContext *formatCtx = avformat_alloc_context();
    avformat_alloc_output_context2(&formatCtx, NULL, NULL, imageName);

    if (avio_open(&formatCtx->pb, imageName, AVIO_FLAG_READ_WRITE) < 0) {
        NSLog(@"打開輸出文件失敗");
        return -1;
    }

    AVStream *pAVStream = avformat_new_stream(formatCtx, 0);
    if (pAVStream == NULL) {
        return -1;
    }

    AVCodecParameters *parameters = pAVStream->codecpar;
    parameters->codec_id = formatCtx->oformat->video_codec;
    parameters->codec_type = AVMEDIA_TYPE_VIDEO;
    parameters->format = AV_PIX_FMT_YUVJ420P;
    parameters->width = videoFrame->width;
    parameters->height = videoFrame->height;

    AVCodec *codec = avcodec_find_encoder(pAVStream->codecpar->codec_id);

    if (!codec) {
        NSLog(@"沒有發(fā)現(xiàn)編碼器");
        return -1;
    }

    AVCodecContext *codectx = avcodec_alloc_context3(codec);
    if (!codectx) {
        NSLog(@"沒有發(fā)現(xiàn)編碼上下文");
        return -1;
    }

    if ((avcodec_parameters_to_context(codectx, pAVStream->codecpar)) < 0) {
        NSLog(@"復(fù)制編碼參數(shù)失敗");
        return -1;
    }

    codectx->time_base = (AVRational) {1, 25};

    if (avcodec_open2(codectx, codec, NULL) < 0) {
        NSLog(@"打開編碼器失敗");
        return -1;
    }

    int ret = avformat_write_header(formatCtx, NULL);
    if (ret < 0) {
        NSLog(@"頭文件寫入失敗");
        return -1;
    }

    AVPacket *packet = av_packet_alloc();
    //分配足夠的空間保證能一次接收完
    av_new_packet(packet, videoFrame->width * videoFrame->height * 3);
    ret = avcodec_send_frame(codectx, videoFrame);
    if (ret < 0) {
        NSLog(@"發(fā)送視頻幀失敗");
        return -1;
    }

    ret = avcodec_receive_packet(codectx, packet);
    if (ret < 0) {
        NSLog(@"接收視頻幀失敗");
        return -1;
    }

    ret = av_write_frame(formatCtx, packet);

    if (ret < 0) {
        NSLog(@"寫入幀失敗");
        return -1;
    }

    av_packet_free(&packet);
    av_write_trailer(formatCtx);
    
    avcodec_close(codectx);
    avio_close(formatCtx->pb);
    avformat_free_context(formatCtx);
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市荞估,隨后出現(xiàn)的幾起案子咳促,更是在濱河造成了極大的恐慌稚新,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件跪腹,死亡現(xiàn)場離奇詭異褂删,居然都是意外死亡,警方通過查閱死者的電腦和手機冲茸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門屯阀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人轴术,你說我怎么就攤上這事难衰。” “怎么了逗栽?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵盖袭,是天一觀的道長。 經(jīng)常有香客問我彼宠,道長鳄虱,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任兵志,我火速辦了婚禮醇蝴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘想罕。我一直安慰自己悠栓,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布按价。 她就那樣靜靜地躺著惭适,像睡著了一般。 火紅的嫁衣襯著肌膚如雪楼镐。 梳的紋絲不亂的頭發(fā)上癞志,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機與錄音框产,去河邊找鬼凄杯。 笑死,一個胖子當(dāng)著我的面吹牛秉宿,可吹牛的內(nèi)容都是我干的戒突。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼描睦,長吁一口氣:“原來是場噩夢啊……” “哼膊存!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤隔崎,失蹤者是張志新(化名)和其女友劉穎今艺,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體爵卒,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡虚缎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了技潘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遥巴。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖享幽,靈堂內(nèi)的尸體忽然破棺而出铲掐,到底是詐尸還是另有隱情,我是刑警寧澤值桩,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布摆霉,位于F島的核電站,受9級特大地震影響奔坟,放射性物質(zhì)發(fā)生泄漏携栋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一咳秉、第九天 我趴在偏房一處隱蔽的房頂上張望婉支。 院中可真熱鬧,春花似錦澜建、人聲如沸向挖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽何之。三九已至,卻和暖如春咽筋,著一層夾襖步出監(jiān)牢的瞬間溶推,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工奸攻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蒜危,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓睹耐,卻偏偏與公主長得像舰褪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子疏橄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354