VS2010旗舰版VB.NET版本音频剪辑代码2024-8-10

Imports System.ComponentModel
Imports System.IO
Imports System.Diagnostics
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
Private WithEvents bgWorker As New BackgroundWorker
Private ffmpegPath As String = “C:\ffmpeg-master-latest-win64-gpl-shared\bin\ffmpeg.exe” 'FFMPEG 安装路径

' outputDevice 声明为类级别的私有变量
Private outputDevice As NAudio.Wave.DirectSoundOut

' 新增:用于表示音频数据的数组
Private audioData() As Byte


' 用于存储打开的音频文件路径
Private audioFilePaths As New List(Of String)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
   


    TrackBar1.Minimum = 0
    TrackBar1.Maximum = 200 ' 假设音频时长为 100 秒
    TrackBar1.TickFrequency = 1


    ' 设置结束滑动条的属性

    TrackBar2.Minimum = 0
    TrackBar2.Maximum = 200 ' 假设音频时长为 100 秒
    TrackBar2.TickFrequency = 1


End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    ' 打开音频文件选择框,选择要剪辑的音频文件
    Dim openFileDialog As New OpenFileDialog()
    openFileDialog.Filter = "音频文件|*.mp3;*.wav"
    If openFileDialog.ShowDialog() = DialogResult.OK Then
        ' 选择的文件路径显示在文本框中
        txtFilePath.Text = openFileDialog.FileName

        ' 加载音频数据并绘制波形图
        LoadAndDrawWaveform(openFileDialog.FileName)
    End If
End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    ' 打开保存对话框,选择剪辑后的音频文件保存路径
    Dim saveFileDialog As New SaveFileDialog()
    saveFileDialog.Filter = "音频文件|*.mp3;*.wav"
    If saveFileDialog.ShowDialog() = DialogResult.OK Then
        ' 选择的保存路径显示在文本框中
        txtSavePath.Text = saveFileDialog.FileName
    End If
End Sub

Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
    ' 播放音频
    If File.Exists(txtFilePath.Text) Then
        ' 用 SoundPlayer 或 NAudio 库播放选定的音频
        ' 例:使用 NAudio 播放
        Dim audioFile As New NAudio.Wave.WaveFileReader(txtFilePath.Text)
        outputDevice = New NAudio.Wave.DirectSoundOut()
        outputDevice.Init(New NAudio.Wave.WaveChannel32(audioFile))
        outputDevice.Play()
    Else
        MessageBox.Show("请选择要播放的音频文件")
    End If
End Sub

Private Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
    ' 停止音频播放
    ' 例:使用 NAudio 停止
    If outputDevice IsNot Nothing Then
        outputDevice.Stop()
    End If
End Sub

Private Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
    If File.Exists(txtFilePath.Text) Then
        Dim startTime As Integer = TrackBar1.Value
        Dim endTime As Integer = TrackBar2.Value

        If startTime >= endTime Then
            MessageBox.Show("起始位置不能大于等于结束位置")
            Exit Sub
        End If

        Dim parameters As String = "-i " & Chr(34) & txtFilePath.Text & Chr(34) & " -acodec copy -ss " & startTime & " -to " & endTime & " " & Chr(34) & txtSavePath.Text & Chr(34)

        RunFFMPEGProcess(parameters)
    Else
        MessageBox.Show("请选择要裁剪的音频文件")
    End If
    MessageBox.Show("剪辑完成!")
End Sub



Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    ' 执行耗时的音频处理任务
    ' 例:在后台线程中调用 FFMPEG 剪辑音频
    Dim startTime As String = TrackBar1.Value.ToString() ' 剪辑的开始时间
    Dim endTime As String = TrackBar2.Value.ToString() ' 剪辑的结束时间
    Dim parameters As String = "-i " & Chr(34) & txtFilePath.Text & Chr(34) & " -acodec copy -ss " & startTime & " -to " & endTime & " " & Chr(34) & txtSavePath.Text & Chr(34)

    RunFFMPEGProcess(parameters)
End Sub

Private Sub BackgroundWorker2_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
    ' 后台任务完成后的处理
    If e.Error IsNot Nothing Then
        MessageBox.Show("出现错误:" & e.Error.Message)
    Else
        MessageBox.Show("音频剪辑完成")
    End If
End Sub

