Android中使用FFmpeg得到視頻中的PCM和YUV數(shù)據(jù)

使用FFmpeg獲取PCM和YUV數(shù)據(jù)的流程基本上一樣的,下面就以獲取YUV數(shù)據(jù)的流程為例,說(shuō)明這個(gè)過(guò)程:

  • 初始化AVFormatContext 。
  • 打開(kāi)文件,獲取流信息,獲取視頻流/音頻流。
  • 找到解碼器,并且初始化解碼器。
  • 初始化AVPacket ,AVFrame ,和buffer。
  • 對(duì)輸出格式進(jìn)行規(guī)范,如視頻的寬高,音頻的采用率,聲道數(shù)等。
  • 讀取一幀數(shù)據(jù),然后把數(shù)據(jù)寫(xiě)入到文件。
  • 讀完數(shù)據(jù)后,釋放內(nèi)存。
#include <jni.h>
#include <string.h>
#include <stdlib.h>
#include "android/log.h"

extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
};

#define LOGD(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"jni",FORMAT,##__VA_ARGS__);

extern "C"
JNIEXPORT jint JNICALL
Java_audioplayer_MainActivity_getYuvData(JNIEnv *env, jobject instance, jstring srcPath_,
                                                   jstring desPath_) {
    const char *srcPath = env->GetStringUTFChars(srcPath_, 0);
    const char *desPath = env->GetStringUTFChars(desPath_, 0);

    AVFormatContext *pFormatCtx;
    int i,videoIndex;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;

    /* init */
    av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();

    /* open file,find streams,and then find a video stream */
    if(avformat_open_input(&pFormatCtx,srcPath,NULL,NULL)!=0){
        LOGD("Couldn't open input stream.");
        return -1;
    }
    if(avformat_find_stream_info(pFormatCtx,NULL)<0){
        LOGD("Couldn't find stream information.");
        return -1;
    }
    videoIndex = -1;
    for(i=0;i<pFormatCtx->nb_streams;i++){
        if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
            videoIndex = i;
            break;
        }
    }
    if(videoIndex == -1){
        LOGD("Don't find a video stream.");
        return -1;
    }

    /* find a decoder */
    pCodecCtx = pFormatCtx->streams[videoIndex]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec == NULL){
        LOGD("Codec not found.\n");
        return -1;
    }
    if(avcodec_open2(pCodecCtx,pCodec,NULL)<0){
        LOGD("Could not open codec.");
        return -1;
    }

    LOGD("File format: %s.\nVideo duration: %lld.\nVideo width: %d,Video height: %d.",
        pFormatCtx->iformat->name,pFormatCtx->duration,pCodecCtx->width,pCodecCtx->height);

    /* init Buffer */
    AVPacket *packet = (AVPacket *)malloc(sizeof(AVPacket));
    AVFrame *pFrame = av_frame_alloc();
    AVFrame *pFrameYuv = av_frame_alloc();
    uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P,pCodecCtx->width,pCodecCtx->height));
    avpicture_fill((AVPicture *)pFrameYuv,out_buffer,AV_PIX_FMT_YUV420P,pCodecCtx->width,pCodecCtx->height);

    struct SwsContext *swsContext= sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
        pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
    int ret,got_picture;

    FILE *fp = fopen(desPath,"wb+");
    int frame_cnt = 0;

    /* ever time read a frame and decode it */
    while(av_read_frame(pFormatCtx,packet)>=0){
        if(packet->stream_index == videoIndex){
            ret = avcodec_decode_video2(pCodecCtx,pFrame,&got_picture,packet);
            if(ret < 0){
                LOGD("Decode Error.");
                return -1;
            }
            if(got_picture){
                sws_scale(swsContext, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                          pFrameYuv->data, pFrameYuv->linesize);
                LOGD("Decoded frame index: %d\n",frame_cnt);
                fwrite(pFrameYuv->data[0],1,pCodecCtx->width * pCodecCtx->height,fp);
                fwrite(pFrameYuv->data[1],1,pCodecCtx->width * pCodecCtx->height / 4,fp);
                fwrite(pFrameYuv->data[2],1,pCodecCtx->width * pCodecCtx->height / 4,fp);
                fflush(fp);
                frame_cnt++;
            }
        }
        av_free_packet(packet);
    }

    LOGD("Decode end");
    fclose(fp);
    sws_freeContext(swsContext);
    av_frame_free(&pFrameYuv);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    env->ReleaseStringUTFChars(srcPath_, srcPath);
    env->ReleaseStringUTFChars(desPath_, desPath);
    return 0;
}

