FFmpeg分離出PCM數(shù)據

本文將使用FFmpeg從mp4/aac/mp3等包含音頻流的多媒體文件中提取PCM數(shù)據保存到本地。

一、使用命令行提取

ffmpeg -i input.mp4 -ar 44100 -ac 2 -f s16le output.pcm

就這樣一條命令搞定,非常簡單。

二、使用代碼編程提取

主要步驟如下:
1、使用avformat_open_input函數(shù)打開輸入文件獲取AVFormatContext上下文ifmt_ctx;
2、使用avformat_find_stream_info函數(shù)查找流信息;
3、使用av_find_best_stream函數(shù)得到音頻流的索引a_stream_index和解碼器a_dec;
4、通過上面得到的a_dec使用avcodec_alloc_context3函數(shù)初始化解碼器,得到解碼器上下文AVCodecContext a_dec_ctx;
5、通過avcodec_parameters_to_context函數(shù)把音頻流的參數(shù)信息賦值給a_dec_ctx;
6、使用avcodec_open2函數(shù)打開編碼器;
7、使用av_read_frame函數(shù)從ifmt_ctx上下文中讀取數(shù)據包;
8、使用avcodec_send_packet、avcodec_receive_frame函數(shù)解碼,得到原始數(shù)據AVFrame,這就是咱們需要的PCM數(shù)據,再通過SwrContext轉換成我們需要的格式,保存到本地就行了。
代碼如下:
初始化輸入輸出文件的路徑

self.inputPath = [[NSBundle mainBundle] pathForResource:@"bb1.mp4" ofType:nil];
self.outputPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"bb1_44100_2_s16le.pcm"];
NSLog(@"%@", self.outputPath);
[NSFileManager.defaultManager removeItemAtPath:self.outputPath error:nil];
[[NSFileManager defaultManager] createFileAtPath:self.outputPath contents:nil attributes:nil];
self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.outputPath];

解碼多媒體文件并保存到本地

    AVFormatContext *ifmt_ctx = NULL;
    int a_stream_index = -1;
    AVCodec *a_dec = NULL;
    AVCodecContext *a_dec_ctx = NULL;
    AVPacket *pkt;
    AVFrame *frame;
    SwrContext *swr_ctx;
    int ret;
    ret = avformat_open_input(&ifmt_ctx, self.inputPath.UTF8String, NULL, NULL);
    if (ret<0) {
        NSLog(@"avformat_open_input出錯了");
        return;
    }
    avformat_find_stream_info(ifmt_ctx, NULL);
    a_stream_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &a_dec, 0);
    if (a_stream_index<0) {
        NSLog(@"沒有音頻流");
        goto __FAIL;
    }
    a_dec_ctx = avcodec_alloc_context3(a_dec);
    a_dec_ctx->pkt_timebase = ifmt_ctx->streams[a_stream_index]->time_base;
    avcodec_parameters_to_context(a_dec_ctx, ifmt_ctx->streams[a_stream_index]->codecpar);
    if (!a_dec_ctx) {
        NSLog(@"avcodec_alloc_context3出錯");
        goto __FAIL;
    }
    ret = avcodec_open2(a_dec_ctx, a_dec, NULL);

    if (ret<0) {
        NSLog(@"avcodec_open2出錯");
        goto __FAIL;
    }
    frame = av_frame_alloc();
    pkt = av_packet_alloc();
    int64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
    int out_sample_rate = 44100;
    
    swr_ctx = swr_alloc_set_opts(NULL, out_ch_layout, out_sample_fmt, out_sample_rate, a_dec_ctx->channel_layout, a_dec_ctx->sample_fmt, a_dec_ctx->sample_rate, 0, NULL);
    ret = swr_init(swr_ctx);
    
    uint8_t *out_buffer = (uint8_t *)av_malloc(44100*2);
    
    while (1) {
        ret = av_read_frame(ifmt_ctx, pkt);
        if (ret<0) {
            break;
        }
        if (pkt->stream_index!=a_stream_index) {
            NSLog(@"%d", pkt->size);
            continue;
        }
        ret = avcodec_send_packet(a_dec_ctx, pkt);
        if (ret<0) {
            NSLog(@"avcodec_send_packet出錯");
            break;
        }
        while (1) {
            ret = avcodec_receive_frame(a_dec_ctx, frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }
            
            swr_convert(swr_ctx, &out_buffer, 44100*2, frame->data, frame->nb_samples);
            
            int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0);
            NSData *outBufferData = [NSData dataWithBytes:out_buffer length:out_buffer_size];
            [self.fileHandle writeData:outBufferData error:nil];
        }
    }
    while (1) {
        pkt->data = NULL;
        pkt->size = 0;
        ret = avcodec_send_packet(a_dec_ctx, pkt);
        if (ret<0) {
            NSLog(@"avcodec_send_packet出錯");
            break;
        }
        while (1) {
            ret = avcodec_receive_frame(a_dec_ctx, frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }
            
            swr_convert(swr_ctx, &out_buffer, 44100*2, frame->data, frame->nb_samples);
            
            int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0);
            NSData *outBufferData = [NSData dataWithBytes:out_buffer length:out_buffer_size];
            [self.fileHandle writeData:outBufferData error:nil];
        }
    }
    
__FAIL:
    if (ifmt_ctx) {
        avformat_close_input(&ifmt_ctx);
        avformat_free_context(ifmt_ctx);
    }
    if (out_buffer) {
        av_free(out_buffer);
    }
    if (frame) {
        av_frame_free(&frame);
    }
    if (pkt) {
        av_packet_free(&pkt);
    }
    if (swr_ctx) {
        swr_free(&swr_ctx);
    }
    if (a_dec_ctx) {
        avcodec_close(a_dec_ctx);
        avcodec_free_context(&a_dec_ctx);
    }

這樣咱們就把多媒體文件中的音頻流取出來保存到本地了,通過下面命令就可以播放了

ffplay -f s16le -ar 44100 -ac 2 output.pcm

-f 指定pcm的存儲格式
-ar 指定采樣率
-ac 指定通道數(shù)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 本文將使用FFmpeg從mp4文件中提取YUV數(shù)據保存到本地,主要步驟如下:1、使用avformat_open_i...
    iOS開發(fā)之FFmpeg閱讀 1,304評論 0 0
  • ffmpeg是一個非常有用的命令行程序,它可以用來轉碼媒體文件。它是領先的多媒體框架FFmpeg的一部分,其有很多...
    城市之光閱讀 7,058評論 3 6
  • 視頻解碼,保存為yuv 1、解碼流程 2、相關函數(shù) * av_register_all():注冊所有組件 * ...
    zjjcc閱讀 9,522評論 0 3
  • ### YUV顏色空間 視頻是由一幀一幀的數(shù)據連接而成,而一幀視頻數(shù)據其實就是一張圖片。 yuv是一種圖片儲存格式...
    天使君閱讀 3,681評論 0 4
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,894評論 28 54

友情鏈接更多精彩內容