📄 mp3audioheader.java
字号:
/**
* Returns the byte position of the first MP3 Frame that the
* <code>file</code> arguement refers to. This is the first byte of music
* data and not the ID3 Tag Frame.
*
* @return the byte position of the first MP3 Frame
*/
public long getMp3StartByte()
{
return startByte;
}
/**
* Set number of frames in this file, use Xing if exists otherwise ((File Size - Non Audio Part)/Frame Size)
*/
private void setNumberOfFrames()
{
numberOfFramesEstimate = (fileSize - startByte) / mp3FrameHeader.getFrameLength();
if(mp3XingFrame!=null&&mp3XingFrame.isFrameCountEnabled()==true)
{
numberOfFrames = mp3XingFrame.getFrameCount();
}
else
{
numberOfFrames = numberOfFramesEstimate;
}
}
/**
*
* @return The number of frames within the Audio File, calculated as accurrately as possible
*/
public long getNumberOfFrames()
{
return numberOfFrames;
}
/**
*
* @return The number of frames within the Audio File, calculated by dividing the filesize by
* the number of frames, this may not be the most accurate method available.
*/
public long getNumberOfFramesEstimate()
{
return numberOfFramesEstimate;
}
/**
* Set the time each frame contributes to the audio in fractions of seconds, the higher
* the sampling rate the shorter the audio segment provided by the frame,
* the number of samples is fixed by the MPEG Version and Layer
*/
private void setTimePerFrame()
{
timePerFrame = mp3FrameHeader.getNoOfSamples() / mp3FrameHeader.getSamplingRate().doubleValue();
//Because when calculating framelenggth we alter the calculation slightly for MPEGVersion2
//we seem to have to make a correspondinf modification to get the correct time
if(
(mp3FrameHeader.getVersion()==MPEGFrameHeader.VERSION_2) ||
(mp3FrameHeader.getVersion()==MPEGFrameHeader.VERSION_2_5))
{
if(
(mp3FrameHeader.getLayer()==MPEGFrameHeader.LAYER_II)||
(mp3FrameHeader.getLayer()==MPEGFrameHeader.LAYER_III)
)
{
timePerFrame = timePerFrame / 2;
}
}
}
/**
*
* @return the the time each frame contributes to the audio in fractions of seconds
*/
private double getTimePerFrame()
{
return timePerFrame;
}
/**
* Estimate the length of the audio track in seconds
* Calculation is Number of frames multiplied by the Time Per Frame using the first frame as a prototype
* Time Per Frame is the number of samples in the frame (which is defined by the MPEGVersion/Layer combination)
* divided by the sampling rate, i.e the higher the sampling rate the shorter the audio represented by the frame is going
* to be.
*/
private void setTrackLength()
{
trackLength = numberOfFrames * getTimePerFrame();
}
/**
*
* @return Track Length in seconds
*/
public double getPreciseTrackLength()
{
return trackLength;
}
public int getTrackLength() {
return (int) getPreciseTrackLength();
}
/**
* Return the length in user friendly format
*/
public String getTrackLengthAsString()
{
try
{
final long lengthInSecs = (long)getTrackLength();
final Date timeIn = timeInFormat.parse(String.valueOf(lengthInSecs));
return timeOutFormat.format(timeIn);
}
catch (ParseException pe)
{
return "";
}
}
/**
*
* @return the audio file type
*/
public String getEncodingType()
{
return TYPE_MP3;
}
/**
* Set bitrate in kbps, if Vbr use Xingheader if possible
*/
private void setBitRate()
{
if(mp3XingFrame!=null&&mp3XingFrame.isVbr())
{
if(mp3XingFrame.isAudioSizeEnabled()&&mp3XingFrame.getAudioSize()>0)
{
bitrate = (long)((mp3XingFrame.getAudioSize() * CONVERTS_BYTE_TO_BITS)/ (timePerFrame * getNumberOfFrames() * CONVERT_TO_KILOBITS ));
}
else
{
bitrate = (long)(((fileSize - startByte) * CONVERTS_BYTE_TO_BITS) / (timePerFrame * getNumberOfFrames() * CONVERT_TO_KILOBITS ));
}
}
else
{
bitrate = mp3FrameHeader.getBitRate();
}
}
private void setEncoder()
{
if(mp3XingFrame!=null)
{
if(mp3XingFrame.getLameFrame()!=null)
{
encoder=mp3XingFrame.getLameFrame().getEncoder();
return;
}
}
}
/**
*
* @return bitrate in kbps, no indicator is provided as to whether or not it is vbr
*/
public long getBitRateAsNumber()
{
return bitrate;
}
/**
*
* @return the BitRate of the Audio, to distinguish cbr from vbr we add a '~'
* for vbr.
*/
public String getBitRate()
{
if(mp3XingFrame!=null&&mp3XingFrame.isVbr()==true)
{
return isVbrIdentifier + String.valueOf(bitrate);
}
else
{
return String.valueOf(bitrate);
}
}
/**
*
* @return the sampling rate in Hz
*/
public int getSampleRateAsNumber()
{
return mp3FrameHeader.getSamplingRate();
}
/**
* @return the sampling rate as string
*/
public String getSampleRate()
{
return String.valueOf(mp3FrameHeader.getSamplingRate());
}
/**
*
* @return MPEG Version (1-3)
*/
public String getMpegVersion()
{
return mp3FrameHeader.getVersionAsString();
}
/**
*
* @return MPEG Layer (1-3)
*/
public String getMpegLayer()
{
return mp3FrameHeader.getLayerAsString();
}
/**
*
* @return the format of the audio (i.e. MPEG-1 Layer3)
*/
public String getFormat()
{
return mp3FrameHeader.getVersionAsString() + " " + mp3FrameHeader.getLayerAsString();
}
/**
*
* @return the Channel Mode such as Stero or Mono
*/
public String getChannels()
{
return mp3FrameHeader.getChannelModeAsString();
}
/**
*
* @return Emphasis
*/
public String getEmphasis()
{
return mp3FrameHeader.getEmphasisAsString();
}
/**
*
* @return if the bitrate is variable, Xing header takes precedence if we have one
*/
public boolean isVariableBitRate()
{
if(mp3XingFrame!=null)
{
return mp3XingFrame.isVbr();
}
else
{
return mp3FrameHeader.isVariableBitRate();
}
}
public boolean isProtected()
{
return mp3FrameHeader.isProtected();
}
public boolean isPrivate()
{
return mp3FrameHeader.isPrivate();
}
public boolean isCopyrighted()
{
return mp3FrameHeader.isCopyrighted();
}
public boolean isOriginal()
{
return mp3FrameHeader.isOriginal();
}
public boolean isPadding()
{
return mp3FrameHeader.isPadding();
}
/**
*
* @return encoder
*/
public String getEncoder()
{
return encoder;
}
/**
* Set the size of the file, required in some calculations
* @param fileSize
*/
private void setFileSize(long fileSize)
{
this.fileSize = fileSize;
}
/**
*
* @return a string represntation
*/
public String toString()
{
String s = "fileSize:" + fileSize
+ " encoder:" + encoder
+ " startByte:" + startByte
+ " numberOfFrames:" + numberOfFrames
+ " numberOfFramesEst:"+ numberOfFramesEstimate
+ " timePerFrame:" + timePerFrame
+ " bitrate:" + bitrate
+ " trackLength:" + getTrackLengthAsString();
if(this.mp3FrameHeader!=null)
{
s +=mp3FrameHeader.toString();
}
if(this.mp3XingFrame!=null)
{
s +=mp3XingFrame.toString();
}
return s;
}
public static void main(String[] args)throws Exception {
MP3AudioHeader head=new MP3AudioHeader(new File("D:\\执着.flac"));
System.out.println(head.getSampleRate());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -