求ffmpeg音频压缩代码(wav压缩成wma)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求ffmpeg音频压缩代码(wav压缩成wma)相关的知识,希望对你有一定的参考价值。
求大虾们提供一个ffmpeg音频压缩源代码(wav压缩成wma),或者说明一下ffmpeg的内部结构和各模块函数的用法,小弟因为没有接触过ffmpeg,不知道ffmpeg怎么个构架,怎么个处理过程,如有赐教,小弟感恩不尽!!
这个简单。大致的思路是1.打开wav文件
2.打开要输出的wma文件
3.不停的读取数据帧
4.读取以后解码并写入wma
5.关闭wav文件
6.关闭wma文件
重新写例子太麻烦贴点代码吧
#include "Debug.h"
#include "FFMpegAVFileReader.h"
#include "FFMpegAVFileReaderFactory.h"
#include <sstream>
#include "yk_convert.h"
static char h264_head[4] = (char)0x00,(char)0x00,(char)0x00,(char)0x01;
namespace YK
FFMpegAVFileReader::FFMpegAVFileReader()
ffmpeg_avcodec_init();
ffmpeg_av_register_all();
av_log_set_callback(ffmpeg_log_callback);
m_format_context = 0;
m_input_format = 0;
m_format_parameters = 0;
m_packet = (AVPacket*)av_mallocz(sizeof(AVPacket));
FFMpegAVFileReader::~FFMpegAVFileReader()
Close();
av_free(m_packet);
avfile_reader_param_t* FFMpegAVFileReader::GetParam()
//YK::AutoLock l(m_lock);
return &m_param;
void FFMpegAVFileReader::Seek(int pts)
YK::AutoLock l(m_lock);
int ret = av_seek_frame(m_format_context,-1,(YK::int64_t)pts * (YK::int64_t)1000,AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY);
//if(ret >= 0)
//
// for(int i = 0; i < m_format_context->nb_streams; i++)
//
// avcodec_flush_buffers( m_format_context->streams[i]->codec );
//
//
//std::stringstream ss;
//ss << "pts = " << pts
// << " ret = " << ret << std::endl;
//OutputDebugString(ss.str().data());
void FFMpegAVFileReader::GetDuration(double& start,double& end)
//YK::AutoLock l(m_lock);
start = (double)(m_start_time / 1000000);
end = start + (double)(m_duration / 1000000);
service_error_t FFMpegAVFileReader::Open(int time_out)
YK::AutoLock l(m_lock);
service_error_t service_error;
// 打开文件
if (av_open_input_file(&m_format_context,TToANSI(m_param.input_file_path).data(),m_input_format,0,m_format_parameters) < 0)
service_error.init(service_error_type_failed,"av_open_input_file failed");
return service_error;
// 查询流信息
if(av_find_stream_info(m_format_context) < 0)
service_error.init(service_error_type_failed,"av_find_stream_info failed");
return service_error;
m_start_time = m_format_context->start_time;
m_duration = m_format_context->duration;
#ifdef _DEBUG
pFile = fopen("D:/1.out","w+b");
#endif
// 保存流信息
av_stream_info_t av_stream_info;
for(unsigned int i = 0; i < m_format_context->nb_streams; i++)
AVStream *st = m_format_context->streams[i];
AVCodecContext *enc = st->codec;
if(enc->codec_type == AVMEDIA_TYPE_AUDIO)
// 音频
av_stream_info.av_stream_info_type = av_stream_info_type_audio;
av_stream_info.codec_id = codec_id_none;
//channel_layout = enc->channel_layout;
av_stream_info.audio_channels = enc->channels;
av_stream_info.audio_samplepersec = enc->sample_rate;
av_stream_info.audio_bitpersample = yk_sample_format(enc->sample_fmt);
//audio_sample_fmt = enc->sample_fmt;
//input_codecs[nb_icodecs++] = avcodec_find_decoder_by_name(audio_codec_name);
av_stream_info.codec_id = yk_code_id(enc->codec_id);
av_stream_info.extradata_size = enc->extradata_size;
if(av_stream_info.extradata_size)
memcpy(av_stream_info.extradata,enc->extradata,enc->extradata_size);
if(av_stream_info.codec_id == codec_id_mp3)
av_stream_info.extradata_size = sizeof(mpeg1_waveformat_extradata);
mpeg1_waveformat_extradata* pMpeg1WaveFormat = (mpeg1_waveformat_extradata*)av_stream_info.extradata;
pMpeg1WaveFormat->dwHeadBitrate = enc->bit_rate;
pMpeg1WaveFormat->dwPTSHigh = 0;
pMpeg1WaveFormat->dwPTSLow = 0;
pMpeg1WaveFormat->fwHeadFlags = 25;
pMpeg1WaveFormat->fwHeadLayer = ACM_MPEG_LAYER3;
pMpeg1WaveFormat->fwHeadMode = ACM_MPEG_STEREO;
pMpeg1WaveFormat->fwHeadModeExt = 1;
pMpeg1WaveFormat->wHeadEmphasis = 1;
AddStreamInfo(av_stream_info);
m_stream_audio_index = i;
else if(enc->codec_type == AVMEDIA_TYPE_VIDEO)
// 视频
av_stream_info.av_stream_info_type = av_stream_info_type_video;
av_stream_info.codec_id = codec_id_none;
av_stream_info.video_width = enc->width;
av_stream_info.video_height = enc->height;
av_stream_info.video_profile = enc->profile;
av_stream_info.video_level = enc->level;
if(av_stream_info.video_profile < 0)
av_stream_info.video_profile = 77;
if(av_stream_info.video_level < 0)
av_stream_info.video_level = 30;
av_stream_info.extradata_size = 参考技术A ?图形界面的还是命令行工具,我知道有个命令行工具ffmpeg,不过解码swf不怎么方便,起码不能是压缩的swf
还有一篇文章希望有用:
另外:flash 8自带的Flash 8 Video Encoder能把别的影片转变成flv格式的,至于把swf转变成flv???就得你用flash 8将SWF导入然后变换成FLV
-----------------------下面方法仅供参考-------------
《MP4/RM转换专家》是梦幻科技的又一优秀专业力作。MP4/RM转换专家支持将几乎所有视频格式如:RM/RMVB/VOB/AVI/MPEG/DAT/VCD/SVCD/DVD/ASF/WMV/MOV/QT/MP4/3GP/3GPP2/DV/YUV等视频文件转换为普通MP4机、SONY PSP机、iPod Video、3GP/3GPP2/MP4手机或DVD影碟机等硬件支持的AVI/MPEG4/3GP/3GPP2/MP4视频格式!软件更是支持Flash的SWF/FLV文件的导出,同时还允许将多种格式转换为RM和RMVB格式。
vs2010音频文件压缩 调用lame_enc.dll将WAV格式转换成MP3
/*
//My_lame.h
*/
#pragma once
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "BladeMP3EncDLL.h"
class My_lame
{
public:
My_lame(void);
~My_lame(void);
HINSTANCE hDLL;
BEINITSTREAM beInitStream;
BEENCODECHUNK beEncodeChunk;
BEDEINITSTREAM beDeinitStream;
BECLOSESTREAM beCloseStream;
BEVERSION beVersion;
BEWRITEVBRHEADER beWriteVBRHeader;
BEWRITEINFOTAG beWriteInfoTag;
int km_convertMp3();
};
/*
//My_lame.cpp
/*
#include "StdAfx.h"
#include "My_lame.h"
#include <string>
using namespace std;
My_lame::My_lame(void)
{
hDLL =NULL;
beInitStream=NULL;
beEncodeChunk=NULL;
beDeinitStream=NULL;
beCloseStream=NULL;
beVersion=NULL;
beWriteVBRHeader=NULL;
beWriteInfoTag=NULL;
hDLL = LoadLibrary("lame_enc.dll");
}
My_lame::~My_lame(void)
{
FreeLibrary(hDLL);
}
int My_lame::km_convertMp3()
{
FILE* pFileIn =NULL;
FILE* pFileOut =NULL;
BE_VERSION Version ={0,};
BE_CONFIG beConfig ={0,};
CHAR* strFileIn1="F:\\1.wav";
CHAR* strFileOut1="F:\\1.mp3";
DWORD dwSamples =0;
DWORD dwMP3Buffer =0;
HBE_STREAM hbeStream =0;
BE_ERR err =0;
PBYTE pMP3Buffer =NULL;
PSHORT pWAVBuffer =NULL;
// Load lame_enc.dll library (Make sure though that you set the
// project/settings/debug Working Directory correctly, otherwhise the DLL can‘t be loaded
hDLL = LoadLibrary("lame_enc.dll");
if ( NULL == hDLL )
{
hDLL = LoadLibrary("lame_enc.dll");
}
if( NULL == hDLL )
{
fprintf(stderr,"Error loading lame_enc.DLL");
return -1;
}
// Get Interface functions from the DLL
beInitStream = (BEINITSTREAM) GetProcAddress(hDLL, TEXT_BEINITSTREAM);
beEncodeChunk = (BEENCODECHUNK) GetProcAddress(hDLL, TEXT_BEENCODECHUNK);
beDeinitStream = (BEDEINITSTREAM) GetProcAddress(hDLL, TEXT_BEDEINITSTREAM);
beCloseStream = (BECLOSESTREAM) GetProcAddress(hDLL, TEXT_BECLOSESTREAM);
beVersion = (BEVERSION) GetProcAddress(hDLL, TEXT_BEVERSION);
beWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(hDLL,TEXT_BEWRITEVBRHEADER);
beWriteInfoTag = (BEWRITEINFOTAG) GetProcAddress(hDLL,TEXT_BEWRITEINFOTAG);
// Check if all interfaces are present
if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion || !beWriteVBRHeader)
{
printf("Unable to get LAME interfaces");
return -1;
}
// Get the version number
beVersion( &Version );
printf(
"lame_enc.dll version %u.%02u (%u/%u/%u)\n"
"lame_enc Engine %u.%02u\n"
"lame_enc homepage at %s\n\n",
Version.byDLLMajorVersion, Version.byDLLMinorVersion,
Version.byDay, Version.byMonth, Version.wYear,
Version.byMajorVersion, Version.byMinorVersion,
Version.zHomepage);
// Try to open the WAV file, be sure to open it as a binary file!
pFileIn = fopen( strFileIn1, "rb" );
// Check file open result
if(pFileIn == NULL)
{
return -1;
}
// Open MP3 file
pFileOut= fopen(strFileOut1,"wb+");
// Check file open result
if(pFileOut == NULL)
{
fprintf(stderr,"Error creating file %s", strFileOut1);
return -1;
}
memset(&beConfig,0,sizeof(beConfig)); // clear all fields
// use the LAME config structure
beConfig.dwConfig = BE_CONFIG_LAME;
// this are the default settings for testcase.wav
beConfig.format.LHV1.dwStructVersion = 1;
beConfig.format.LHV1.dwStructSize = sizeof(beConfig);
beConfig.format.LHV1.dwSampleRate = 44100; // INPUT FREQUENCY
beConfig.format.LHV1.dwReSampleRate = 0; // DON"T RESAMPLE
beConfig.format.LHV1.nMode = BE_MP3_MODE_JSTEREO; // OUTPUT IN STREO
beConfig.format.LHV1.dwBitrate = 128; // MINIMUM BIT RATE
beConfig.format.LHV1.nPreset = LQP_R3MIX; // QUALITY PRESET SETTING
beConfig.format.LHV1.dwMpegVersion = MPEG1; // MPEG VERSION (I or II)
beConfig.format.LHV1.dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL
beConfig.format.LHV1.dwEmphasis = 0; // NO EMPHASIS TURNED ON
beConfig.format.LHV1.bOriginal = TRUE; // SET ORIGINAL FLAG
beConfig.format.LHV1.bWriteVBRHeader = TRUE; // Write INFO tag
beConfig.format.LHV1.bNoRes = TRUE; // No Bit resorvoir
// Init the MP3 Stream
err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
// Check result
if(err != BE_ERR_SUCCESSFUL)
{
fprintf(stderr,"Error opening encoding stream (%lu)", err);
return -1;
}
// Allocate MP3 buffer
pMP3Buffer = new BYTE[dwMP3Buffer];
// Allocate WAV buffer
pWAVBuffer = new SHORT[dwSamples];
// Check if Buffer are allocated properly
if(!pMP3Buffer || !pWAVBuffer)
{
printf("Out of memory");
return -1;
}
DWORD dwRead=0;
DWORD dwWrite=0;
DWORD dwDone=0;
DWORD dwFileSize=0;
// Seek to end of file
fseek(pFileIn,0,SEEK_END);
// Get the file size
dwFileSize=ftell(pFileIn);
// Seek back to start of WAV file,
// but skip the first 44 bytes, since that‘s the WAV header
fseek(pFileIn,44,SEEK_SET);
// Convert All PCM samples
while ( (dwRead=fread(pWAVBuffer,sizeof(SHORT),dwSamples,pFileIn)) >0 )
{
// Encode samples
err = beEncodeChunk(hbeStream, dwRead, pWAVBuffer, pMP3Buffer, &dwWrite);
// Check result
if(err != BE_ERR_SUCCESSFUL)
{
beCloseStream(hbeStream);
fprintf(stderr,"beEncodeChunk() failed (%lu)", err);
return -1;
}
// write dwWrite bytes that are returned in tehe pMP3Buffer to disk
if(fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
{
fprintf(stderr,"Output file write error");
return -1;
}
dwDone += dwRead*sizeof(SHORT);
printf("Done: %0.2f%% \r", 100 * (float)dwDone/(float)(dwFileSize));
}
// Deinit the stream
err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
// Check result
if(err != BE_ERR_SUCCESSFUL)
{
beCloseStream(hbeStream);
fprintf(stderr,"beExitStream failed (%lu)", err);
return -1;
}
// Are there any bytes returned from the DeInit call?
// If so, write them to disk
if( dwWrite )
{
if( fwrite( pMP3Buffer, 1, dwWrite, pFileOut ) != dwWrite )
{
fprintf(stderr,"Output file write error");
return -1;
}
}
// close the MP3 Stream
beCloseStream( hbeStream );
// Delete WAV buffer
delete [] pWAVBuffer;
// Delete MP3 Buffer
delete [] pMP3Buffer;
// Close input file
fclose( pFileIn );
// Close output file
fclose( pFileOut );
if ( beWriteInfoTag )
{
// Write the INFO Tag
//change
//beWriteInfoTag( hbeStream, strFileOut );
beWriteInfoTag( hbeStream, strFileOut1 );
}
else
{
//change
//beWriteVBRHeader( strFileOut );
beWriteVBRHeader( strFileOut1 );
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
My_lame lame;
lame.km_convertMp3();
return 0;
}
再把dll文件加进去,进行相应的设置,就可以了。
以上是关于求ffmpeg音频压缩代码(wav压缩成wma)的主要内容,如果未能解决你的问题,请参考以下文章