/*
* Copyright (c) 2013-2018 Andreas Unterweger
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Simple audio converter
*
* @example transcode_aac.c
* Convert an input audio file to AAC in an MP4 container using FFmpeg.
* Formats other than MP4 are supported based on the output file extension.
* @author Andreas Unterweger (dustsigns@gmail.com)
*/
#include <stdio.h>
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavcodec/avcodec.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/frame.h"
#include "libavutil/opt.h"
#include "libswresample/swresample.h"
/* The output bit rate in bit/s */
#define OUTPUT_BIT_RATE 96000
/* The number of output channels */
#define OUTPUT_CHANNELS 2
/**
* Open an input file and the required decoder.
* @param filename File to be opened
* @param[out] input_format_context Format context of opened file
* @param[out] input_codec_context Codec context of opened file
* @return Error code (0 if successful)
*/
static int open_input_file(const char *filename,
AVFormatContext **input_format_context,
AVCodecContext **input_codec_context)
{
AVCodecContext *avctx;
AVCodec *input_codec;
int error;
/* Open the input file to read from it. */
if ((error = avformat_open_input(input_format_context, filename, NULL,
NULL)) < 0) {
fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
filename, av_err2str(error));
*input_format_context = NULL;
return error;
}
/* Get information on the input file (number of streams etc.). */
if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
fprintf(stderr, "Could not open find stream info (error '%s')\n",
av_err2str(error));
avformat_close_input(input_format_context);
return error;
}
/* Make sure that there is only one stream in the input file. */
if ((*input_format_context)->nb_streams != 1) {
fprintf(stderr, "Expected one audio input stream, but found %d\n",
(*input_format_context)->nb_streams);
avformat_close_input(input_format_context);
return AVERROR_EXIT;
}
/* Find a decoder for the audio stream. */
if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codecpar->codec_id))) {
fprintf(stderr, "Could not find input codec\n");
avformat_close_input(input_format_context);
return AVERROR_EXIT;
}
/* Allocate a new decoding context. */
avctx = avcodec_alloc_context3(input_codec);
if (!avctx) {
fprintf(stderr, "Could not allocate a decoding context\n");
avformat_close_input(input_format_context);
return AVERROR(ENOMEM);
}
/* Initialize the stream parameters with demuxer information. */
error = avcodec_parameters_to_context(avctx, (*input_format_context)->streams[0]->codecpar);
if (error < 0) {
avformat_close_input(input_format_context);
avcodec_free_context(&avctx);
return error;
}
/* Open the decoder for the audio stream to use it later. */
if ((error = avcodec_open2(avctx, input_codec, NULL)) < 0) {
fprintf(stderr, "Could not open input codec (error '%s')\n",
av_err2str(error));
avcodec_free_context(&avctx);
avformat_close_input(input_format_context);
return error;
}
/* Save the decoder context for easier access later. */
*input_codec_context = avctx;
return 0;
}
/**
* Open an output file and the required encoder.
* Also set some basic encoder parameters.
* Some of these parameters are based on the input file's parameters.
* @param filename File to be opened
* @param input_codec_context Codec context of input file
* @param[out] output_format_context Format context of output file
* @param[out] output_codec_context Codec context of output file
* @return Error code (0 if successful)
*/
static int open_output_file(const char *filename,
AVCodecContext *input_codec_context,
AVFormatContext **output_format_context,
AVCodecContext **output_codec_context)
{
AVCodecContext *avctx = NULL;
AVIOContext *output_io_context = NULL;
AVStream *stream = NULL;
AVCodec *output_codec = NULL;
int error;
/* Open the output file to write to it. */
if ((error = avio_open(&output_io_context, filename,
AVIO_FLAG_WRITE)) < 0) {
fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
filename, av_err2str(error));
return error;
}
/* Create a new format context for the output container format. */
if (!(*output_format_context = avformat_alloc_context())) {
fprintf(stderr, "Could not allocate output format context\n");
return AVERROR(ENOMEM);
}
/* Associate the output file (pointer) with the container format context. */
(*output_format_context)->pb = output_io_context;
/* Guess the desired container format based on the file extension. */
if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
NULL))) {
fprintf(stderr, "Could not find output file format\n");
goto cleanup;
}
if (!((*output_format_context)->url = av_strdup(filename))) {
fprintf(stderr, "Could not allocate url.\n");
error = AVERROR(ENOMEM);
goto cleanup;
}
/* Find the encoder to be used by its name. */
if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
fprintf(stderr, "Could not find an AAC encoder.\n");
goto cleanup;
}
/* Create a new audio stream in the output file container. */
if (!(stream = avformat_new_stream(*output_format_context, NULL))) {
fprintf(stderr, "Could not create new stream\n");
error = AVERROR(ENOMEM);
goto cleanup;
}
avctx = avcodec_alloc_context3(output_codec);
if (!avctx) {
fprintf(stderr, "Could not allocate an encoding context\n");
error = AVERROR(ENOMEM);
goto cleanup;
}
/* Set the basic encoder parameters.
* The input file's sample rate is used to avoid a sample rate conversion. */
avctx->channels = OUTPUT_CHANNELS;
avctx->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
avctx->sample_rate = input_codec_context->sample_rate;
avctx->sample_fmt = output_codec->sample_fmts[0];
avctx->bit_rate = OUTPUT_BIT_RATE;
/* Allow the use of the experimental AAC encoder. */
avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
/* Set the sample rate for the container. */
stream->time_base.den = input_codec_context->sample_rate;
stream->time_base.num = 1;
/* Some container formats (like MP4) require global headers to be present.
* Mark the encoder so that it behaves accordingly.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
(简易)视频监控项目源码 (2000个子文件)
libfreetype.so.6.9.0 2.56MB
libcrypto.so.1.0.0 1.81MB
libsrt.so.1.5.0 761KB
libssl.so.1.0.0 394KB
libSDL_ttf-2.0.so.0 96KB
ffmpeg-all.1 1.34MB
ffmpeg-all.1 1.26MB
ffprobe-all.1 1.04MB
ffprobe-all.1 1009KB
ffmpeg-filters.1 748KB
ffmpeg-filters.1 697KB
ffmpeg-codecs.1 175KB
ffmpeg-codecs.1 163KB
ffmpeg-formats.1 152KB
ffmpeg-formats.1 142KB
ffmpeg.1 112KB
ffmpeg.1 110KB
libSDL_ttf-2.0.so.0.10.1 96KB
ffmpeg-devices.1 75KB
ffmpeg-devices.1 73KB
ffmpeg-protocols.1 64KB
ffmpeg-protocols.1 64KB
ffprobe.1 41KB
ffprobe.1 41KB
ffmpeg-utils.1 35KB
ffmpeg-utils.1 35KB
ffmpeg-bitstream-filters.1 29KB
ffmpeg-bitstream-filters.1 27KB
ffmpeg-resampler.1 14KB
ffmpeg-resampler.1 14KB
ffmpeg-scaler.1 9KB
ffmpeg-scaler.1 8KB
libavcodec.so.58.54.100 10.29MB
libavcodec.so.58.35.100 10.15MB
libavfilter.so.7.57.100 2.27MB
libavformat.so.58.29.100 1.94MB
libavformat.so.58.20.100 1.9MB
libavutil.so.56.31.100 430KB
libavutil.so.56.22.100 366KB
libswscale.so.5.5.100 354KB
libswscale.so.5.3.100 354KB
libswresample.so.3.5.100 74KB
libswresample.so.3.3.100 74KB
libavdevice.so.58.8.100 47KB
libavdevice.so.58.5.100 47KB
libpostproc.so.55.5.100 41KB
libpostproc.so.55.3.100 41KB
libavfilter.so.7.40.101 2.13MB
libx264.so.148 970KB
libx264.so.148 970KB
libswresample.so.3 74KB
libswresample.so.3 74KB
engine.3 35KB
EVP_CIPHER_CTX_type.3 33KB
EVP_CIPHER_CTX_flags.3 33KB
EVP_idea_cfb.3 33KB
EVP_CipherInit_ex.3 33KB
EVP_CIPHER_asn1_to_param.3 33KB
EVP_rc5_32_12_16_ecb.3 33KB
EVP_get_cipherbyobj.3 33KB
EVP_CIPHER_block_size.3 33KB
EVP_CIPHER_iv_length.3 33KB
EVP_CipherFinal_ex.3 33KB
EVP_get_cipherbyname.3 33KB
EVP_aes_256_gcm.3 33KB
EVP_EncryptInit_ex.3 33KB
EVP_CIPHER_CTX_cleanup.3 33KB
EVP_CIPHER_CTX_set_key_length.3 33KB
EVP_des_ede_cfb.3 33KB
EVP_rc5_32_12_16_cfb.3 33KB
EVP_CipherInit.3 33KB
EVP_desx_cbc.3 33KB
EVP_aes_192_ccm.3 33KB
EVP_CIPHER_CTX_ctrl.3 33KB
EVP_CIPHER_param_to_asn1.3 33KB
EVP_CIPHER_CTX_init.3 33KB
EVP_EncryptFinal_ex.3 33KB
EVP_CIPHER_CTX_mode.3 33KB
EVP_CIPHER_flags.3 33KB
EVP_rc5_32_12_16_cbc.3 33KB
EVP_bf_ecb.3 33KB
EVP_CIPHER_CTX_nid.3 33KB
EVP_rc4.3 33KB
EVP_CIPHER_CTX_get_app_data.3 33KB
EVP_aes_128_gcm.3 33KB
EVP_des_ofb.3 33KB
EVP_rc4_40.3 33KB
EVP_des_cbc.3 33KB
EVP_CIPHER_mode.3 33KB
EVP_des_ede3_ofb.3 33KB
EVP_des_cfb.3 33KB
EVP_cast5_ecb.3 33KB
EVP_CIPHER_CTX_set_padding.3 33KB
EVP_EncryptFinal.3 33KB
EVP_cast5_cfb.3 33KB
EVP_DecryptInit_ex.3 33KB
EVP_des_ede3_cfb.3 33KB
EVP_aes_192_gcm.3 33KB
EVP_rc2_40_cbc.3 33KB
EVP_des_ede_cbc.3 33KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Amosico
- 粉丝: 167
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 编辑cdd文件时需要的一个浮动license软件
- 非常好的电子设计小软件winhex非常好用的软件.zip
- 微信小程序中实现手机屏幕亮度调节技术解析与应用实例
- 基于SSM+maven+mysql实现的企业考勤管理系统 【源码+数据库】
- GBase8a-MPP-Cluster-NoLicense-8.6.2-build43-R11-redhat7.3-x86-64
- 162230326 王郅勋.zip
- Unity 插件-地形贴合到模型底面
- 基于matlab的空间博弈追逃算法源码(研究生毕设仿真高分项目)
- 非常好的电子设计小软件PCtoLCD2002完美版非常好用的软件.zip
- 新员工试用期考核表.xls
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功