国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ffmpeg學(xué)習(xí)之音頻解碼數(shù)據(jù)

這篇具有很好參考價(jià)值的文章主要介紹了ffmpeg學(xué)習(xí)之音頻解碼數(shù)據(jù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

音頻數(shù)據(jù)經(jīng)過(guò)解碼后會(huì)被保存為,pcm數(shù)據(jù)格式。而對(duì)應(yīng)的處理流程如下所示。

ffmpeg學(xué)習(xí)之音頻解碼數(shù)據(jù),FFmpeg學(xué)習(xí),ffmpeg,學(xué)習(xí),音視頻

avcodec_find_encoder()?

/**
 * 查找具有匹配編解碼器ID的已注冊(cè)編碼器.
 *
 * @param id AVCodecID of the requested encoder
 * @return An encoder if one was found, NULL otherwise.
 */
const AVCodec *avcodec_find_encoder(enum AVCodecID id);

avcodec_find_encoder_by_name()?

/**
 * 查找具有指定名稱的已注冊(cè)編碼器.
 *
 * @param name name of the requested encoder
 * @return An encoder if one was found, NULL otherwise.
 */
const AVCodec *avcodec_find_encoder_by_name(const char *name);

avcodec_alloc_context3()?

/**
分配AVCodecContext并將其字段設(shè)置為默認(rèn)值。該應(yīng)使用avcodec_free_context()釋放結(jié)果結(jié)構(gòu)。
 *
 * @param codec if non-NULL, allocate private data and initialize defaults
 *              for the given codec. It is illegal to then call avcodec_open2()
 *              with a different codec.
 *              If NULL, then the codec-specific defaults won't be initialized,
 *              which may result in suboptimal default settings (this is
 *              important mainly for encoders, e.g. libx264).
 *
 * @return An AVCodecContext filled with default values or NULL on failure.
 */
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);

?設(shè)置對(duì)應(yīng)音頻編碼的數(shù)據(jù)類型

    codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;          //輸入音頻的采樣大小
    codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;    //輸入音頻的channel layout
    codec_ctx->channels = 2;                            //輸入音頻 channel 個(gè)數(shù)
    codec_ctx->sample_rate = 44100;                     //輸入音頻的采樣率
    codec_ctx->bit_rate = 0; //AAC_LC: 128K, AAC HE: 64K, AAC HE V2: 32K
    codec_ctx->profile = FF_PROFILE_AAC_HE_V2; //閱讀 ffmpeg 代碼

設(shè)置編碼的frame的相關(guān)參數(shù)

//set parameters
    frame->nb_samples     = 512;                //單通道一個(gè)音頻幀的采樣數(shù)
    frame->format         = AV_SAMPLE_FMT_S16;  //每個(gè)采樣的大小
    frame->channel_layout = AV_CH_LAYOUT_STEREO; //channel layout

整個(gè)代碼:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-593288.html

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
 extern "C"{
    
    
    #include <libavcodec/avcodec.h>
    #include <libavutil/channel_layout.h>
    #include <libavutil/common.h>
    #include <libavutil/frame.h>
    #include <libavutil/samplefmt.h>
 }

   

 
/* check that a given sample format is supported by the encoder */
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
{
    const enum AVSampleFormat *p = codec->sample_fmts;
 
    while (*p != AV_SAMPLE_FMT_NONE) {
        if (*p == sample_fmt)
            return 1;
        p++;
    }
    return 0;
}
 
/* just pick the highest supported samplerate */
static int select_sample_rate(const AVCodec *codec)
{
    const int *p;
    int best_samplerate = 0;
 
    if (!codec->supported_samplerates)
        return 44100;
 
    p = codec->supported_samplerates;
    while (*p) {
        if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
            best_samplerate = *p;
        p++;
    }
    return best_samplerate;
}
 
