/*
* 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. ul li列子

    <html> <body> <p>有序列表:</p> <ol> <li>打开冰箱门</li> <li>把 ...

  2. python打包成.exe

    pyuic5 mainwindow.ui -o test.py pip install pyinstaller pyinstaller -F -w ***.py https://blog.csdn.n ...

  3. scanf格式化中的\n

    如果一个scanf的格式串以\n结尾,那么在读取完后还会阻塞等待,比如: int a; scanf("%d\n", &a); 这种情况,输入一个数字然后敲下回车后,程序还是 ...

  4. CodeForces - 457C:Elections(三分)

    You are running for a governor in a small city in Russia. You ran some polls and did some research, ...

  5. Html页面Dom对象之Document

    Document 对象 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 提示:Document 对 ...

  6. 数据库表结构转成设计书,PowerDesigner 表格导出为excel

    数据库中的表导入到PowerDesigner中并转为excel文档 1.打开PowerDesigner12,在菜单中按照如下方式进行操作 file->Reverse Engineer->D ...

  7. sublime 自动添加兼容前缀插件autoprefixer

    安装插件autoprefixer步骤: 1.确保Node.js已经安装,未安装请 点击 这里>> 2.下载autoprefixer插件 https://github.com/sindres ...

  8. Git冲突和解决冲突

    1.产生冲突原因 产生:多个开发者同时使用或者操作git中的同一个文件,最后在依次提交和push的时候,第一个操作的是可以正常提交的,而之后的开发者想要执行pull(拉)和pull(push)操作的时 ...

  9. 2019第1周日-MQ选型要点

    用消息中间件犹如小马过河,选择合适的才最重要,这需要贴合自身的业务需求,技术服务于业务.具体在选择上可从下面功能.性能.可靠性和可用性.运维管理.社区和生态.团队技术栈等维度来进行筛选. 具体技术选型 ...

  10. graphEdit

    目的 实现支持算法流程图 参考代码 c# DSGraphEdit - CodeProject NetworkView: A WPF custom control for visualizing and ...