extern "C"
JNIEXPORT jint JNICALL
Java_audioplayer_MainActivity_getPcmData(JNIEnv *env, jobject instance, jstring srcPath_,
                                                   jstring desPath_) {
    const char *srcPath = env->GetStringUTFChars(srcPath_, 0);
    const char *desPath = env->GetStringUTFChars(desPath_, 0);

    AVFormatContext *pFormatCtx;
    int i,audioIndex;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;

    /* init */
    av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();

    /* open file,find streams,and then find a video stream */
    if(avformat_open_input(&pFormatCtx,srcPath,NULL,NULL)!=0){
        LOGD("Couldn't open input stream.");
        return -1;
    }
    if(avformat_find_stream_info(pFormatCtx,NULL)<0){
        LOGD("Couldn't find stream information.");
        return -1;
    }
    audioIndex = -1;
    for(i=0;i<pFormatCtx->nb_streams;i++){
        if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO){
            audioIndex = i;
            break;
        }
    }
    if(audioIndex == -1){
        LOGD("Don't find a video stream.");
        return -1;
    }

    /* find a decoder */
    pCodecCtx = pFormatCtx->streams[audioIndex]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec == NULL){
        LOGD("Codec not found.\n");
        return -1;
    }
    if(avcodec_open2(pCodecCtx,pCodec,NULL)<0){
        LOGD("Could not open codec.");
        return -1;
    }

    LOGD("File format: %s.\nVideo duration: %lld.\nVideo width: %d,Video height: %d.",
         pFormatCtx->iformat->name,pFormatCtx->duration,pCodecCtx->width,pCodecCtx->height);

    /* init Buffer */
    AVPacket *packet = (AVPacket *)malloc(sizeof(AVPacket));
    AVFrame *pFrame = av_frame_alloc();

    /* change sample rate and format to a standard format */
    SwrContext *swrCtx = swr_alloc();
    enum AVSampleFormat in_sample_fmt = pCodecCtx->sample_fmt;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
    int in_sample_rate = pCodecCtx->sample_rate;
    int out_sample_rate = 44100;
    uint64_t in_ch_layout = pCodecCtx->channel_layout;
    uint64_t  out_ch_layout = AV_CH_LAYOUT_STEREO;

    swr_alloc_set_opts(swrCtx,out_ch_layout,out_sample_fmt,out_sample_rate,
        in_ch_layout,in_sample_fmt,in_sample_rate,0,NULL);
    swr_init(swrCtx);

    int out_channel_nb = av_get_channel_layout_nb_channels(out_ch_layout);
    uint8_t *out_buffer = (uint8_t *) av_malloc(2 * 44100);
    FILE *fp = fopen(desPath,"wb+");

    int ret,got_frame,frame_cnt = 0;

    while(av_read_frame(pFormatCtx,packet)>=0){
        if(packet->stream_index == audioIndex){
            ret = avcodec_decode_audio4(pCodecCtx,pFrame,&got_frame,packet);
            if(ret < 0){
                LOGD("Decode end");
            }
            if(got_frame){
                LOGD("Decoded frame index: %d\n",frame_cnt);
                swr_convert(swrCtx,&out_buffer,2 * 44100,(const uint8_t **)pFrame->data,pFrame->nb_samples);
                int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel_nb, pFrame->nb_samples, out_sample_fmt, 1);
                fwrite(out_buffer, 1, out_buffer_size, fp);
                fflush(fp);
                frame_cnt++;
            }

        }
        av_free_packet(packet);
    }
    LOGD("Decode finish");
    fclose(fp);
    swr_free(&swrCtx);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    env->ReleaseStringUTFChars(srcPath_, srcPath);
    env->ReleaseStringUTFChars(desPath_, desPath);
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • FFmpeg 介紹 FFmpeg是一套可以用來(lái)記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開(kāi)源計(jì)算機(jī)程序。采用LG...
    Y了個(gè)J閱讀 11,595評(píng)論 0 28
  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,992評(píng)論 0 3
  • ### YUV顏色空間 視頻是由一幀一幀的數(shù)據(jù)連接而成,而一幀視頻數(shù)據(jù)其實(shí)就是一張圖片。 yuv是一種圖片儲(chǔ)存格式...
    天使君閱讀 3,682評(píng)論 0 4
  • ffmpeg是一個(gè)非常有用的命令行程序,它可以用來(lái)轉(zhuǎn)碼媒體文件。它是領(lǐng)先的多媒體框架FFmpeg的一部分,其有很多...
    城市之光閱讀 7,062評(píng)論 3 6
  • 現(xiàn)狀:現(xiàn)在視頻直播非常的火,所以在視頻直播開(kāi)發(fā)中,使用的對(duì)視頻進(jìn)行遍解碼的框架顯得尤為重要了,其實(shí),這種框架蠻多的...
    ZHANG_GO閱讀 3,282評(píng)論 0 2

友情鏈接更多精彩內(nèi)容