如何削减MP3的一部分? [英] How to cut a part of the MP3?

查看:75
本文介绍了如何削减MP3的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要剪切MP3文件的一部分,例如从这一部分的1分钟20秒减少到3分40秒,保存一个新的MP3文件.
该怎么做?
我曾尝试以二进制模式读写MP3文件,虽然可以达到一定的效果,但是这种效果非常糟糕,因为每个MP3文件的比特率都不相同,所以我削减的时间长度是未知的. /> 这是我的代码的一部分,它是固定的比特率值.
还是我应该问一下,如何获得MP3文件的比特率?

I want to cut part of an MP3 file.For example, from 1 minute 20 seconds to 3 minutes and 40 seconds of this part, save for a new MP3 file.
How to do this?
I have tried to read and write MP3 files in binary mode, although you can achieve a certain effect, but this effect is very bad, because each MP3 file bitrate is not the same, so I cut the length of time is unknown.
Here is part of my code, which is a fixed bitrate value.
Or should I ask, how to get the bitrate of the MP3 files?

void CMp3PlayCutDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	int B = 38; // bitrate is fixed...too bad!
	
	CFileDialog dlg(FALSE);
	FILE *fp=NULL;
	FILE *ffp=NULL;
	
	TCHAR *FileName	= new char[m_FilePathName.GetLength()+1];
	strcpy(FileName, (LPCSTR)m_FilePathName);
	if (IDOK == dlg.DoModal())
	{
		fp = fopen(FileName, "rb");
		if (fp)
		{
			// Read file
			int len;
			fseek(fp, 0, SEEK_END);
			len = ftell(fp);
			fseek(fp, m_nBeginTime*B, SEEK_SET);
			char *p = new char[len-m_nBeginTime*B+1];
			fread(p, sizeof(char), (m_nEndTime - m_nBeginTime)*B, fp);
			fclose(fp);
			// Write file
			CString str = dlg.GetFileName();
			TCHAR *KeepFileName = new char[str.GetLength()+1];
			strcpy(KeepFileName, (LPCSTR)str);
			ffp = fopen(KeepFileName, "wb");
			if (ffp)
			{
				
				fwrite(p, sizeof(char), (m_nEndTime - m_nBeginTime)*B, ffp);
				fclose(ffp);
			}
			delete KeepFileName;
			delete p;
		}
	}
	delete FileName;
	

	
	

}

推荐答案

这是类似问题 [
This is an answer[^] to a similar question[^] on Cprogramming.com.

DougDbug写道:
DougDbug wrote:

MP3文件非常复杂.好吧...除了纯文本文件之外,大多数现实世界中的文件格式都相当复杂.开始学习文件格式的好地方是 wotsit.org [
codeproject.com有一个
示例程序 [文章/示例 [ mp3-converter.com [ LAME [

MP3 files are rather complicated. Well... Except for plain-text files, most real-world file formats are rather complicated. A good place to start learning about file formats is wotsit.org[^]. Their information on MP3 looks a little "light"... I didn''t see a link to an actual spec.

codeproject.com has an example program[^] that uses DirectShow (Microsoft) to open and convert an MP3. They also have an article/example[^] about the MP3 frame header.

MP3 files are divided into frames (MP3 was originally designed to be used with compressed audio/video files). Some data can be stored in "extra space" in preceding or following frames as part of the compression process. So, if you simply cut the file at the frame boundary, you may loose some data. You can learn a bit more about this at mp3-converter.com[^].

You can avoid this issue by decoding the file (into an array), cutting it, and then re-compressing it. But, since MP3 is lossy compression, you will degrade the audio quality if you do this. Most audio editors do exactly that, but there are a few special-purpose MP3 programs that can cut & join MP3s without re-coding. Windows comes with an MP3 decoder, but you''d will need an encoder (i.e. LAME[^]).

You might want to start-out experimenting with "regular" uncompressed WAV files. A WAV file is simpler. It''s easier to cut/paste and you can fairly-simply re-create or reformat the file-header.


这篇关于如何削减MP3的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