VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   8325
   ClientLeft      =   165
   ClientTop       =   810
   ClientWidth     =   13965
   LinkTopic       =   "Form1"
   ScaleHeight     =   8325
   ScaleWidth      =   13965
   StartUpPosition =   3  'Windows Default
   Begin VB.ListBox listAuto 
      Height          =   1815
      Left            =   9960
      TabIndex        =   32
      Top             =   6360
      Width           =   3735
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Full Auto"
      Height          =   195
      Left            =   10020
      TabIndex        =   31
      Top             =   6120
      Width           =   1155
   End
   Begin VB.ComboBox Combo1 
      Height          =   315
      Left            =   10560
      Style           =   2  'Dropdown List
      TabIndex        =   29
      Top             =   60
      Width           =   2955
   End
   Begin VB.ListBox lstAI 
      Height          =   1815
      Left            =   9900
      TabIndex        =   27
      Top             =   4140
      Width           =   3735
   End
   Begin VB.CommandButton cmdConvert 
      Caption         =   "AI Convert"
      Height          =   375
      Left            =   9960
      TabIndex        =   26
      Top             =   1680
      Width           =   1275
   End
   Begin VB.TextBox txtPy 
      Height          =   315
      Left            =   2280
      TabIndex        =   25
      Top             =   1680
      Width           =   7515
   End
   Begin VB.CommandButton cmdOutDir 
      Caption         =   "..."
      Height          =   315
      Left            =   7860
      TabIndex        =   23
      Top             =   360
      Width           =   735
   End
   Begin VB.TextBox txtOutDir 
      Height          =   285
      Left            =   600
      OLEDropMode     =   1  'Manual
      TabIndex        =   22
      Top             =   360
      Width           =   7095
   End
   Begin VB.FileListBox File1 
      Height          =   2235
      Left            =   11880
      TabIndex        =   18
      Top             =   1560
      Width           =   1575
   End
   Begin VB.TextBox txtOut 
      Height          =   4635
      Left            =   120
      MultiLine       =   -1  'True
      ScrollBars      =   3  'Both
      TabIndex        =   16
      Top             =   3540
      Width           =   9735
   End
   Begin VB.CommandButton Command5 
      Caption         =   "Reintegrate"
      Height          =   435
      Left            =   9960
      TabIndex        =   15
      Top             =   2880
      Width           =   1455
   End
   Begin VB.CommandButton Command4 
      Caption         =   "Rejoin Audio"
      Height          =   315
      Left            =   9900
      TabIndex        =   14
      Top             =   2280
      Width           =   1455
   End
   Begin VB.TextBox txtIntegrate 
      Height          =   315
      Left            =   1140
      TabIndex        =   13
      Text            =   "ffmpeg -i ""%1"" -i ""%ap\joined.mp4"" -c:v copy -map 0:v:0? -map 1:a:0 ""%2\%bn_AI.mp4"" -y"
      Top             =   2940
      Width           =   8595
   End
   Begin VB.TextBox txtConcat 
      Height          =   315
      Left            =   1080
      TabIndex        =   11
      Text            =   "ffmpeg -f concat -safe 0 -i ""%ap\concat_files.txt"" -c copy ""%ap\joined.mp4"" -y "
      Top             =   2340
      Width           =   8655
   End
   Begin VB.CommandButton Command3 
      Caption         =   "Split Audio"
      Height          =   315
      Left            =   9960
      TabIndex        =   8
      Top             =   1260
      Width           =   1335
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Extract Audio"
      Height          =   375
      Left            =   10020
      TabIndex        =   7
      Top             =   720
      Width           =   1215
   End
   Begin VB.TextBox txtSplit 
      Height          =   375
      Left            =   1020
      TabIndex        =   6
      Text            =   "ffmpeg -i ""%ap\raw.mp4"" -f segment -segment_time 299 -c copy ""%ap\parts\output%09d.mp4"""
      Top             =   1200
      Width           =   8775
   End
   Begin VB.TextBox txtExtractCmd 
      Height          =   315
      Left            =   1020
      TabIndex        =   4
      Text            =   "ffmpeg -i ""%1"" -vn -acodec copy ""%ap\raw.mp4"""
      Top             =   720
      Width           =   8775
   End
   Begin VB.CommandButton cmdBrowse 
      Caption         =   "..."
      Height          =   315
      Left            =   7860
      TabIndex        =   2
      Top             =   0
      Width           =   735
   End
   Begin VB.TextBox txtInput 
      Height          =   315
      Left            =   600
      OLEDropMode     =   1  'Manual
      TabIndex        =   1
      Text            =   "Can Drag & Drop"
      Top             =   0
      Width           =   7095
   End
   Begin VB.Label Label10 
      Caption         =   "Voice"
      Height          =   315
      Left            =   9900
      TabIndex        =   30
      Top             =   60
      Width           =   435
   End
   Begin VB.Label Label9 
      Caption         =   "Ai Conversion Progress"
      Height          =   255
      Left            =   10020
      TabIndex        =   28
      Top             =   3900
      Width           =   1695
   End
   Begin VB.Label lblDelOutput 
      Caption         =   "del output*"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   -1  'True
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   12420
      TabIndex        =   24
      Top             =   1320
      Width           =   855
   End
   Begin VB.Label Label8 
      Caption         =   "OutDir"
      Height          =   255
      Left            =   0
      TabIndex        =   21
      Top             =   420
      Width           =   615
   End
   Begin VB.Label lblOpenParts 
      Caption         =   "open"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   -1  'True
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   12420
      TabIndex        =   20
      Top             =   1080
      Width           =   855
   End
   Begin VB.Label lblrefresh 
      Caption         =   "refresh"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   -1  'True
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   12420
      TabIndex        =   19
      Top             =   840
      Width           =   555
   End
   Begin VB.Label Label7 
      Caption         =   "Parts"
      Height          =   255
      Left            =   11940
      TabIndex        =   17
      Top             =   840
      Width           =   1095
   End
   Begin VB.Label Label6 
      Caption         =   "reintegrate"
      Height          =   255
      Left            =   60
      TabIndex        =   12
      Top             =   3060
      Width           =   915
   End
   Begin VB.Label Label5 
      Caption         =   "rejoin cmd"
      Height          =   255
      Left            =   60
      TabIndex        =   10
      Top             =   2400
      Width           =   855
   End
   Begin VB.Label Label4 
      Caption         =   "ai translate audio python path:"
      Height          =   255
      Left            =   60
      TabIndex        =   9
      Top             =   1740
      Width           =   2535
   End
   Begin VB.Label Label3 
      Caption         =   "split cmd"
      Height          =   315
      Left            =   60
      TabIndex        =   5
      Top             =   1320
      Width           =   735
   End
   Begin VB.Label Label2 
      Caption         =   "Extract Cmd"
      Height          =   255
      Left            =   0
      TabIndex        =   3
      Top             =   780
      Width           =   915
   End
   Begin VB.Label Label1 
      Caption         =   "Input"
      Height          =   315
      Left            =   60
      TabIndex        =   0
      Top             =   60
      Width           =   555
   End
   Begin VB.Menu mnuTools 
      Caption         =   "Tools"
      Begin VB.Menu mnuConvertWMV 
         Caption         =   "Convert wmv to mp4"
      End
      Begin VB.Menu mnuSetApiKey 
         Caption         =   "Set elevenlabs_api_key env var"
      End
      Begin VB.Menu mnuVoiceSamples 
         Caption         =   "Voice Samples"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'I was lazy so this requires:
'  vbdevkit: http://sandsprite.com/tools.php?id=3


Option Explicit

Dim fso As New vbDevKit.CFileSystem2
Dim exe As String
Dim cmd As New vbDevKit.CCmdOutput
Dim dlg As New vbDevKit.CCmnDlg

Private Declare Function SHSetValue Lib "SHLWAPI.DLL" Alias "SHSetValueA" _
                                (ByVal hKey As Long, _
                                 ByVal pszSubKey As String, _
                                 ByVal pszValue As String, _
                                 ByVal dwType As Long, _
                                 pvData As String, _
                                 ByVal cbData As Long) As Long
                                 
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _
                                (ByVal hwnd As Long, _
                                 ByVal msg As Long, _
                                 ByVal wParam As Long, _
                                 ByVal lParam As String, _
                                 ByVal fuFlags As Long, _
                                 ByVal uTimeout As Long, _
                                 lpdwResult As Long) As Long
                                 
Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" _
                                (ByVal lpName As String, _
                                ByVal lpValue As String) As Long
Private Const REG_SZ = 1
Private Const REG_EXPAND_SZ = 2
Private Const HWND_BROADCAST = &HFFFF&
Private Const WM_WININICHANGE = &H1A
Private Const HKEY_CURRENT_USER = &H80000001
Private Const SHREGSET_FORCE_HKCU = &H1
Private Const SMTO_ABORTIFHUNG = &H2