/* select layout with the highest channel count */
static int select_channel_layout(const AVCodec *codec, AVChannelLayout *dst){

    const AVChannelLayout *p, *best_ch_layout;
    int best_nb_channels   = 0;
 
    if (!codec->ch_layouts){
        AVChannelLayout src = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
        return av_channel_layout_copy(dst,&src);
    }
      
 
    p = codec->ch_layouts;
    while (p->nb_channels) {
        int nb_channels = p->nb_channels;
 
        if (nb_channels > best_nb_channels) {
            best_ch_layout   = p;
            best_nb_channels = nb_channels;
        }
        p++;
    }
    return av_channel_layout_copy(dst, best_ch_layout);
}
 
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
                   FILE *output)
{
    int ret;
 
    /* send the frame for encoding */
    ret = avcodec_send_frame(ctx, frame);
    if (ret < 0) {
        fprintf(stderr, "Error sending the frame to the encoder\n");
        exit(1);
    }
 
    /* read all the available output packets (in general there may be any
     * number of them */
    while (ret >= 0) {
        ret = avcodec_receive_packet(ctx, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr, "Error encoding audio frame\n");
            exit(1);
        }
 
        fwrite(pkt->data, 1, pkt->size, output);
        av_packet_unref(pkt);
    }
}
 
int main(int argc, char **argv)
{
    // 對(duì)應(yīng)文件的編碼數(shù)據(jù)
    const char *filename;

    // 編碼器
    const AVCodec *codec;

    // 編碼器上下文
    AVCodecContext *c= NULL;

    // 保存未曾解碼的數(shù)據(jù)
    AVFrame *frame;
    // 對(duì)應(yīng)的保存數(shù)據(jù)
    AVPacket *pkt;
    int i, j, k, ret;
    FILE *f;
    uint16_t *samples;
    float t, tincr;
 
    if (argc <= 1) {
        fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
        return 0;
    }
    // 獲得輸出文件
    filename = argv[1];
 
    // 找到對(duì)應(yīng)的編碼器
    codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }
    // 使用默認(rèn)值設(shè)置編碼器的內(nèi)容數(shù)據(jù)
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate audio codec context\n");
        exit(1);
    }
 
    // 設(shè)置編碼器的相關(guān)信息
    // 設(shè)置編碼器的比特率
    c->bit_rate = 64000;
 
    // /采樣的數(shù)據(jù)類型
    c->sample_fmt = AV_SAMPLE_FMT_S16;
    if (!check_sample_fmt(codec, c->sample_fmt)) {
        fprintf(stderr, "Encoder does not support sample format %s",
                av_get_sample_fmt_name(c->sample_fmt));
        exit(1);
    }
 
    // 對(duì)應(yīng)的采樣頻率
    c->sample_rate    = select_sample_rate(codec);
    // 設(shè)置對(duì)應(yīng)布局
    ret = select_channel_layout(codec, &c->ch_layout);
    if (ret < 0)
        exit(1);
 
    // 打開對(duì)應(yīng)的編碼器
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
 
    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }
 
    /* packet for holding encoded output */
    pkt = av_packet_alloc();
    if (!pkt) {
        fprintf(stderr, "could not allocate the packet\n");
        exit(1);
    }
 
    /* frame containing input raw audio */
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate audio frame\n");
        exit(1);
    }
    // 設(shè)置單通道數(shù)據(jù)采樣大小
    frame->nb_samples     = c->frame_size;
    // 數(shù)據(jù)采樣的大小
    frame->format         = c->sample_fmt;
    // 設(shè)置對(duì)用的通道大小
    ret = av_channel_layout_copy(&frame->ch_layout, &c->ch_layout);
    if (ret < 0)
        exit(1);
 
    /* 設(shè)置對(duì)應(yīng)數(shù)據(jù)的緩沖區(qū) */
    ret = av_frame_get_buffer(frame, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate audio data buffers\n");
        exit(1);
    }
 
    /* encode a single tone sound */
    t = 0;
    tincr = 2 * M_PI * 440.0 / c->sample_rate;
    for (i = 0; i < 200; i++) {
        /* make sure the frame is writable -- makes a copy if the encoder
         * kept a reference internally */
        ret = av_frame_make_writable(frame);
        if (ret < 0)
            exit(1);
        samples = (uint16_t*)frame->data[0];
 
        for (j = 0; j < c->frame_size; j++) {
            samples[2*j] = (int)(sin(t) * 10000);
 
            for (k = 1; k < c->ch_layout.nb_channels; k++)
                samples[2*j + k] = samples[2*j];
            t += tincr;
        }
        encode(c, frame, pkt, f);
    }
 
    /* flush the encoder */
    encode(c, NULL, pkt, f);
 
    fclose(f);
 
    av_frame_free(&frame);
    av_packet_free(&pkt);
    avcodec_free_context(&c);
 
    return 0;
}

