/*
* 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. ListBox 控件单击事件

    XAML: <ListBox x:Name="ItemBox" Grid.Column="0" Tap="ItemBox_Tap"&g ...

  2. 如何使用 python3 将RGB 图片转换为 灰度图

    首先,介绍第一种方法, 使用  PIL  库,   PIL库是一种python语言常用的一个图形处理库. 关于   PIL  库的安装本文就不介绍了. from PIL import Image I ...

  3. python 正则表达式 提取网页中标签的中文

    转载请注明出处 http://www.cnblogs.com/pengwang52/. >>> p= re.compile(r'\<div class="commen ...

  4. PAT 团体程序设计天梯赛 L1-046 整除光棍(模拟除法)

    L1-046. 整除光棍 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 这里所谓的"光棍",并不是指单身汪 ...

  5. BC32(hdu5182~5185)

    恩……又是一个悲伤的故事,然后BC做出来一题,因为自己傻逼,可能紧张,也可能是其他,反正没看全题目就敲,敲完WA,WA完改,改完WA,没了……大概五十几分钟WA了五法,然后问了才知道没看全,就这样,后 ...

  6. python pandas Timestamp 转为 datetime 类型

    In [11]: ts = pd.Timestamp('2014-01-23 00:00:00', tz=None) In [12]: ts.to_pydatetime() Out[12]: date ...

  7. MHA之Binlog Dump (GTID)僵尸进程清理

      master存活的状态下切换 masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_mas ...

  8. FastAdmin 系统配置中添加选项卡

    群里有小伙伴问怎么在系统配置中添加选卡,之前试过. 流程如下 刷新页面,然后就有了.

  9. java中原子操作的实现分析

    一.CAS原理: CAS的全程即Compare And Swap,翻译成中文为比较并交换: CAS操作依赖于CPU指令CMPXCHG来实现比较并交换操作的原子性,通过查看HotSpot源码如下: 可以 ...

  10. dell support

    部門營業時間 電話號碼訂單支援中小型企業 (員工不多於 500名 )00852-3416-0910 9:00 - 18:00 訂單編號:  810607806 訂單日期:  26/11/2014 客戶 ...