Private Const SND_APPLICATION = &H80         '  look for application specific association
Private Const SND_ALIAS = &H10000     '  name is a WIN.INI [sounds] entry
Private Const SND_ALIAS_ID = &H110000    '  name is a WIN.INI [sounds] entry identifier
Private Const SND_ASYNC = &H1         '  play asynchronously
Private Const SND_FILENAME = &H20000     '  name is a file name
Private Const SND_LOOP = &H8         '  loop the sound until next sndPlaySound
Private Const SND_MEMORY = &H4         '  lpszSoundName points to a memory file
Private Const SND_NODEFAULT = &H2         '  silence not default, if sound not found
Private Const SND_NOSTOP = &H10        '  don't stop any currently playing sound
Private Const SND_NOWAIT = &H2000      '  don't wait if the driver is busy
Private Const SND_PURGE = &H40               '  purge non-static events for task
Private Const SND_RESOURCE = &H40004     '  name is a resource name or atom
Private Const SND_SYNC = &H0         '  play synchronously (default)
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Function lst(it)
    lstAI.AddItem Format(Now, "hh:mm:ss") & " " & it
    lstAI.Refresh
    DoEvents
End Function

Function lst2(it)
    listAuto.AddItem Format(Now, "hh:mm:ss") & " " & it
    listAuto.Refresh
    DoEvents
End Function

Function playwav(fpath)
    PlaySound fpath, ByVal 0&, SND_FILENAME Or SND_ASYNC
End Function

Sub SetEnvVar(EnvName, EnvValue, Optional setGlobal As Boolean = True)
    Dim resApi As Long
    
    ' Set Environment Variable for the current process and children
    SetEnvironmentVariable EnvName, EnvValue
    
    If setGlobal Then
        ' Set Environment Variable via the registry for all other processes
        resApi = SHSetValue(HKEY_CURRENT_USER, "Environment", CStr(EnvName), REG_EXPAND_SZ, ByVal CStr(EnvValue), CLng(LenB(StrConv(EnvValue, vbFromUnicode)) + 1))
        ' Make sure all other processes are aware of the Environment Variable Change
        SendMessageTimeout HWND_BROADCAST, WM_WININICHANGE, 0, "Environment", SMTO_ABORTIFHUNG, 5000, resApi
    End If
    
End Sub

Private Sub cmdBrowse_Click()
    On Error Resume Next
    Dim tmp As String
    tmp = dlg.OpenDialog()
    If Len(tmp) = 0 Then Exit Sub
    txtInput = tmp
    If Len(txtOutDir) = 0 And Not fso.FileExists(txtOutDir) Then
        txtOutDir = fso.GetParentFolder(txtInput)
    End If
End Sub

