首页 > 其他分享 >VS2010旗舰版VB.NET版本音频剪辑代码2024-8-10

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

时间:2024-08-10 16:24:27浏览次数:14  
标签:10 VB End Sub Dim ByVal VS2010 Private Integer

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

标签:10,VB,End,Sub,Dim,ByVal,VS2010,Private,Integer
From: https://blog.csdn.net/qq_32257509/article/details/141090247

相关文章

  • 10分钟学会Docker的安装和使用
    前言在现代软件开发中,Docker作为一种轻量级的容器化技术,已成为开发者必备的工具之一。本文将带您在10分钟内快速掌握Docker的安装和基本使用方法。1.Docker是什么?Docker是一种开源的容器化平台,它通过将应用程序及其所有依赖项打包到一个标准化的单元中,使应用程序能够在......
  • P1091 [NOIP2004 提高组] 合唱队形
    这道题主要考察的是线性dp,最基础的dp,这道题的主要思路1.求出最大子序列,2.求出最小子序列,3.找出最少要多少个人要出列。其实我们主要2可以变成逆序查找最大子序列,所以我们只需要把前两个找出来之后我们就可以求出主要3(注意一定要减1,因为中间的那个同学一定会被多算一次所以必......
  • Github 2024-08-10开源项目日报 Top10
    根据GithubTrendings的统计,今日(2024-08-10统计)共有10个项目上榜。根据开发语言中项目的数量,汇总情况如下:开发语言项目数量Python项目4JavaScript项目2TypeScript项目2C#项目1Dockerfile项目1RichTextFormat项目1Swift项目1Jupyt......
  • 0810day03
    1.简述python垃圾回收机制就是对于之前占用的内存空间,比如一开始x的值为10,后面改为了11,那之前定义占用的空间(被视为垃圾)就会被自动删除,这也是与其他语言不一样的点,其他的语言可能还需要自己去删,python会自动删去不需要的内存占用2.对于下述代码:x=10y=10z=10dely10......
  • 2024/08/10 每日一题
    LeetCode2940找到Alice和Bob可以相遇的建筑方法1:线段树classSolution{staticint[]tree;//存储区间的最大值staticvoidbuild(into,intleft,intright,int[]datas){if(left==right){tree[o]=datas[left-1];......
  • 8.10第四周周六学习总结
    1vj团队12补题不错的一个题解https://blog.fishze.com/archives/3011)字符串变化(模拟+找规律)题目:给定一个字符串,给定一个特定操作方式:该字符串前半段+该字符串自己+该字符串后半段求next(每一个字符向后移动一个),组成一个新字符串,求经过10^100次这样的操作后,......
  • AP2402 5-100V 1.5A 外围简单DC-DC降压恒流驱动IC 手电筒与汽车灯方案
    产品描述AP2402是一款PWM工作模式,高效率、外围简单、内置功率管,适用于5-100V输入的高精度降压LED恒流驱动芯片。输出最大功率可达15W,最大电流1.5A。AP2402可实现三段功能切换,通过MODE1/2/3切换三种功能模式:全亮,半亮,爆闪。AP2402工作频率固定在150KHZ左右,同时内置抖......
  • 如何在 Windows 11/10/8/7 中恢复已删除和未保存的记事本文本文件
    很多原因都会导致未保存的记事本文本文件丢失。这些包括意外关闭、系统崩溃或电源故障等。无论丢失文本文件的原因是什么,相关的焦虑都是一样的。如果您遇到这种情况,可以使用以下有效方法在Windows11/10/8/7 中恢复已删除的文本文件。在这篇文章中,我们将分享三种在Windows......
  • 下载量10w+!大型语言模型:语言理解和生成
    近年来,人工智能在新语言能力方面取得了显著进展,深度学习技术的快速发展推动了语言AI系统在文本编写和理解方面的表现。免费获取:下载量10w+!大型语言模型:语言理解和生成......
  • 10 个令人惊叹的 Python 自动化脚本!
    /01/剪贴板管理器你是否曾发现自己忙于处理多个文本片段,而忘记了自己复制了什么?有没有想过有一个工具可以记录你一天中复制的所有内容?这个自动化脚本可以监控你复制的所有内容,将复制的每个文本无缝地存储在一个时尚的图形界面中,这样你就不必在无尽的标签页中搜索,也不会丢失......