Attribute VB_Name = "VideoDownloader"
Option Explicit
Public YouTubeDLPath As String
Public DownloadDir As String
Private FormatsListBuffer As String
Private FormatsListURL As String
Public BufferSizeValue As Integer
Public ComandosAdicionales As String
Public VlCertificado As Byte
Public DownloadURL As String
Public CalidadIndex As Byte
Public FormatoEspecificado As String
Public ItemsPlayList As String
Public HabilitarItemsPlayList As Byte
Public NombreArchivoSalida As String
Public EspecificarNombreArchivoSalida As Byte
Public HabilitarComandosAdicionales As Byte
Public IgnorarErrores As Byte

Public Sub InicializarVideoDownloader()
Dim FormsStatus() As Boolean, i As Integer, EnEjecucion As Boolean

ReDim FormsStatus(0 To Forms.Count - 1)
For i = 0 To Forms.Count - 1
FormsStatus(i) = Forms(i).Visible
Forms(i).Visible = False
Next

Load Form23
Form23.Show

EnEjecucion = True
While EnEjecucion
DoEvents
EnEjecucion = False
For i = 0 To Forms.Count - 1
If Forms(i) Is Form23 Then
EnEjecucion = True
Exit For
End If
Next
Wend

For i = 0 To Forms.Count - 1
Forms(i).Visible = FormsStatus(i)
Next
End Sub

Public Sub AcercaDeYoutubeDL()
MsgBox "youtube-dl is a command-line program to download videos from YouTube.com" & vbCrLf & _
       "and a few more sites. It requires the Python interpreter, version 2.6, 2.7," & vbCrLf & _
       "or 3.2+, and it is not platform specific. It should work on your Unix box," & vbCrLf & _
       "on Windows or on macOS. It is released to the public domain, which means" & vbCrLf & _
       "you can modify it, redistribute it or use it however you like.", vbOKOnly, "youtube-dl"
End Sub

Public Sub MostrarAyuda(FormObj As Form)
Dim Comando As String
If ScriptObj.ExisteArchivo(YouTubeDLPath) = False Then
    Call Lang.MsgyoutubedlNotFound
    Exit Sub
End If
Comando = Chr(34) & YouTubeDLPath & Chr(34) & " --help"
Call IntDocView.IniciarDocVisor(Comando, FormObj)
End Sub

Public Function ListarFormatosDisponibles() As String
Dim Comandos As String, CmdRet As String, i As Long
If FormatsListURL = Trim(LCase(Form23.Text1.Text)) And FormatsListBuffer <> "" Then
    ListarFormatosDisponibles = FormatsListBuffer
    Exit Function
End If
Comandos = Chr(34) & YouTubeDLPath & Chr(34)
If Form23.Opt_ValCert.Checked = False Then Comandos = Comandos & " --no-check-certificate"
Comandos = Comandos & " -F " & Chr(34) & Form23.Text1.Text & Chr(34)
If CaptureConsole.ExecAndCapture(Comandos, , , 1, , , , CmdRet) = False Then Exit Function
i = InStr(1, CmdRet, " Available formats ", vbTextCompare)
If i = 0 Then Exit Function
i = i + 2
i = InStr(i, CmdRet, vbCrLf, vbTextCompare)
If i = 0 Then Exit Function
i = i + 2
i = InStr(i, CmdRet, vbCrLf, vbTextCompare)
If i = 0 Then Exit Function
i = i + 2
FormatsListBuffer = Mid(CmdRet, i, Len(CmdRet) - i + 1)
FormatsListURL = Trim(LCase(Form23.Text1.Text))
ListarFormatosDisponibles = FormatsListBuffer
End Function

Public Sub RestaurarCuadroSelFormato()
With Form23
    .Command7.Visible = True
    .Command12.Visible = True
    .Label5.Visible = False
    .List1.Clear
    .List1.Visible = False
    .Label4.Visible = True
    .Label6.Visible = False
    .Command4.Visible = False
    .IconoCtrl6.Visible = False
    .Command5.Visible = False
    .IconoCtrl7.Visible = False
    .Check4.Visible = False