Private Sub cmdConvert_Click()
    
    If Not fso.FileExists(txtPy) Then
        MsgBox "Python.exe not found: " & txtPy
        Exit Sub
    End If
    
    Dim c() As String, cc, base As String, py As String, i As Long, out As String
    Dim voice_id As String
    
    lstAI.Clear
    base = App.Path & "\parts"
    py = App.Path & "\convert.py"
    voice_id = Trim(Split(Combo1.Text, ",")(1))
    
    c = fso.GetFolderFiles(base, "output*")
    
    If AryIsEmpty(c) Then
        MsgBox "No output files in \parts?"
        Exit Sub
    End If
    
    If Not fso.FileExists(py) Then
        MsgBox "convert.py not found?"
        Exit Sub
    End If
    
    lst (UBound(c) + 1) & " files found"
    
    'output%09d.mp4
    For Each cc In c
        lst "starting " & fso.GetBaseName(CStr(cc))
        i = Mid(fso.GetBaseName(CStr(cc)), 7)
        i = i + 1 'now 1 based
        out = base & "\" & i & ".mp4"
        If cmd.GetCommandOutput(txtPy, """" & py & """ """ & cc & """ """ & out & """ " & voice_id) Then
            If InStr(cmd.result, "OK") > 0 Then
                Kill cc
                lstAI.AddItem cmd.result
            Else
                txtOut = cmd.result
                lst "Error"
                Exit Sub
            End If
        Else
            txtOut = cmd.result
            lst "Error"
            Exit Sub
        End If
    Next
            
    lst "Complete!"
    playwav App.Path & "\complete.wav"

End Sub

Private Sub cmdOutDir_Click()
    Dim tmp As String
    
    If Len(txtOutDir) > 0 Then
        tmp = dlg.FolderDialog2(txtOutDir)
    Else
        tmp = dlg.FolderDialog2(fso.GetParentFolder(txtInput))
    End If
    
    If Len(tmp) > 0 Then txtOutDir.Text = tmp
    
End Sub



 
Private Sub Command1_Click()
    listAuto.Clear
    lst2 "Extracting"
    Command2_Click
    lst2 "Splitting"
    Command3_Click
    lst2 "AI Conversion"
    cmdConvert_Click
    lst2 "Joining"
    Command4_Click
    lst2 "Final output"
    Command5_Click
    playwav App.Path & "\complete.wav"
End Sub

Private Sub Command2_Click()
    Dim tmp As String
    
    If Not fso.FileExists(txtInput) Then
        MsgBox "no input"
        Exit Sub
    End If
    
    tmp = Replace(txtExtractCmd, "ffmpeg", "", 1, 1, vbTextCompare)
    tmp = Replace(tmp, "%1", txtInput)
    tmp = Replace(tmp, "%pf", fso.GetParentFolder(txtInput))
    tmp = Replace(tmp, "%ap", App.Path)
    tmp = Replace(tmp, "%2", txtOutDir)
     tmp = Replace(tmp, "%bn", fso.GetBaseName(txtInput))
    
    If fso.FileExists(App.Path & "\raw.mp4") Then Kill App.Path & "\raw.mp4"
    
    'cmd.LaunchProcess exe, tmp, False
    If cmd.GetCommandOutput(exe, tmp) Then
        txtOut = cmd.result
    Else
        txtOut = "Error: " & cmd.result
    End If
    
End Sub


Private Sub Command3_Click()
    
    Dim tmp As String, c() As String, cc
    
    If Not fso.FileExists(txtInput) Then
        MsgBox "no input"
        Exit Sub
    End If
    
    c = fso.GetFolderFiles(App.Path & "\parts")
    
    If Not AryIsEmpty(c) Then
        For Each cc In c
            Kill cc
        Next
    End If
    
    tmp = Replace(txtSplit, "ffmpeg", "", 1, 1, vbTextCompare)
    tmp = Replace(tmp, "%1", txtInput)
    tmp = Replace(tmp, "%pf", fso.GetParentFolder(txtInput))
    tmp = Replace(tmp, "%ap", App.Path)
    tmp = Replace(tmp, "%2", txtOutDir)
     tmp = Replace(tmp, "%bn", fso.GetBaseName(txtInput))
    
    If Not fso.FileExists(App.Path & "\raw.mp4") Then
        MsgBox "Could not find raw.mp4"
        Exit Sub
    End If
    
    If cmd.GetCommandOutput(exe, tmp) Then
        txtOut = cmd.result
    Else
        txtOut = "Error: " & cmd.result
    End If
    
    File1.Refresh
    Kill App.Path & "\raw.mp4"
    
End Sub

Private Sub Command4_Click()

    Dim tmp As String
    Dim f() As String
    
    If Not fso.FileExists(txtInput) Then
        MsgBox "no input"
        Exit Sub
    End If
    
    tmp = Replace(txtConcat, "ffmpeg", "", 1, 1, vbTextCompare)
    tmp = Replace(tmp, "%1", txtInput)
    tmp = Replace(tmp, "%pf", fso.GetParentFolder(txtInput))
    tmp = Replace(tmp, "%ap", App.Path)
    tmp = Replace(tmp, "%2", txtOutDir)
    tmp = Replace(tmp, "%bn", fso.GetBaseName(txtInput))
    
    Dim c() As String, cc
    
    c = fso.GetFolderFiles(App.Path & "\parts")
    If AryIsEmpty(c) Then
        MsgBox "No files in \parts?"
        Exit Sub
    End If
    
    For Each cc In c
        push f, "file '" & cc & "'"
    Next
    
    fso.WriteFile App.Path & "\concat_files.txt", Join(f, vbCrLf)
    
    If cmd.GetCommandOutput(exe, tmp) Then
        txtOut = cmd.result
    Else
        txtOut = "Error: " & cmd.result
    End If
    
    Kill App.Path & "\concat_files.txt"
    
End Sub

Private Sub Command5_Click()
    
    Dim tmp As String
    
    If Not fso.FileExists(txtInput) Then
        MsgBox "no input"
        Exit Sub
    End If
    
    tmp = Replace(txtIntegrate, "ffmpeg", "", 1, 1, vbTextCompare)
    tmp = Replace(tmp, "%1", txtInput)
    tmp = Replace(tmp, "%pf", fso.GetParentFolder(txtInput))
    tmp = Replace(tmp, "%bn", fso.GetBaseName(txtInput))
    tmp = Replace(tmp, "%ap", App.Path)
    tmp = Replace(tmp, "%2", txtOutDir)
    
    If Not fso.FileExists(App.Path & "\joined.mp4") Then
        MsgBox "Could not find joined.mp4"
        Exit Sub
    End If
    
    If cmd.GetCommandOutput(exe, tmp) Then
        txtOut = cmd.result
    Else
        txtOut = "Error: " & cmd.result
    End If
    
End Sub

Private Sub Form_Load()

    'playwav App.Path & "\complete.wav"
    'End
    
    cmd.hideWindow = False
    exe = App.Path & "\ffmpeg\bin\ffmpeg.exe"
    If Not fso.FileExists(exe) Then End

    Me.Caption = "exe ok: " & exe
    If Not fso.FolderExists(App.Path & "\parts") Then fso.CreateFolder App.Path & "\parts"
    File1.Path = App.Path & "\parts"
    txtOutDir = GetSetting("ffmpeg_gui", "settings", "outdir")
    
    Dim t() As String
    push t, "%1 = input file"
    push t, "%2 = output dir"
    push t, "%pf = input file parent folder"
    push t, "%ap = exe home directory"
    push t, "%bn = base name of input file"
    push t, "we expect ffmpeg in %ap\ffmpeg\bin\"
    txtOut = Join(t, vbCrLf)
    
    txtPy = getPythonPath

    Combo1.AddItem "Rachel, 21m00Tcm4TlvDq8ikWAM"
    Combo1.AddItem "Drew, 29vD33N1CtxCmqQRPOHJ"
    Combo1.AddItem "Clyde, 2EiwWnXFnvU5JabPnv8n"
    Combo1.AddItem "Paul, 5Q0t7uMcjvnagumLfvZi"
    Combo1.AddItem "Domi, AZnzlk1XvdvUeBnXmlld"
    Combo1.AddItem "Dave, CYw3kZ02Hs0563khs1Fj"
    Combo1.AddItem "Fin, D38z5RcWu1voky8WS1ja"
    Combo1.AddItem "Sarah, EXAVITQu4vr4xnSDxMaL"
    Combo1.AddItem "Antoni, ErXwobaYiN019PkySvjV"
    Combo1.AddItem "Thomas, GBv7mTt0atIp3Br8iCZE"
    Combo1.AddItem "Charlie, IKne3meq5aSn9XLyUdCD"
    Combo1.AddItem "George, JBFqnCBsd6RMkjVDRZzb"
    Combo1.AddItem "Emily, LcfcDJNUP1GQjkzn1xUU"
    Combo1.AddItem "Elli, MF3mGyEYCl7XYWbV9V6O"
    Combo1.AddItem "Callum, N2lVS1w4EtoT3dr4eOWO"
    Combo1.AddItem "Patrick, ODq5zmih8GrVes37Dizd"
    Combo1.AddItem "Harry, SOYHLrjzK2X1ezoPC6cr"
    Combo1.AddItem "Liam, TX3LPaxmHKxFdv7VOQHJ"
    Combo1.AddItem "Dorothy, ThT5KcBeYPX3keUQqHPh"
    Combo1.AddItem "Josh, TxGEqnHWrfWFTfGW9XjX"
    Combo1.AddItem "Arnold, VR6AewLTigWG4xSOukaG"
    Combo1.AddItem "Charlotte, XB0fDUnXU5powFXDhCwa"
    Combo1.AddItem "Alice, Xb7hH8MSUJpSbSDYk0k2"
    Combo1.AddItem "Matilda, XrExE9yKIg1WjnnlVkGX"
    Combo1.AddItem "James, ZQe5CZNOzWyzPSCn5a3c"
    Combo1.AddItem "Joseph, Zlb1dXrM653N07WRdFW3"
    Combo1.AddItem "Jeremy, bVMeCyTHy58xNoL34h3p"
    Combo1.AddItem "Michael, flq6f7yk4E4fJM5XTYuZ"
    Combo1.AddItem "Ethan, g5CIjZEefAph4nQFvHAz"
    Combo1.AddItem "Chris, iP95p4xoKVk53GoZ742B"
    Combo1.AddItem "Gigi, jBpfuIE2acCO8z3wKNLl"
    Combo1.AddItem "Freya, jsCqWAovK2LkecY7zXl4"
    Combo1.AddItem "Brian, nPczCjzI2devNBz1zQrb"
    Combo1.AddItem "Grace, oWAxZDx7w5VEj9dCyTzz"
    Combo1.AddItem "Daniel, onwK4e9ZLuTAKqWW03F9"
    Combo1.AddItem "Lily, pFZP5JQG7iQjIQuC4Bku"
    Combo1.AddItem "Serena, pMsXgVXv3BLzUgSXRplE"
    Combo1.AddItem "Adam, pNInz6obpgDQGcFmaJgB"
    Combo1.AddItem "Nicole, piTKgcLEGmPE4e6mEKli"
    Combo1.AddItem "Bill, pqHfZKP75CvOlQylNhV4"
    Combo1.AddItem "Jessie, t0jbNlBVZ17f02VDIeMI"
    Combo1.AddItem "Sam, yoZ06aMxZJJ28mfd3POQ"
    Combo1.AddItem "Glinda, z9fAnlkpzviPz146aGWa"
    Combo1.AddItem "Giovanni, zcAOhNBS3c14rBihAFp1"
    Combo1.AddItem "Mimi, zrHiDhphv9ZnVXBqCLjz"
    
    On Error Resume Next
    Dim ci As Long
    ci = GetSetting("ffmpeg_gui", "settings", "comboindex", 17)
    Combo1.ListIndex = ci
    
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim tmp As String, c() As String, cc
    
    SaveSetting "ffmpeg_gui", "settings", "comboindex", Combo1.ListIndex
    
    tmp = App.Path & "\joined.mp4"
    If Len(txtOutDir) > 0 Then SaveSetting "ffmpeg_gui", "settings", "outdir", txtOutDir
    If fso.FileExists(tmp) Then fso.DeleteFile tmp
    c = fso.GetFolderFiles(App.Path & "\parts")
    If Not AryIsEmpty(c) Then
        For Each cc In c
            fso.DeleteFile CStr(cc)
        Next
    End If
End Sub

Private Sub lblDelOutput_Click()
    On Error Resume Next
    Dim c() As String, cc
    c = fso.GetFolderFiles(App.Path & "\parts", "output*")
    If Not AryIsEmpty(c) Then
        For Each cc In c
            Kill cc
        Next
        File1.Refresh
    End If
End Sub

Private Sub lblOpenParts_Click()
    On Error Resume Next
    Shell "explorer.exe """ & App.Path & "\parts""", vbNormalFocus
End Sub

Private Sub lblrefresh_Click()
    On Error Resume Next
    File1.Refresh
End Sub

Private Sub mnuConvertWMV_Click()
    
    Dim wmv As String, mp4 As String, tmp As String

    wmv = dlg.OpenDialog()
    If Len(wmv) = 0 Then Exit Sub
    
    mp4 = dlg.SaveDialog(fso.GetBaseName(wmv) & ".mp4", fso.GetParentFolder(wmv))
    If Len(mp4) = 0 Then Exit Sub
    
    tmp = "ffmpeg -i """ & wmv & """ -c:v libx264 -crf 23 -c:a aac -q:a 100 """ & mp4 & """"
    tmp = Replace(tmp, "ffmpeg", "", 1, 1, vbTextCompare)
    
    If cmd.GetCommandOutput(exe, tmp) Then
        txtOut = cmd.result
        If fso.FileExists(mp4) Then
            txtOut = txtOut & vbCrLf & "FileSize: " & FileLen(mp4)
            txtInput = mp4
        End If
    Else
        txtOut = "Error: " & cmd.result
    End If
    
End Sub



Sub push(ary, value) 'this modifies parent ary object
    On Error GoTo init
    Dim x
    x = UBound(ary) '<-throws Error If Not initalized
    ReDim Preserve ary(UBound(ary) + 1)
    ary(UBound(ary)) = value
    Exit Sub
init:     ReDim ary(0): ary(0) = value
End Sub

Function AryIsEmpty(ary) As Boolean
  On Error GoTo oops
  Dim x
    If IsObject(UBound(ary)) Then AryIsEmpty = False
    'x = UBound(ary)
  Exit Function
oops: AryIsEmpty = True
End Function

 
Private Sub mnuSetApiKey_Click()
    Dim apikey As String
    apikey = InputBox("Paste in your elevenlabs api key")
    If Len(apikey) = 0 Then Exit Sub
    SetEnvVar "elevenlabs_api_key", apikey
End Sub

Private Sub mnuVoiceSamples_Click()
    On Error Resume Next
    Dim tmp As String, pid As Long
    tmp = App.Path & "\voices.exe"
    
    If fso.FileExists(tmp) Then
        pid = Shell(tmp) 'do they have mscomctl32.ocx installed?
    End If
    
    If pid = 0 Then
        tmp = App.Path & "\voices\samples"
        If fso.FolderExists(tmp) Then
            Shell "explorer.exe """ & tmp & """", vbNormalFocus
        End If
    End If
        
End Sub

Private Sub txtInput_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, Y As Single)
    On Error Resume Next
    txtInput = Data.Files(1)
End Sub

Private Sub txtOutDir_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, Y As Single)
    On Error Resume Next
    txtOutDir = Data.Files(1)
End Sub

Function getPythonPath()
    Dim exe As String, x() As String, i, p, a, p2 As String
    
    exe = Environ("windir") & "\py.exe"
    'Installed Pythons found by py Launcher for Windows
    '-3.9-64        C:\Users\dzzie\AppData\Local\Programs\Python\Python39\python.exe *

    If fso.FileExists(exe) Then
        If cmd.GetCommandOutput(exe, "-0p") Then
            x = Split(cmd.result, vbLf)
            If Not AryIsEmpty(x) Then
                If UBound(x) > 1 Then
                    For i = 0 To UBound(x) 'skip first line
                        p = x(i)
                        If InStr(1, p, "Python3", vbTextCompare) > 0 Or InStr(1, p, "Py3", vbTextCompare) > 0 Then
                            a = InStr(p, ":\")
                            If a > 0 Then
                                p2 = Trim(Mid(p, a - 1))
                                p2 = Replace(p2, vbCr, Empty)
                                p2 = Trim(Replace(p2, vbLf, Empty))
                                If fso.FileExists(p2) Then
                                    getPythonPath = p2
                                    Exit Function
                                End If
                            End If
                        End If
                    Next
                End If
            End If
        End If
    End If
    
End Function
