当前位置:  -> 首页 -> 原创文章 -> 如何利用Bass函数库对Mp3文件分割

上一篇 | 下一篇
如何利用Bass函数库对Mp3文件分割
作者:洛羽叶  点击率:4719  发布时间:2009-01-04

 

所需函数库:  

       bass.dll(Un4seen Developments)音频函数库

       bassenc.dll(Un4seen Developments)扩展函数库

       lame.exe(Un4seen Developments)Mp3文件编码命令行工具

       从www. un4seen.com可以下载得到  

 

un4seen自己的播放器界面之一

 

 

授权方式

       non-commercial(非商业用途):免费

       Shareware licence(共享产品): €100

       Single Commercial licence(单一商业产品): €950

       Unlimited Commercial licence(单一商业产品不限数量): €2450 

 

步骤: 

1.  打开文件

       mStreamHandle = BASS_StreamCreateFile(BASSFALSE, “c:\test.mp3”, 0, 0, 0)

 

2.  播放文件

       BASS_ChannelPlay(mStreamHandle, BASSFALSE)

 

3.  暂停播放

       BASS_ChannelPause(mStreamHandle)

 

4.  停止播放

       BASS_ChannelStop(mStreamHandle)

 

5.  得到当前播放位置信息

       'get total size

       lSize = BASS_ChannelGetLength(mStreamHandle, BASS_POS_BYTE)

       dSize = BASS_ChannelBytes2Seconds(mStreamHandle, lSize)

     

       'get current position

       pos = BASS_ChannelGetPosition(mStreamHandle, BASS_POS_BYTE)

       posSecond = BASS_ChannelBytes2Seconds(mStreamHandle, pos)

 

6.  进行切割

Private Sub Split(ByVal strSourceFile As String, ByVal strDestFile As String, ByVal lBegin As Long, ByVal lEnd As Long)

    On Error GoTo Last:

    Dim StreamHandle As Long

    Dim wr As WAVEHEADER_RIFF

    Dim wf As WAVEFORMAT

    Dim wd As WAVEHEADER_data

    Dim info As BASS_CHANNELINFO

    Dim handle As Long

    Dim i As Long

    Dim m As Long

    Dim n As Long

    Dim pos As Long

    Dim fs As FileSystemObject

    Dim bFinish As Boolean

   

    'init variable

    Set fs = New FileSystemObject

    bFinish = False

   

    'create file stream

    StreamHandle = BASS_StreamCreateFile(BASSFALSE, StrPtr(strSourceFile), 0, 0, BASS_STREAM_DECODE)

   

    'get channel information

    Call BASS_ChannelGetInfo(StreamHandle, info) 

 

    wf.wFormatTag = 1

    wf.nChannels = info.chans

    wf.wBitsPerSample = IIf(info.flags And BASS_SAMPLE_8BITS, 8, 16)

    wf.nBlockAlign = wf.nChannels * wf.wBitsPerSample / 8

    wf.nSamplesPerSec = info.freq

    wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign 

 

    wf.wfBlockType = &H20746D66

    wf.wfBlockSize = 16

   

    wr.RIFF = &H46464952

    wr.riffBlockSize = 0

    wr.riffBlockType = &H45564157

   

    wd.dataBlockType = &H61746164

    wd.dataBlockSize = 0

   

    'create new file

    handle = FreeFile(0)

    Open strDestFile For Binary Lock Read Write As handle 

 

    Put handle, , wr

    Put handle, , wf

    Put handle, , wd 

 

    'get start position

    Call BASS_ChannelSetPosition(StreamHandle, lBegin, BASS_POS_BYTE)

   

    'write data to new file

    Do While (BASS_ChannelIsActive(StreamHandle) And (n < (lEnd - lBegin)) And (Not bFinish))

        If ((n + 20000) > (lEnd - lBegin)) Then 'write last buffer

            ReDim buf(lEnd - lBegin - n) As Byte

            i = BASS_ChannelGetData(StreamHandle, buf(0), lEnd - lBegin - n)

            bFinish = True

        Else 'write buffer

            i = BASS_ChannelGetData(StreamHandle, buf(0), 20000)

        End If

       

        ' write data to WAV file

        Put handle, , buf

           

        'get total size

        n = n + i

       

        'get current position

        pos = BASS_ChannelGetPosition(StreamHandle, BASS_POS_BYTE)        

 

 

 

   

 

   

    Exit Sub

Last:   

End Sub

 

7.  将分割后的文件转换为Mp3格式

       Shell(App.Path & "\lame.exe """ & strSourceFile & """ """ & strDestFile & """", vbHide) 

 

   

    'complete WAV header

    wr.riffBlockSize = pos + 44 - 8

    wd.dataBlockSize = pos 

 

    Put handle, 41, wd.dataBlockSize 

 

   

    'close stream

Call BASS_StreamFree(StreamHandle)   

 

    Close handle 

 

    Put handle, 5, wr.riffBlockSize 

    Loop 

标签: BASS VB MP3
引用地址:本站原创
   站点首页      技术人生      旅途足迹      我要留言      友情链接      关于站长   
[本站统计]
在线人数:8
今日访问:101
总访问量:1472742
Copyright 2006-2022 EasyWeb 1.6 订阅 All Rights Reserved
粤ICP备08028977号-1
www.luoriver.com
Created by WWH in 2006