' 新增:加载音频数据并绘制波形图的方法
Private Sub LoadAndDrawWaveform(ByVal filePath As String)
    ' 加载音频数据
    Using reader As New BinaryReader(File.OpenRead(filePath))
        audioData = reader.ReadBytes(CInt(reader.BaseStream.Length))
    End Using

    ' 绘制波形图
    Using g As Graphics = PictureBox1.CreateGraphics()
        g.Clear(Color.White)
        Dim width = PictureBox1.Width
        Dim height = PictureBox1.Height

        ' 调整计算步长,以适应图片框的大小
        Dim s = audioData.Length / width

        For x As Integer = 0 To width - 1
            Dim average As Double = 0
            For i As Integer = x * s To Math.Min((x + 1) * s - 1, audioData.Length - 1)
                average += audioData(i)
            Next
            average /= s

            ' 调整纵坐标计算,以完整显示波形
            Dim y As Integer = CInt(height - average * height / 256)
            If x = 0 Then
                g.DrawLine(Pens.Black, x, y, x, y)
            Else
                g.DrawLine(Pens.Black, x - 1, PreviousY, x, y)
            End If
            PreviousY = y
        Next
    End Using
End Sub

' 新增:起始滑动条滚动事件处理
Private PreviousY As Integer
Private Sub startTrackBarTrackBar1_Scroll(ByVal sender As Object, ByVal e As EventArgs)
    ' 根据起始滑动条位置重新计算并显示波形
    Dim startPosition As Integer = TrackBar1.Value
    Dim endPosition As Integer = TrackBar2.Value

    If startPosition > endPosition Then
        MessageBox.Show("起始位置不能大于结束位置")
        TrackBar1.Value = endPosition
        Exit Sub
    End If

    Dim newAudioData() As Byte

    ' 假设音频采样率为 44100,计算截取的数据范围
    Dim startIndex As Integer = startPosition * 44100
    Dim endIndex As Integer = Math.Min(endPosition * 44100, audioData.Length - 1)

    ReDim newAudioData(endIndex - startIndex)
    Array.Copy(audioData, startIndex, newAudioData, 0, endIndex - startIndex)

    ' 重新绘制波形图
    Using g As Graphics = PictureBox1.CreateGraphics()
        g.Clear(Color.White)
        Dim width = PictureBox1.Width
        Dim height = PictureBox1.Height

        ' 调整计算步长,以适应图片框的大小
        Dim s = newAudioData.Length / width

        For x As Integer = 0 To width - 1
            Dim average As Double = 0
            For i As Integer = x * s To Math.Min((x + 1) * s - 1, newAudioData.Length - 1)
                average += newAudioData(i)
            Next
            average /= s

            ' 调整纵坐标计算,以完整显示波形
            Dim y As Integer = CInt(height - average * height / 256)
            If x = 0 Then
                g.DrawLine(Pens.Black, x, y, x, y)
            Else
                g.DrawLine(Pens.Black, x - 1, PreviousY, x, y)
            End If
            PreviousY = y
        Next
    End Using
End Sub

' 新增:结束滑动条滚动事件处理
Private Sub TrackBar2_Scroll(ByVal sender As Object, ByVal e As EventArgs)
    ' 根据结束滑动条位置重新计算并显示波形
    Dim startPosition As Integer = TrackBar1.Value
    Dim endPosition As Integer = TrackBar2.Value

    If startPosition > endPosition Then
        MessageBox.Show("起始位置不能大于结束位置")
        TrackBar2.Value = startPosition
        Exit Sub
    End If

    Dim newAudioData() As Byte

    ' 假设音频采样率为 44100,计算截取的数据范围
    Dim startIndex As Integer = startPosition * 44100
    Dim endIndex As Integer = Math.Min(endPosition * 44100, audioData.Length - 1)

    ReDim newAudioData(endIndex - startIndex)
    Array.Copy(audioData, startIndex, newAudioData, 0, endIndex - startIndex)

    ' 重新绘制波形图
    Using g As Graphics = PictureBox1.CreateGraphics()
        g.Clear(Color.White)
        Dim width = PictureBox1.Width
        Dim height = PictureBox1.Height

        ' 调整计算步长,以适应图片框的大小
        Dim s = newAudioData.Length / width

        For x As Integer = 0 To width - 1
            Dim average As Double = 0
            For i As Integer = x * s To Math.Min((x + 1) * s - 1, newAudioData.Length - 1)
                average += newAudioData(i)
            Next
            average /= s

            ' 调整纵坐标计算,以完整显示波形
            Dim y As Integer = CInt(height - average * height / 256)
            If x = 0 Then
                g.DrawLine(Pens.Black, x, y, x, y)
            Else
                g.DrawLine(Pens.Black, x - 1, PreviousY, x, y)
            End If
            PreviousY = y
        Next
    End Using
