[ffmpeg]deocde audio(v3.3.2)
/*
* 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)的更多相关文章
- (转)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 ...
- C# 使用ffmpeg视频截图
<appSettings> <add key="ffmpeg" value="E:\ffmpeg\ffmpeg-20141012-git-20df026 ...
- ffmpeg命令详解(转)
FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频编解码库l ...
- 【视频开发】【CUDA开发】ffmpeg Nvidia硬件加速总结
原文链接:https://developer.nvidia.com/ffmpeg GPU-accelerated video processing integrated into the most p ...
- ffmpeg音频播放代码示例-avcodec_decode_audio4
一.概述 最近在学习ffmpeg解码的内容,参考了官方的教程http://dranger.com/ffmpeg/tutorial03.html,结果发现这个音频解码的教程有点问题.参考了各种博客,并同 ...
- 微信公众号开发——通过ffmpeg解决amr文件无法播放问题
今天刚好碰到个需求,要在微信浏览器中实现录音,并在其他页面上播放.录音功能本身是JS SDK的功能,倒没啥问题,然而录音的文件保存下来是amr格式,而IOS的浏览器没法播放amr(据说微信浏览器的vi ...
- FFMpeg for PHP
PHP使用FFMpeg来转换视频格式.Github上搜索FFMPEG,到https://github.com/PHP-FFMpeg/PHP-FFMpeg. For Windows users : Pl ...
- ffmpeg 简介及使用
简介 ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url ...
- ffmpeg mp4 mp3 wav flac webm aac ac3 ogg格式转换
版权声明:本文为博主原创文章,未经允许不得转载. ffmpeg是Linux中转换音频视频文件的常用工具. mp4 to mp3: ffmpeg -i $ID.mp4 -acodec libmp3lam ...
随机推荐
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- ping命令技巧详解 windows下ping命令知识大全
windows ping命令对于多数电脑爱好者都不会陌生,通过ping ip可以知道网络是否畅通或者网络传输质量如何等,是网络技术人员常用的检测网络命令,多数朋友对ping命令知道的并不多,接下来本文 ...
- 对线性模型进行最小二乘法学习的实例(使用三角多项式基函数 Python实现)
该文为个人学习时的学习笔记.最小二乘法在统计学中需要验证数据的多重共性性等问题,需要做相关的假设检验,这里我们假设一切为理想状态. 最小二乘法 一个简单的应用就是进行线性模型的拟合,一般情况下我们 ...
- win8转win7+真正解决 “安装程序无法定位现有系统分区,也无法创建新的系统分区”的方法
问题情况:win8电脑转win7系统,出现问题:“安装程序无法定位现有系统分区,也无法创建新的系统分区” 第一种:猜测可行的方法: 1.老毛桃U盘启动电脑,进入PE系统后,直接使用Diskgeni ...
- 浏览器中的data类型的Url格式,data:image/png,data:image/jpeg!(源自:http://blog.csdn.net/roadmore/article/details/38498719)
所谓"data"类型的Url格式,是在RFC2397中 提出的,目的对于一些“小”的数据,可以在网页中直接嵌入,而不是从外部文件载入.例如对于img这个Tag,哪怕这个图片非常非常 ...
- gevent和tornado异步
阅读目录 从 Tornado 说起 再来看下 Gevent 总要总结一下 原文:http://www.pywave.com/2012/08/17/about-gevent-and-tornado/ 还 ...
- MHA之Binlog Dump (GTID)僵尸进程清理
master存活的状态下切换 masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_mas ...
- [DL] 基于theano.tensor.dot的逻辑回归代码中的SGD部分的疑问探幽
在Hinton的教程中, 使用Python的theano库搭建的CNN是其中重要一环, 而其中的所谓的SGD - stochastic gradient descend算法又是如何实现的呢? 看下面源 ...
- 小米盒子root及sshdroid安装
1.root 参考屌丝猫的教程 主要原理是通过运行自定义recovery实现root功能 2.安装sshdroid以及幸运破解器 3.使用幸运破解器吧sshdroid编程系统应用,从而实现自启动
- oracle之 redo过高诊断
一.诊断过度redo 要找到生成大量重做的会话,您可以使用以下任何一种方法.这两种方法都检查生成的撤销量.当一个事务生成撤销,它将自动生成重做. 当需要检查生成大量的程序时,使用第一个查询.当这些程序 ...