ffmpeg学习(印对应的文件信息,抽取音频文件信息)

这篇具有很好参考价值的文章主要介绍了ffmpeg学习(印对应的文件信息,抽取音频文件信息)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

打印对应的文件信息

抽取音频文件信息


打印对应的文件信息

# 概念
多媒体文件是个容器,在容器中有很多流,使用(stream或者track)表示。每种不同的流不交叉,是由不同的编码器的编码的(音频和视频的不同编码形似),从流中读取的数据为包,包中包含一帧或多个帧压缩数据,

```
AVFormatContext:多媒体文件流上下文
AVstream:多媒体流,从多媒体文件中读取流
AVPacket:获得多媒体流压缩的帧率
```
FFmpeg操作流数据的基本步骤:

 **1. 解复用
 2. 数据流
 3. 获得数据包
 4. 释放相关资源**
# 打印多媒体流信息

```cpp
extern "C"
{
    #include <libavutil/log.h>
    #include <libavformat/avformat.h>
}
#ifdef av_err2str
#undef av_err2str
#include <string>
av_always_inline std::string av_err2string(int errnum) {
    char str[AV_ERROR_MAX_STRING_SIZE];
    return av_make_error_string(str, AV_ERROR_MAX_STRING_SIZE, errnum);
}
#define av_err2str(err) av_err2string(err).c_str()
#endif


int main(int argc, char const *argv[])
{   int ret;
    av_log_set_level(   AV_LOG_INFO);
    // 注册信息
    av_register_all();
    AVFormatContext *fmt_ctx=NULL;
    // 打开多媒体文件,制定多媒体文件内容上向文,制定文件地质,指定文件类型,制定传入参数
    ret=avformat_open_input(&fmt_ctx,argv[1],NULL,NULL);
    if(ret<0){
        av_log(NULL,AV_LOG_INFO,"CAN'T OPEN FILE:%s \n",av_err2str(ret));
        return -1;
    }
    // av_dump_format()参数为上下文信息,流的索引值,多媒体文件名,输入流0或者输出流信息
    // 打印媒体流信息
    av_dump_format(fmt_ctx,0,argv[1],0);
    // 带开多媒体文件
    avformat_close_input(&fmt_ctx);
    return 0;
}


------------------------------------------输出信息------------------------------------------
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '../source/big_buck_bunny_720p_30mb.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01T00:00:00.000000Z
    encoder         : Lavf53.24.2
  Duration: 00:02:50.86, bitrate: N/A
  Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 1280x720, 1086 kb/s, 25 fps, 25 tbr, 12800 tbn (default)
    Metadata:
      creation_time   : 1970-01-01T00:00:00.000000Z
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, 6 channels, 383 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01T00:00:00.000000Z
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
```

抽取音频文件信息

# 抽取多媒体文件中的音频数据
 1. av_init_packet():初始化数据包结构体
 2. av_find_best_stream:从多媒体文件中获得最好的数据流
 3. av_read_frame()/av_packet_unref():读取流中的包,可以使用之后释放空间

**由于进行的是使用ACC格式的封装,在封装的过程中需要以ADTS的格式进行保存,相比ADIS,ADTS格式容易的在流传输中使用,其为7个字节大小**
ffmpeg学习(印对应的文件信息,抽取音频文件信息),FFmpeg学习,ffmpeg

 - syncword (其中共占有12个位):同步头代表着1个ADTS帧的开始,所有bit置1,即 0xFFF   
 - ID(1位):MPEG标识符,0标识MPEG-4,1标识MPEG-2
 -  Layer(2位): 直接置00
 - protection_absent(1位):表示是否误码校验。1 no CRC , 0 has CRC profile:AAC 编码级别, 0:
 - Main Profile(2位): 1:LC(最常用), 2: SSR, 3: reserved.
 - sampling_frequency_index(采样频率标识符):采样率标识 Private bit:直接置0,解码时忽略这个参数
 - channel_configuration(3位): 声道数标识 ,生命了布局结构
 - original_copy(1位): 直接置0,解码时忽略这个参数
 - home(1位):直接置0,解码时忽略这个参数

