Skip to content

Commit 7d18a5e

Browse files
committed
v1.13.0: Ffmpeg and Opusenc piping! No more temporary files when encoding are created.
1 parent 5522afc commit 7d18a5e

4 files changed

Lines changed: 91 additions & 51 deletions

File tree

opus_gui/Form1.Designer.vb

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

opus_gui/Form1.vb

Lines changed: 83 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Public Class Form1
1+
Imports System.IO.Pipes
2+
3+
Public Class Form1
24
Private opusenc_version As String = String.Empty
35
Private ffmpeg_version As String = String.Empty
46
Public Running As Boolean = False
@@ -53,7 +55,7 @@
5355
If Not String.IsNullOrEmpty(OutputFolder) Then
5456
outputPath = OutputTxt.Text + "\" + IO.Path.GetFileNameWithoutExtension(Item) + ".opus"
5557
Else
56-
outputPath = IO.Path.GetDirectoryName(Item) + "\" + IO.Path.GetFileNameWithoutExtension(Item) + ".opus"
58+
outputPath = IO.Path.ChangeExtension(Item, ".opus")
5759
End If
5860
Return outputPath
5961
End Function
@@ -64,30 +66,24 @@
6466
Dim FileAlreadyExist As List(Of String) = New List(Of String)
6567
Dim ErrorList As List(Of String) = New List(Of String)
6668
Dim IgnoreFilesWithExtensions As String = String.Empty
69+
Dim Item_Type As Integer = 0
6770
If IO.File.Exists("ignore.txt") Then IgnoreFilesWithExtensions = My.Computer.FileSystem.ReadAllText("ignore.txt")
6871
If IO.Directory.Exists(InputTxt.Text) Or GoogleDrive Then
6972
Dim Items As Object
7073
If Not GoogleDrive Then
74+
Item_Type = 0
7175
Items = IO.Directory.GetFiles(InputTxt.Text)
7276
Else
77+
Item_Type = 1
7378
Items = GDriveItemsToProcess
74-
ItemsToDelete = GDriveItemsToProcess
7579
End If
7680
For Each File In Items
77-
If (IO.Path.GetExtension(File) = ".wav" Or IO.Path.GetExtension(File) = ".flac" Or IO.Path.GetExtension(File) = ".opus" And EncOpusenc.Checked) Or EncFfmpeg1.Checked Or EncFFmpeg2.Checked Then
78-
ItemsToProcess.Add(File)
79-
ElseIf IO.Path.GetExtension(File) = ".mp3" Or IO.Path.GetExtension(File) = ".m4a" And EncOpusenc.Checked Then
80-
If Not ffmpeg_version = String.Empty Then
81-
ffmpeg_preprocess(File, IO.Path.GetFileNameWithoutExtension(File))
82-
ItemsToProcess.Add(IO.Path.GetFileNameWithoutExtension(File) + ".flac")
83-
ItemsToDelete.Add(IO.Path.GetFileNameWithoutExtension(File) + ".flac")
84-
Else
85-
ErrorList.Add(File)
86-
End If
87-
Else
88-
If Not String.IsNullOrEmpty(OutputTxt.Text) Then
89-
If Not IO.File.Exists(OutputTxt.Text + "\" + My.Computer.FileSystem.GetName(File)) Then
90-
If Not IgnoreFilesWithExtensions.Contains(IO.Path.GetExtension(File)) Then My.Computer.FileSystem.CopyFile(File, OutputTxt.Text + "\" + My.Computer.FileSystem.GetName(File))
81+
If Not String.IsNullOrEmpty(OutputTxt.Text) Then
82+
If Not IO.File.Exists(OutputTxt.Text + "\" + My.Computer.FileSystem.GetName(File)) Then
83+
If Not IgnoreFilesWithExtensions.Contains(IO.Path.GetExtension(File)) Then
84+
ItemsToProcess.Add(File)
85+
Else
86+
My.Computer.FileSystem.CopyFile(File, OutputTxt.Text + "\" + My.Computer.FileSystem.GetName(File))
9187
End If
9288
End If
9389
End If
@@ -96,25 +92,18 @@
9692
ItemsToProcess.Add(InputTxt.Text)
9793
End If
9894
ProgressBar1.BeginInvoke(Sub()
99-
If Not GoogleDrive Then
100-
ProgressBar1.Maximum = ItemsToProcess.Count
101-
Else
102-
ProgressBar1.Maximum = ItemsToProcess.Count * 2
103-
End If
95+
ProgressBar1.Maximum = ItemsToProcess.Count
10496
ProgressBar1.Value = 0
10597
End Sub)
106-
If GoogleDrive Then
107-
Dim download_tasks = New List(Of Action)
108-
For Counter As Integer = 0 To GDriveItemIDs.Count - 1
109-
Dim i As Integer = Counter
110-
download_tasks.Add(Function() Download_Files(GDriveItemsToProcess(i), GDriveItemIDs(i)))
111-
Next
112-
Parallel.Invoke(New ParallelOptions With {.MaxDegreeOfParallelism = My.Settings.MaxDriveDownloads}, download_tasks.ToArray())
113-
End If
11498
Dim tasks = New List(Of Action)
11599
If enableMultithreading.Checked Then
116100
For Counter As Integer = 0 To ItemsToProcess.Count - 1
117-
Dim args As Array = {ItemsToProcess(Counter), GetOutputPath(OutputTxt.Text, ItemsToProcess(Counter)), My.Settings.Bitrate}
101+
Dim args As Array
102+
If GoogleDrive Then
103+
args = {GDriveItemIDs(Counter), 1, GetOutputPath(OutputTxt.Text, ItemsToProcess(Counter)), IO.Path.GetExtension(ItemsToProcess(Counter)), My.Settings.Bitrate}
104+
Else
105+
args = {ItemsToProcess(Counter), 0, GetOutputPath(OutputTxt.Text, ItemsToProcess(Counter)), IO.Path.GetExtension(ItemsToProcess(Counter)), My.Settings.Bitrate}
106+
End If
118107
If Not IO.File.Exists(args(1)) Then
119108
If EncOpusenc.Checked Then
120109
tasks.Add(Function() Run_opus(args, "opusenc", "opusenc"))
@@ -175,50 +164,97 @@
175164
End If
176165
MsgBox(MessageToShow)
177166
End Sub
178-
Private Function Download_Files(Filename As String, Id As String)
179-
Dim fileStream As New IO.FileStream(Filename, IO.FileMode.Create)
180-
GoogleDriveForm.drive.DownloadFile(Id, fileStream)
181-
fileStream.Close()
182-
ProgressBar1.BeginInvoke(Sub() ProgressBar1.PerformStep())
183-
Return True
167+
Private Function Download_Files(id As String) As IO.MemoryStream
168+
Using memoryStream As New IO.MemoryStream
169+
GoogleDriveForm.drive.DownloadFile(id, memoryStream)
170+
Return memoryStream
171+
End Using
184172
End Function
185-
Private Function Run_opus(args As Array, encoder As String, encoderExe As String)
173+
Private Function Run_opus(args As Array, encoder As String, encoderExe As String) As Boolean
186174
Dim Input_File As String = args(0)
187-
Dim Output_File As String = args(1)
188-
Dim Bitrate As String = args(2)
175+
Dim Input_Type As Integer = args(1)
176+
Dim Output_File As String = args(2)
177+
Dim FileExtension As String = args(3)
178+
Dim Bitrate As String = args(4)
189179
Dim opusProcessInfo As New ProcessStartInfo
190180
Dim opusProcess As Process
191181
opusProcessInfo.FileName = encoderExe + ".exe"
182+
Dim Data As Byte()
183+
If Input_Type = 0 Then
184+
Data = IO.File.ReadAllBytes(Input_File)
185+
Else
186+
Data = Download_Files(Input_File).ToArray()
187+
End If
192188
Select Case encoder
193189
Case "opusenc"
194-
opusProcessInfo.Arguments = "--music --bitrate " & Bitrate & " """ + Input_File + """ """ + Output_File + """"
190+
If Not (FileExtension = ".wav" Or FileExtension = ".flac" Or FileExtension = ".opus") Then
191+
If Not ffmpeg_version = String.Empty Then
192+
Data = ffmpeg_preprocess(Data)
193+
Else
194+
Return False
195+
End If
196+
End If
197+
opusProcessInfo.Arguments = "--music --bitrate " & Bitrate & " - """ + Output_File + """"
195198
Case "ffmpeg1"
196-
opusProcessInfo.Arguments = "-i """ + Input_File + """ -c:a libopus -application audio -b:a " & Bitrate & "K """ + Output_File + """"
199+
opusProcessInfo.Arguments = "-i - -c:a libopus -application audio -b:a " & Bitrate & "K -c:v copy """ + Output_File + """"
197200
Case "ffmpeg2"
198-
opusProcessInfo.Arguments = "-i """ + Input_File + """ -c:a opus -strict -2 -b:a " & Bitrate & "K """ + Output_File + """"
201+
opusProcessInfo.Arguments = "-i - -c:a opus -strict -2 -b:a " & Bitrate & "K -c:v copy """ + Output_File + """"
199202
End Select
200203
opusProcessInfo.WorkingDirectory = IO.Path.GetDirectoryName(Input_File)
201204
opusProcessInfo.CreateNoWindow = True
202205
opusProcessInfo.RedirectStandardOutput = False
206+
opusProcessInfo.RedirectStandardInput = True
203207
opusProcessInfo.UseShellExecute = False
204208
opusProcess = Process.Start(opusProcessInfo)
209+
Dim ffmpegIn As IO.Stream = opusProcess.StandardInput.BaseStream
210+
ffmpegIn.Write(Data, 0, Data.Length)
211+
ffmpegIn.Flush()
212+
ffmpegIn.Close()
205213
opusProcess.WaitForExit()
206214
ProgressBar1.BeginInvoke(Sub() ProgressBar1.PerformStep())
207215
Return True
208216
End Function
209-
Private Function ffmpeg_preprocess(Input As String, Output As String)
217+
Private Function ffmpeg_preprocess(Input As Byte()) As Byte()
218+
Dim InputPipe As New NamedPipeServerStream("ffin", PipeDirection.Out, -1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 16384, 0)
219+
Dim OutputPipe As New NamedPipeServerStream("ffout.flac", PipeDirection.In, -1, PipeTransmissionMode.Byte, PipeOptions.WriteThrough, 0, 16384)
210220
Dim ffmpegProcessInfo As New ProcessStartInfo
211221
Dim ffmpegProcess As Process
212222
ffmpegProcessInfo.FileName = "ffmpeg.exe"
213-
ffmpegProcessInfo.Arguments = "-i """ + Input + """ -c:a flac """ + Output + ".flac"" -y"
223+
ffmpegProcessInfo.Arguments = "-i \\.\pipe\ffin -c:a flac -c:v copy \\.\pipe\ffout.flac -y"
214224
ffmpegProcessInfo.CreateNoWindow = True
215-
ffmpegProcessInfo.RedirectStandardOutput = False
225+
ffmpegProcessInfo.RedirectStandardInput = True
226+
ffmpegProcessInfo.RedirectStandardOutput = True
216227
ffmpegProcessInfo.UseShellExecute = False
217228
ffmpegProcess = Process.Start(ffmpegProcessInfo)
229+
WriteByteAsync(InputPipe, Input)
230+
Dim lastRead As Integer
231+
OutputPipe.WaitForConnection()
232+
Dim PipedOutput As Byte()
233+
Using ms As New IO.MemoryStream
234+
Dim buffer As Byte() = New Byte(16384) {}
235+
Do
236+
lastRead = OutputPipe.Read(buffer, 0, 16384)
237+
ms.Write(buffer, 0, lastRead)
238+
Loop While lastRead > 0
239+
PipedOutput = ms.ToArray()
240+
OutputPipe.Close()
241+
End Using
218242
ffmpegProcess.WaitForExit()
219-
Return True
243+
Return PipedOutput
220244
End Function
245+
Private Async Sub WriteByteAsync(InputPipe As NamedPipeServerStream, Input As Byte())
246+
InputPipe.WaitForConnection()
247+
Dim ChunkSize As Integer = 16384
248+
For Bytes As Long = 0 To Input.Length Step 16384
249+
If Input.Length - Bytes < ChunkSize Then
250+
ChunkSize = Input.Length - Bytes
251+
End If
252+
Await InputPipe.WriteAsync(Input, Bytes, ChunkSize)
253+
Next
221254

255+
InputPipe.Flush()
256+
InputPipe.Dispose()
257+
End Sub
222258
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
223259
BitrateNumberBox.Value = My.Settings.Bitrate
224260
enableMultithreading.Checked = My.Settings.Multithreading

opus_gui/GoogleDriveClass.vb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ Public Class GoogleDriveClass
115115
End Sub
116116

117117
Public Sub DownloadFile(ByVal Id As String, ByVal stream As FileStream)
118-
Dim getRequest As FilesResource.GetRequest = service.Files.[Get](Id)
118+
Dim getRequest As FilesResource.GetRequest = service.Files.Get(Id)
119+
getRequest.Download(stream)
120+
End Sub
121+
Public Sub DownloadFile(ByVal Id As String, ByVal stream As MemoryStream)
122+
Dim getRequest As FilesResource.GetRequest = service.Files.Get(Id)
119123
getRequest.Download(stream)
120124
End Sub
121125
End Class

opus_gui/My Project/AssemblyInfo.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
3131
' by using the '*' as shown below:
3232
' <Assembly: AssemblyVersion("1.0.*")>
3333

34-
<Assembly: AssemblyVersion("1.12.9.0")>
35-
<Assembly: AssemblyFileVersion("1.12.9.0")>
34+
<Assembly: AssemblyVersion("1.13.0.0")>
35+
<Assembly: AssemblyFileVersion("1.13.0.0")>

0 commit comments

Comments
 (0)