/*
* Copyright (c) 2001 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/ /**
* @file
* audio decoding with libavcodec API example
*
* @example decode_audio.c
*/ #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <libavutil/frame.h>
#include <libavutil/mem.h> #include <libavcodec/avcodec.h> #define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096 int main(int argc, char **argv)
{
  //定义输入输出文件名字
const char *outfilename, *filename;
  //定义编码器和编码器上下文
const AVCodec *codec;
AVCodecContext *c= NULL;
int len;
FILE *f, *outfile;
uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
AVPacket avpkt;
AVFrame *decoded_frame = NULL; if (argc <= ) {
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[]);
exit();
}
filename = argv[];
outfilename = argv[]; /* register all the codecs */
avcodec_register_all(); av_init_packet(&avpkt); /* find the MPEG audio decoder */
codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit();
} c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit();
} /* open it */
if (avcodec_open2(c, codec, NULL) < ) {
fprintf(stderr, "Could not open codec\n");
exit();
} f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit();
}
outfile = fopen(outfilename, "wb");
if (!outfile) {
av_free(c);
exit();
} /* decode until eof */
avpkt.data = inbuf;
avpkt.size = fread(inbuf, , AUDIO_INBUF_SIZE, f);
  //解码直到所有的都完成
while (avpkt.size > ) {
int i, ch;
int got_frame = ;
     //如果未定义解码帧,则申请一个用来存储解码后的数据
if (!decoded_frame) {
if (!(decoded_frame = av_frame_alloc())) {
fprintf(stderr, "Could not allocate audio frame\n");
exit();
}
}
     //解码失败,或者解码接受,解码到了文件EOF位置
len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
if (len < ) {
fprintf(stderr, "Error while decoding\n");
exit();
}
if (got_frame) {
/* if a frame has been decoded, output it */
int data_size = av_get_bytes_per_sample(c->sample_fmt);
if (data_size < ) {
/* This should not occur, checking just for paranoia */
fprintf(stderr, "Failed to calculate data size\n");
exit();
}
       //将解码后的数据写入到文件中,按通道写入,data_size是一个音频sample的大小
for (i=; i<decoded_frame->nb_samples; i++)
for (ch=; ch<c->channels; ch++)
fwrite(decoded_frame->data[ch] + data_size*i, , data_size, outfile);
}
avpkt.size -= len;
avpkt.data += len;
avpkt.dts =
avpkt.pts = AV_NOPTS_VALUE;
if (avpkt.size < AUDIO_REFILL_THRESH) {
/* Refill the input buffer, to avoid trying to decode
* incomplete frames. Instead of this, one could also use
* a parser, or use a proper container format through
* libavformat. */
//将一个packet后剩余的内容移动到缓冲区的头部
       memmove(inbuf, avpkt.data, avpkt.size);
avpkt.data = inbuf;
       //在移动后将文件中的内容继续拷贝到packet中以提供下次解码
len = fread(avpkt.data + avpkt.size, ,
AUDIO_INBUF_SIZE - avpkt.size, f);
if (len > )
avpkt.size += len;
}
} fclose(outfile);
fclose(f); avcodec_free_context(&c);
av_frame_free(&decoded_frame); return ;
}

[ffmpeg]deocde audio(v3.3.2)的更多相关文章

  1. (转)Integrating Intel® Media SDK with FFmpeg for mux/demuxing and audio encode/decode usages 1

    Download Article and Source Code Download Integrating Intel® Media SDK with FFmpeg for mux/demuxing ...

  2. C# 使用ffmpeg视频截图

    <appSettings> <add key="ffmpeg" value="E:\ffmpeg\ffmpeg-20141012-git-20df026 ...

  3. ffmpeg命令详解(转)

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频编解码库l ...

  4. 【视频开发】【CUDA开发】ffmpeg Nvidia硬件加速总结

    原文链接:https://developer.nvidia.com/ffmpeg GPU-accelerated video processing integrated into the most p ...

  5. ffmpeg音频播放代码示例-avcodec_decode_audio4

    一.概述 最近在学习ffmpeg解码的内容,参考了官方的教程http://dranger.com/ffmpeg/tutorial03.html,结果发现这个音频解码的教程有点问题.参考了各种博客,并同 ...

  6. 微信公众号开发——通过ffmpeg解决amr文件无法播放问题

    今天刚好碰到个需求,要在微信浏览器中实现录音,并在其他页面上播放.录音功能本身是JS SDK的功能,倒没啥问题,然而录音的文件保存下来是amr格式,而IOS的浏览器没法播放amr(据说微信浏览器的vi ...

  7. FFMpeg for PHP

    PHP使用FFMpeg来转换视频格式.Github上搜索FFMPEG,到https://github.com/PHP-FFMpeg/PHP-FFMpeg. For Windows users : Pl ...

  8. ffmpeg 简介及使用

    简介 ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url ...

  9. ffmpeg mp4 mp3 wav flac webm aac ac3 ogg格式转换

    版权声明:本文为博主原创文章,未经允许不得转载. ffmpeg是Linux中转换音频视频文件的常用工具. mp4 to mp3: ffmpeg -i $ID.mp4 -acodec libmp3lam ...

随机推荐

  1. CS231n 斯坦福深度视觉识别课 学习笔记(完结)

    课程地址 第1章 CS231n课程介绍 ---1.1 计算机视觉概述 这门课的主要内容是计算机视觉.它是一门需要涉及很多其他科目知识的学科. 视觉数据占据了互联网的绝大多数,但是它们很难利用. --- ...

  2. PyCharm 注释

    1.1 单行注释(行注释) 语法格式: #[空格]说明性文字信息 注:可放一行代码的后面进行说明 添加快捷键:  Ctrl+/ 取消快捷键:  同上 1.2 多行注释(块注释) 语法格式: " ...

  3. Linux driver 板级文件跟踪一般方法

    /*********************************************************************************** * Linux driver ...

  4. OK335xS-Android pack-ubi-256M.sh hacking

    #/******************************************************************************* # * OK335xS-Androi ...

  5. linux-锁屏时间设置

    系统:ubuntu16.04 操作步骤 设置setting -> System Settings -> Brightness&lock, 在界面中选择想要的设置即可: 参考 1.百 ...

  6. ubuntu16.04 中文输入法

    https://blog.csdn.net/qq_21792169/article/details/53152700 在主文件夹目录即home目录,按快捷键Ctrl+H(显示隐藏文件),看到的.bas ...

  7. 小程序api请求层封装(Loading全局配置)

    前言 小程序开发,没有vue中的axios那么好使,请求层的封装需要自己来搞. 当然请求层的配置少不了loading,这里索性也就将loading做一个配置,避免以后重复造轮子 请求封装 小程序中有封 ...

  8. 剑指offer-特定二维数组中查找一个元素是否存在-二分搜索-二维数组

    int [][] array ={ {1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,19} }; 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都 ...

  9. log4net保存到数据库系列二:独立配置文件中配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...

  10. 【转】每天一个linux命令(56):netstat命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...