**可变头信息:adts_variable_header()**
    ffmpeg学习(印对应的文件信息,抽取音频文件信息),FFmpeg学习,ffmpeg

 - copyright_identification_bit: 直接置0,解码时忽略这个参数
 - copyright_identification_start: 直接置0,解码时忽略这个参数
 - aac_frame_lenght: 当前音频帧的字节数,编码元数据字节数 + 文件头字节数(0 == protection_absent   ? 7: 9)
 - adts_buffer_fullness: 当设置为0x7FF时表示时可变码率
 - number_of_raw_data_blocks_in_frames: 当前音频包里面包含的音频编码帧数, 置为 aac_nums -   1, 即只有一帧音频时置0

```cpp
extern "C"
{
   #include <stdio.h>
    #include <libavutil/log.h>
    #include <libavformat/avio.h>
    #include <libavformat/avformat.h> 
}

#define ADTS_HEADER_LEN  7;

static int get_audio_obj_type(int aactype){
    //AAC HE V2 = AAC LC + SBR + PS
    //AAV HE = AAC LC + SBR
    //所以无论是 AAC_HEv2 还是 AAC_HE 都是 AAC_LC
    switch(aactype){
        case 0:
        case 2:
        case 3:
            return aactype+1;
        case 1:
        case 4:
        case 28:
            return 2;
        default:
            return 2;

    }
}

static int get_sample_rate_index(int freq, int aactype){

    int i = 0;
    int freq_arr[13] = {
        96000, 88200, 64000, 48000, 44100, 32000,
        24000, 22050, 16000, 12000, 11025, 8000, 7350
    };

    //如果是 AAC HEv2 或 AAC HE, 则频率减半
    if(aactype == 28 || aactype == 4){
        freq /= 2; 
    }

    for(i=0; i< 13; i++){
        if(freq == freq_arr[i]){
            return i;
        }
    }
    return 4;//默认是44100
}

static int get_channel_config(int channels, int aactype){
    //如果是 AAC HEv2 通道数减半
    if(aactype == 28){
        return (channels / 2); 
    }
    return channels;
}

static void adts_header(char *szAdtsHeader, int dataLen, int aactype, int frequency, int channels){

    int audio_object_type = get_audio_obj_type(aactype);
    int sampling_frequency_index = get_sample_rate_index(frequency, aactype);
    int channel_config = get_channel_config(channels, aactype);

    printf("aot=%d, freq_index=%d, channel=%d\n", audio_object_type, sampling_frequency_index, channel_config);

    int adtsLen = dataLen + 7;

    szAdtsHeader[0] = 0xff;         //syncword:0xfff                          高8bits
    szAdtsHeader[1] = 0xf0;         //syncword:0xfff                          低4bits
    szAdtsHeader[1] |= (0 << 3);    //MPEG Version:0 for MPEG-4,1 for MPEG-2  1bit
    szAdtsHeader[1] |= (0 << 1);    //Layer:0                                 2bits 
    szAdtsHeader[1] |= 1;           //protection absent:1                     1bit

    szAdtsHeader[2] = (audio_object_type - 1)<<6;            //profile:audio_object_type - 1                      2bits
    szAdtsHeader[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index  4bits 
    szAdtsHeader[2] |= (0 << 1);                             //private bit:0                                      1bit
    szAdtsHeader[2] |= (channel_config & 0x04)>>2;           //channel configuration:channel_config               高1bit

    szAdtsHeader[3] = (channel_config & 0x03)<<6;     //channel configuration:channel_config      低2bits
    szAdtsHeader[3] |= (0 << 5);                      //original:0                               1bit
    szAdtsHeader[3] |= (0 << 4);                      //home:0                                   1bit
    szAdtsHeader[3] |= (0 << 3);                      //copyright id bit:0                       1bit  
    szAdtsHeader[3] |= (0 << 2);                      //copyright id start:0                     1bit
    szAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11);           //frame length:value   高2bits

    szAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3);     //frame length:value    中间8bits
    szAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5);       //frame length:value    低3bits
    szAdtsHeader[5] |= 0x1f;                                 //buffer fullness:0x7ff 高5bits
    szAdtsHeader[6] = 0xfc;
}

int main(int argc, char *argv[])
{
    int err_code;
    char errors[1024];

    char *src_filename = NULL;
    char *dst_filename = NULL;

    FILE *dst_fd = NULL;

    int audio_stream_index = -1;
    int len;

    AVFormatContext *ofmt_ctx = NULL;
    AVOutputFormat *output_fmt = NULL;

    AVStream *out_stream = NULL;

    AVFormatContext *fmt_ctx = NULL;
    AVFrame *frame = NULL;
    AVPacket pkt;

    av_log_set_level(AV_LOG_DEBUG);

    if(argc < 3){
        av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");
        return -1;
    }

    src_filename = argv[1];
    dst_filename = argv[2];

    if(src_filename == NULL || dst_filename == NULL){
        av_log(NULL, AV_LOG_DEBUG, "src or dts file is null, plz check them!\n");
        return -1;
    }

    dst_fd = fopen(dst_filename, "wb");
    if (!dst_fd) {
        av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", dst_filename);
        return -1;
    }

    /*open input media file, and allocate format context*/
    if((err_code = avformat_open_input(&fmt_ctx, src_filename, NULL, NULL)) < 0){
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",
               src_filename,
               err_code,
               errors);
        return -1;
    }

    /*retrieve audio stream*/
    if((err_code = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",
               src_filename,
               err_code,
               errors);
        return -1;
    }

    /*dump input information*/
    av_dump_format(fmt_ctx, 0, src_filename, 0);

    frame = av_frame_alloc();
    if(!frame){
        av_log(NULL, AV_LOG_DEBUG, "Could not allocate frame\n");
        return AVERROR(ENOMEM);
    }

    /*initialize packet*/
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

    /*find best audio stream*/
    audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    if(audio_stream_index < 0){
        av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",
               av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
               src_filename);
        return AVERROR(EINVAL);
    }

    /*
     #define FF_PROFILE_AAC_MAIN 0
     #define FF_PROFILE_AAC_LOW  1
     #define FF_PROFILE_AAC_SSR  2
     #define FF_PROFILE_AAC_LTP  3
     #define FF_PROFILE_AAC_HE   4
     #define FF_PROFILE_AAC_HE_V2 28
     #define FF_PROFILE_AAC_LD   22
     #define FF_PROFILE_AAC_ELD  38
     #define FF_PROFILE_MPEG2_AAC_LOW 128
     #define FF_PROFILE_MPEG2_AAC_HE  131
    */

    int aac_type = fmt_ctx->streams[1]->codecpar->profile;
    int channels = fmt_ctx->streams[1]->codecpar->channels;
    int sample_rate= fmt_ctx->streams[1]->codecpar->sample_rate;

    if(fmt_ctx->streams[1]->codecpar->codec_id != AV_CODEC_ID_AAC){
        av_log(NULL, AV_LOG_ERROR, "the audio type is not AAC!\n");
        goto __ERROR;
    }else{
        av_log(NULL, AV_LOG_INFO, "the audio type is AAC!\n"); 
    }

    /*read frames from media file*/
    while(av_read_frame(fmt_ctx, &pkt) >=0 ){
        if(pkt.stream_index == audio_stream_index){

            
            char adts_header_buf[7];
            adts_header(adts_header_buf, pkt.size, aac_type, sample_rate, channels);
            fwrite(adts_header_buf, 1, 7, dst_fd);

            len = fwrite( pkt.data, 1, pkt.size, dst_fd);
            if(len != pkt.size){
                av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
                       len,
                       pkt.size);
            }
        }
        av_packet_unref(&pkt);
    }

__ERROR:
    /*close input media file*/
    avformat_close_input(&fmt_ctx);
    if(dst_fd) {
        fclose(dst_fd);
    }

    return 0;
}


```
 文章来源地址https://www.toymoban.com/news/detail-575329.html