End With
End Sub

Public Function GenerarComandosYTDL(Optional SoloDarLink As Byte, Optional IgnorarBufferSize As Byte) As String
Dim Comandos As String, PlayListComando As String
If ScriptObj.ExisteArchivo(VideoDownloader.YouTubeDLPath) = False Then
    Lang.MsgyoutubedlNotFound
    Exit Function
End If
If Form23.Text1.Text = "" Then
    Lang.MsgURLNoEspecificada
    Exit Function
End If
If Form23.Combo1.ListIndex = Form23.Combo1.ListCount - 1 And Form23.Text3.Text = "" Then
    Lang.MsgYTdlNoEspecificoIDDeFormato
    Exit Function
End If
If SoloDarLink = 1 Then Comandos = " --skip-download --get-url"
If Form23.Check1.Value = 1 Then
    PlayListComando = Replace(Trim(Form23.Text5.Text), " ", "")
    If PlayListComando <> "" Then
        If Len(PlayListComando) > 1 Then
            If Right(PlayListComando, 1) = "-" Then
                PlayListComando = Left(PlayListComando, Len(PlayListComando) - 1)
                PlayListComando = "--playlist-start " & PlayListComando
            Else
                If Left(PlayListComando, 1) = "-" Then
                    PlayListComando = Right(PlayListComando, Len(PlayListComando) - 1)
                    PlayListComando = "--playlist-end " & PlayListComando
                Else
                    PlayListComando = "--playlist-items " & PlayListComando
                End If
            End If
        Else
            PlayListComando = "--playlist-items " & PlayListComando
        End If
        Comandos = Comandos & " " & PlayListComando
    End If
End If
If Form23.Opt_ValCert.Checked = False Then Comandos = Comandos & " --no-check-certificate"
If Form23.Opt_ErrIgn.Checked = True Then Comandos = Comandos & " -i"
If BufferSizeValue > 0 And SoloDarLink = 0 And IgnorarBufferSize = 0 Then Comandos = Comandos & " --buffer-size " & Trim(CStr(BufferSizeValue)) & "M"
Select Case Form23.Combo1.ListIndex
    Case 0: Comandos = Comandos & " "
    Case 1: Comandos = Comandos & " -f best "
    Case 2: Comandos = Comandos & " -f worst "
    Case 3: Comandos = Comandos & " -f bestvideo "
    Case 4: Comandos = Comandos & " -f worstvideo "
    Case 5: Comandos = Comandos & " -f bestaudio "
    Case 6: Comandos = Comandos & " -f worstaudio "
    Case 7: Comandos = Comandos & " -f bestvideo,bestaudio "
    Case 8: Comandos = Comandos & " -f bestvideo,worstaudio "
    Case 9: Comandos = Comandos & " -f worstvideo,bestaudio "
    Case 10: Comandos = Comandos & " -f worstvideo,worstaudio "
    Case 11: Comandos = Comandos & " --all-formats  "
    Case 12: Comandos = Comandos & " -f " & Form23.Text3.Text & " "
End Select
If ComandosAdicionales <> "" And Form23.Check3.Value = 1 Then Comandos = Comandos & Trim(ComandosAdicionales) & " "
GenerarComandosYTDL = Comandos
End Function

Public Function DetenerPlayer() As Boolean
Dim NumProcesos, i As Integer, Procesos() As Long, IdCmdExe As Long
IdCmdExe = CaptureConsole.IDProcesoPrincipal
If IdCmdExe = 0 Then Exit Function
NumProcesos = APIS.BuscarProcesosAsociadosARuta(ffmpeg.ffplayPath, Procesos)
For i = 1 To NumProcesos
    If APIS.GetParentProcessID(Procesos(i)) = IdCmdExe Then
        DetenerPlayer = APIS.MatarProcesoPorID(Procesos(i))
        Exit For
    End If
Next
End Function