到了這里,關(guān)于ffmpeg學(xué)習(xí)之音頻解碼數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 音視頻八股文(11)-- ffmpeg 音頻重采樣

    音視頻八股文(11)-- ffmpeg 音頻重采樣

    所謂的重采樣,就是改變?頻的采樣率、sample format、聲道數(shù)等參數(shù),使之按照我們期望的參數(shù)輸出。 為什么要重采樣?當(dāng)然是原有的?頻參數(shù)不滿?我們的需求,?如在FFmpeg解碼?頻的時(shí)候,不同的?源有不同的格式,采樣率等,在解碼后的數(shù)據(jù)中的這些參數(shù)也會(huì)不?致(最

    2024年02月04日
    瀏覽(24)
  • qt+ffmpeg 實(shí)現(xiàn)音視頻播放(二)之音頻播放

    qt+ffmpeg 實(shí)現(xiàn)音視頻播放(二)之音頻播放

    通過(guò)? avformat_open_input ()?打開媒體文件并分配和初始化? AVFormatContext?? 結(jié)構(gòu)體。 函數(shù)原型如下: int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options); 參數(shù)說(shuō)明: - `ps`:指向 `AVFormatContext` 結(jié)構(gòu)體指針的指針,用于存儲(chǔ)打開的媒體文件的信息。

    2024年04月22日
    瀏覽(37)
  • 【配置環(huán)境】安裝Ffmpeg音視頻編解碼工具和搭建EasyDarwin開源流媒體服務(wù)器

    【配置環(huán)境】安裝Ffmpeg音視頻編解碼工具和搭建EasyDarwin開源流媒體服務(wù)器

    目錄 一,安裝Ffmpeg音視頻編解碼工具 1,簡(jiǎn)介 2,開發(fā)文檔 3,安裝部署 二,搭建EasyDarwin開源流媒體服務(wù)器 1,簡(jiǎn)介 2,主要功能特點(diǎn) 3,安裝部署 4,效果圖 三,簡(jiǎn)單測(cè)試 Ffmpeg是一套可以用來(lái)記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開源計(jì)算機(jī)程序。采用LGPL或GPL許

    2024年02月07日
    瀏覽(122)
  • 音視頻剪輯|FFMPEG|windows10下的音視頻格式轉(zhuǎn)換,遮擋填充,GIF動(dòng)圖制作,背景音頻抽取,替換

    音視頻剪輯|FFMPEG|windows10下的音視頻格式轉(zhuǎn)換,遮擋填充,GIF動(dòng)圖制作,背景音頻抽取,替換

    最近對(duì)于音視頻和圖像的處理問(wèn)題比較感興趣,但發(fā)現(xiàn)很多目前需要的功能要么需要付費(fèi)但不會(huì)過(guò)于麻煩,要么比較麻煩,很可能某個(gè)功能實(shí)現(xiàn)需要安裝很多軟件 例如,視頻轉(zhuǎn)GIF動(dòng)圖,該功能的實(shí)現(xiàn)要么使用Photoshop全家桶,要么找在線網(wǎng)站,或者是wps充會(huì)員,或者找其它方法

    2024年02月20日
    瀏覽(26)
  • ffmpeg系列學(xué)習(xí)——FFmpeg的音視頻處理

    1.音視頻的采樣率、采樣位深度和聲道數(shù) 音頻和視頻的采樣率、采樣位深度和聲道數(shù)是媒體文件中的重要參數(shù),它們會(huì)直接影響到音視頻的質(zhì)量和文件大小。下面對(duì)它們進(jìn)行詳細(xì)解釋: 采樣率 采樣率指音頻每秒鐘采樣的次數(shù),用赫茲(Hz)表示。采樣率越高,音頻的還原度越

    2024年02月04日
    瀏覽(84)
  • FFmpeg源碼分析:avcodec_send_packet()與avcodec_receive_frame()音視頻解碼

    FFmpeg源碼分析:avcodec_send_packet()與avcodec_receive_frame()音視頻解碼

    FFmpeg在libavcodec模塊,舊版本提供avcodec_decode_video2()作為視頻解碼函數(shù),avcodec_decode_audio4()作為音頻解碼函數(shù)。在FFmpeg 3.1版本新增avcodec_send_packet()與avcodec_receive_frame()作為音視頻解碼函數(shù)。后來(lái),在3.4版本把a(bǔ)vcodec_decode_video2()和avcodec_decode_audio4()標(biāo)記為過(guò)時(shí)API。版本變更描述如下

    2024年02月03日
    瀏覽(94)
  • 音視頻 ffmpeg命令提取音視頻數(shù)據(jù)

    保留封裝格式 提取視頻 提取音頻 推薦一個(gè)零聲學(xué)院項(xiàng)目課,個(gè)人覺得老師講得不錯(cuò),分享給大家: 零聲白金學(xué)習(xí)卡(含基礎(chǔ)架構(gòu)/高性能存儲(chǔ)/golang云原生/音視頻/Linux內(nèi)核) https://xxetb.xet.tech/s/VsFMs

    2024年02月10日
    瀏覽(26)
  • 音視頻 ffmpeg命令提取PCM數(shù)據(jù)

    提取PCM 推薦一個(gè)零聲學(xué)院項(xiàng)目課,個(gè)人覺得老師講得不錯(cuò),分享給大家: 零聲白金學(xué)習(xí)卡(含基礎(chǔ)架構(gòu)/高性能存儲(chǔ)/golang云原生/音視頻/Linux內(nèi)核) https://xxetb.xet.tech/s/VsFMs

    2024年02月09日
    瀏覽(32)
  • 【音視頻 ffmpeg 學(xué)習(xí)】 RTMP推流 mp4文件

    【音視頻 ffmpeg 學(xué)習(xí)】 RTMP推流 mp4文件

    1.RTMP(實(shí)時(shí)消息傳輸協(xié)議)是Adobe 公司開發(fā)的一個(gè)基于TCP的應(yīng)用層協(xié)議。 2.RTMP協(xié)議中基本的數(shù)據(jù)單元稱為消息(Message)。 3.當(dāng)RTMP協(xié)議在互聯(lián)網(wǎng)中傳輸數(shù)據(jù)的時(shí)候,消息會(huì)被拆分成更小的單元,稱為消息塊(Chunk)。 (1). linux 環(huán)境準(zhǔn)備 安裝nginx 和 rtmp模塊 下載nginx安裝包 下載

    2024年02月03日
    瀏覽(33)
  • 【FFmpeg】ffmpeg 命令行參數(shù) ⑤ ( 使用 ffmpeg 命令提取 音視頻 數(shù)據(jù) | 保留封裝格式 | 保留編碼格式 | 重新編碼 )

    【FFmpeg】ffmpeg 命令行參數(shù) ⑤ ( 使用 ffmpeg 命令提取 音視頻 數(shù)據(jù) | 保留封裝格式 | 保留編碼格式 | 重新編碼 )

    使用 ffmpeg 命令 從 視頻數(shù)據(jù) 中 提取 音頻數(shù)據(jù) / 視頻數(shù)據(jù) 保留封裝格式 , 封裝格式 指的就是 封裝 視頻數(shù)據(jù) 的 容器 ; 一個(gè) mp4 格式的視頻 , 其 封裝容器 就是 mp4 容器 , 其中 封裝了 h.264 格式的視頻數(shù)據(jù) 和 aac 格式的 音頻數(shù)據(jù) ; 執(zhí)行 命令 , 從 input.mp4 輸入文件中 , 提取 音頻

    2024年03月23日
    瀏覽(65)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包