到了这里,关于ffmpeg学习(印对应的文件信息,抽取音频文件信息)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python ffmpeg将mp4文件实时转码为ts,并指定pid等信息,输出到udp

    要将MP4文件实时转码为TS格式,并将PID等信息指定为UDP输出,可以使用 subprocess 模块和ffmpeg命令行工具来实现。以下是一个示例代码,用于实时转码并将输出发送到UDP服务器: 在上述代码中,我们首先定义了输入文件、UDP服务器地址和PID等信息。然后,我们使用 subprocess.Pop

    2024年01月22日
    浏览(2)
  • ffmpeg视频音频命令

    视频音频合并,以视频时间为主,音频短了循环 方法1:混音,视频权重0,volume调节音量,aloop无限循环,duration:first为第一个素材的长度 ffmpeg -i video.mp4 -i audio.mp3 -filter_complex \\\"[1:a]volume=0.5[a1];[a1]aloop=loop=-1:size=2e+09[a2];[0:a][a2]amix=inputs=2:duration=first:weights=\\\'0 1\\\'[a]\\\" -map 0:v -map \\\"[a]\\\"

    2024年02月13日
    浏览(1)
  • 使用FFMPEG提取音频数据

      我的上一篇博文中已经介绍了FFmpeg,讲解了下载与安装的方式,并在命令行下将视频数据提取为图片数据,如有需要请查看:使用FFMPEG提取图片数据   提取的音频格式有很多种,在这里只讲解两种格式的提取:mp3、wav。mp3格式和wav格式都是数字音频格式,wav格式是最接

    2024年02月06日
    浏览(1)
  • 利用FFmpeg合并音频和视频

    一、FFmpeg 多个音频合并的2种方法 多个mp3文件合并成一个mp3文件 一种方法是连接到一起 ffmpeg64.exe -i \\\"concat:123.mp3|124.mp3\\\" -acodec copy output.mp3 解释:-i代表输入参数     contact:123.mp3|124.mp3代表着需要连接到一起的音频文件                  -acodec copy  output.mp3 重新编码并复制到

    2024年04月10日
    浏览(1)
  • FFmpeg连载6-音频重采样

    今天我们的实战内容是将音频解码成PCM,并将PCM重采样成特定的采样率,然后输出到本地文件进行播放。 什么是重采样? 所谓重采样,一句话总结就是改变音频的三元素,也就是通过重采样改变音频的采样率、采样格式或者声道数。 例如音频A是采样率48000hz、采样格式为f

    2024年01月17日
    浏览(1)
  • FFmpeg从视频中提取音频

    参考博客 ffmpeg Documentation FFmpeg最全教程 FFmpeg 提取视频的音频 FFMPEG 提取音频 ffmpeg 给音频添加封面,ffmpeg对音视频metadata相关操作 查看 使用 FFprobe ffprobe 是一个多媒体流分析工具。它从多媒体流中收集信息,并且以人类和机器可读的形式打印出来。它可以用来检测多媒体流的

    2023年04月08日
    浏览(2)
  • python加上ffmpeg实现音频分割

    前言: 这是一个系列的文章,主要是使用python加上ffmpeg来对音视频文件进行处理,包括音频播放、音频格式转换、音频文件分割、视频播放等。 系列文章链接: 链接1: python使用ffmpeg来制作音频格式转换工具(优化版) 链接2:<Python>PyQt5+ffmpeg,简单视频播放器的编写(解

    2024年02月04日
    浏览(1)
  • 【FFmpeg】音视频录制 ① ( 查询系统中 ffmpeg 可录制的音视频输入设备 | 使用 ffmpeg 命令录制音视频数据 | 录制视频数据命令 |录制音频数据| 同时录制音频和视频数据命令 )

    在 Windows 系统中 , 使用 ffmpeg 命令 录制 音视频 , 需要先获取 系统的 音视频设备 信息 , 录制 音视频 本质上是从 系统音视频设备 中获取数据 ; 执行 命令 , 可以获取 系统中 ffmpeg 可用的 DirectShow 音视频输入设备 ; 命令参数解析 : -list_devices true : 列出所有 ffmpeg 的 指定类型的可

    2024年04月25日
    浏览(1)
  • 【音视频 ffmpeg 学习】 RTMP推流 mp4文件

    1.RTMP(实时消息传输协议)是Adobe 公司开发的一个基于TCP的应用层协议。 2.RTMP协议中基本的数据单元称为消息(Message)。 3.当RTMP协议在互联网中传输数据的时候,消息会被拆分成更小的单元,称为消息块(Chunk)。 (1). linux 环境准备 安装nginx 和 rtmp模块 下载nginx安装包 下载

    2024年02月03日
    浏览(1)
  • FFmpeg音频解码流程详解及简单demo参考

            本文主要讲解FFmpeg的音频解码具体流程,API使用。最后再以一个非常简单的demo演示将一个mp3格式的音频文件解码为原始数据pcm文件。 本文主要基于FFmpeg音频解码新接口。    API接口简单大体讲解如下:         这一步是ffmpeg的任何程序的第一步都是需要先注

    2023年04月08日
    浏览(1)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包