End Sub



Private Sub RunFFMPEGProcess(ByVal parameters As String)
    ' 启动 FFMPEG 进程
    Dim startInfo As New ProcessStartInfo()
    startInfo.FileName = ffmpegPath
    startInfo.Arguments = parameters
    startInfo.UseShellExecute = False
    startInfo.RedirectStandardError = True
    startInfo.CreateNoWindow = True

    Using process As Process = Process.Start(startInfo)
        Using reader As StreamReader = process.StandardError
            ' 获取 FFMPEG 输出信息
            Dim result As String = reader.ReadToEnd()
            Console.WriteLine(result)
        End Using
    End Using
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
   ' 打开文件选择对话框,允许选择多个文件
    Dim openFileDialog As New OpenFileDialog()
    openFileDialog.Filter = "音频文件|*.mp3;*.wav"
    openFileDialog.Multiselect = True

    If openFileDialog.ShowDialog() = DialogResult.OK Then
        ' 将选择的文件路径逐个添加到列表
        For Each filePath As String In openFileDialog.FileNames
            audioFilePaths.Add(filePath)
            ListBoxFiles.Items.Add(Path.GetFileName(filePath))
        Next
    End If
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    If audioFilePaths.Count > 1 Then
        Dim concatList As New List(Of String)

        For Each filePath As String In audioFilePaths
            concatList.Add("file '" & filePath & "'")
        Next

        ' 创建临时的文本文件来存储文件列表
        Dim tempFile As String = Path.GetTempFileName()
        Using writer As New StreamWriter(tempFile)
            For Each line As String In concatList
                writer.WriteLine(line)
            Next
        End Using

        Dim parameters As String = "-f concat -safe 0 -i " & Chr(34) & tempFile & Chr(34) & " -c copy " & Chr(34) & txtSavePath.Text & Chr(34)

        RunFFMPEGProcess(parameters)
    Else
        MessageBox.Show("请选择至少两个音频文件进行拼接")
    End If

    MessageBox.Show("拼接完成!")



End Sub

End Class

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,由于引用[1]中提供的是vue-quill-editor.js的使用教程,而非与vb.net的集成方法,因此无法直接回答该问题。但是,我可以提供一些相关信息来帮助你更好地了解如何在vb.net中使用vue-quill-editor。 Vue-quill-editor是一个基于Quill.js的富文本编辑器,而Quill.js本身是一个纯JavaScript库,因此可以在任何支持JavaScript的环境中使用。在vb.net中使用Vue-quill-editor,你需要将其作为一个JavaScript库引入到你的项目中,并在你的代码中调用它的API来实现富文本编辑器的功能。 具体来说,你需要在你的vb.net项目中引入Vue.js和Vue-quill-editor.js这两个JavaScript库,并在你的代码中创建一个Vue实例来初始化Vue-quill-editor组件。以下是一个简单的示例代码: ```html <!-- 引入Vue.js和Vue-quill-editor.js --> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://cdn.jsdelivr.net/npm/vue-quill-editor@3.0.6/dist/vue-quill-editor.js"></script> <!-- 在HTML中添加一个容器元素,用于渲染Vue-quill-editor组件 --> <div id="editor"> <quill-editor></quill-editor> </div> <!-- 在JavaScript中创建Vue实例,并初始化Vue-quill-editor组件 --> <script> new Vue({ el: '#editor', components: { 'quill-editor': VueQuillEditor.default } }) </script> ``` 在上面的代码中,我们首先在HTML中添加了一个id为“editor”的容器元素,用于渲染Vue-quill-editor组件。然后,在JavaScript中创建了一个Vue实例,并将其绑定到“editor”元素上。最后,我们通过调用Vue-quill-editor组件的API来初始化富文本编辑器。 需要注意的是,上面的代码仅仅是一个简单的示例,实际使用中还需要根据具体的需求进行相应的配置和调整。同时,由于Vue-quill-editor是一个第三方库,其API和使用方法可能会随着版本的更新而发生变化,因此在使用时需要仔细查阅官方文档并进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值