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