Attribute VB_Name = "Grabador"
Option Explicit
Private Type ConfigGDIGrab
    Opcion As Byte
    Ancho As String
    Alto As String
    x As String
    y As String
    Titulo As String
    NoCapturarMouse As Byte
    fpsOpt As Byte
    fpsVal As String
End Type

Public OutFilePath As String
Public Grabando As Byte
Public realTBuffSize As String
Public GrabacionDetenida As Byte
Public FormatoGrabacion As String
Private ConfiguracionScreenCapture As ConfigGDIGrab

Public Sub IncializarGrabador(Optional Direccion As String)
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 Form14
Form14.Show
Form14.Menu_Options.Enabled = False
Call ListarFiltrosDirectShow
Form14.Frame3.Visible = False
Form14.Menu_Options.Enabled = True

If Direccion <> "" Then
    With Form14
        .Option1.Value = True
        .Text1.Text = Direccion
    End With
End If

EnEjecucion = True
While EnEjecucion
DoEvents
EnEjecucion = False
For i = 0 To Forms.Count - 1
If Forms(i) Is Form14 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 ListarFiltrosDirectShow()
Dim Comandos As String, BufferConsola As String
Dim ADevices() As String, VDevices() As String
Dim Posicion1 As String, Posicion2 As String
Dim i As Integer
Dim FiltrosDirectShowA() As String
Dim FiltrosDirectShowV() As String

Form14.Combo1.Clear
Form14.Combo2.Clear

Form14.Combo2.AddItem "<" & Lang.GrabarPantallaString & ">"

Comandos = Chr(34) & ffmpeg.ffmpegPath & Chr(34) & " -list_devices true " & ffmpeg.FFmpegParametros.Contenedor & " dshow -i dummy"
Call CaptureConsole.ExecAndCapture(Comandos, , , , , , , BufferConsola)
If BufferConsola = "" Then Exit Sub

On Error Resume Next

Posicion1 = InStr(1, BufferConsola, "] DirectShow video devices", vbTextCompare)
Posicion2 = InStr(1, BufferConsola, "] DirectShow audio devices", vbTextCompare)
If Posicion1 = 0 And Posicion2 = 0 Then Exit Sub

If Posicion1 > 0 Then
If Posicion1 > Posicion2 Then VDevices = Split(Right(BufferConsola, Len(BufferConsola) - Posicion1), vbCrLf)
If Posicion1 < Posicion2 Then VDevices = Split(Mid(BufferConsola, Posicion1, Posicion2 - Posicion1), vbCrLf)
End If
If Posicion2 > 0 Then
If Posicion2 > Posicion1 Then ADevices = Split(Right(BufferConsola, Len(BufferConsola) - Posicion2), vbCrLf)
If Posicion2 < Posicion1 Then ADevices = Split(Mid(BufferConsola, Posicion2, Posicion1 - Posicion2), vbCrLf)
End If
BufferConsola = ""

If Len(Join(VDevices, ":")) > 0 Then
For i = LBound(VDevices) To UBound(VDevices)
Posicion1 = InStr(1, VDevices(i), Chr(34))
If Posicion1 > 0 Then
Posicion1 = Posicion1 + 1
Posicion2 = InStr(Posicion1, VDevices(i), Chr(34))
If Posicion2 > 0 Then
Form14.Combo2.AddItem Mid(VDevices(i), Posicion1, Posicion2 - Posicion1)
End If
End If
Next
End If

If Len(Join(ADevices, ":")) > 0 Then
For i = LBound(ADevices) To UBound(ADevices)
Posicion1 = InStr(1, ADevices(i), Chr(34))
If Posicion1 > 0 Then
Posicion1 = Posicion1 + 1
Posicion2 = InStr(Posicion1, ADevices(i), Chr(34))
If Posicion2 > 0 Then
Form14.Combo1.AddItem Mid(ADevices(i), Posicion1, Posicion2 - Posicion1)
End If
End If
Next
End If
End Sub

Public Sub EmpezarRecord()
Dim NumArchivo As Integer, BufError As Byte
Dim LineaCmd As Convertidor.FormatoCmdL, Comandos As String
Dim EntradaCmd As String, BufferConsola As String, BufferError As Long
Dim Duracion As String, RutaSalida As String
If Form14.Option3.Value = True And OutFilePath = "" Then
Lang.Convert_EspecifiqueSalida
Exit Sub
End If
If ffmpeg.ComprobarExistenciaFFmpeg = False Then Exit Sub
If Form14.Check1.Value = 1 And ScriptObj.ExisteArchivo(ffmpeg.ffplayPath) = False Then
Lang.MsgFileNotFound (ffmpeg.ffplayPath)
Exit Sub
End If
If Form14.Option3.Value = True And Form14.Combo3.ListIndex = 0 And ScriptObj.DevolverExtension(OutFilePath) = "" Then
Lang.Convert_Error_GenerarCmdL_OutExt
Exit Sub
End If
If Form14.Option2.Value = True And Form14.Option3.Value = True And Form14.Text11.Text <> "" Then
Duracion = ffmpeg.FFConvertirCronometroASegungos(Form14.Text11.Text)
If Duracion = "" Then
Lang.Grabador_Msg_DuracionNoValida
Exit Sub
End If
Duracion = ffmpeg.FFmpegParametros.Duracion & " " & Duracion
End If
EntradaCmd = GrabGenerarComandosEntrada(1)
If EntradaCmd = "" Then
Lang.Grabador_Msg_OrigenNoEspecificado
Exit Sub
End If

If Form14.Option3.Value = True Then
If FormatoGrabacion <> "" Then
LineaCmd = Convertidor.InterpretarPFormato(FormatoGrabacion, BufError)
If BufError = 1 Then
Lang.Convert_Error_GenerarCmdL_FormatoNotFound (FormatoGrabacion)
Exit Sub
End If
If BufError = 2 Then
Lang.Convert_Error_GenerarCmdL_LeerFmt
Exit Sub
End If
If LineaCmd.ECANombre <> "" Or LineaCmd.ECVNombre <> "" Then
Lang.Grabador_Msg_FormatoNoValido
Exit Sub
End If
End If

RutaSalida = OutFilePath
If ScriptObj.DevolverExtension(RutaSalida) = "" Then RutaSalida = RutaSalida & "." & LineaCmd.OutExtension
RutaSalida = ScriptObj.ResolverConflictoDeNombres(RutaSalida)

Comandos = Chr(34) & ffmpeg.ffmpegPath & Chr(34) & " " & LineaCmd.InCmdL & " " & EntradaCmd & " " & Duracion & " " & LineaCmd.OutCmdL & " " & LineaCmd.CmdLA & " " & LineaCmd.CmdLS & " " & LineaCmd.CmdLV & " " & LineaCmd.RelAspect & " " & LineaCmd.TamanoVideo & " " & ffmpeg.FFmpegParametros.SobreescribirYes & " " & LineaCmd.EliminarA & " " & LineaCmd.EliminarS & " " & LineaCmd.EliminarV & " "
Comandos = Comandos & " " & Chr(34) & RutaSalida & Chr(34)
Form14.Frame4.Visible = True
Form14.Frame1.Visible = False
Form14.Frame2.Visible = False
Form14.Height = 2400
Form14.Text9.Text = ""
Form14.Command4.Enabled = True
Form14.Command9.Enabled = True
Form14.Command8.Enabled = True
Form14.Menu_Options.Enabled = False
If Form14.Check1.Value = 1 Then CapturarYVerEnFFPlay (1)
Grabando = 1
If CaptureConsole.ExecAndCapture(Comandos, , , 1, Form14.Text9, False, 3, BufferConsola, 256, BufferError) = False Then
MsgBox Lang.Convert_Str_CantIniProc, vbCritical, Form1.Caption
Else
If BufferError > 0 And GrabacionDetenida = 0 Then Lang.Grabador_Msg_ffmpegerror (BufferConsola)
End If
Grabando = 0
Form14.Command4.Enabled = False
Form14.Command9.Enabled = False
Form14.Command8.Enabled = False
Form14.Text9.Text = Lang.Grabador_Str_Terminado
GrabacionDetenida = 0
End If

If Form14.Option4.Value = True Then Call Emisor.InicializarEmisor(EntradaCmd)
End Sub

Public Sub CapturarYVerEnFFPlay(Optional Asincronico As Byte = 0)
Dim CmdInDevice As String, Comandos As String
If ffmpeg.ComprobarExistenciaFFplay = False Then Exit Sub
CmdInDevice = GrabGenerarComandosEntrada
If CmdInDevice = "" Then
Lang.Grabador_Msg_OrigenNoEspecificado
Exit Sub
End If
Comandos = ffmpeg.FFmpegParametros.ffplayWidthDisp & " 640 " & ffmpeg.FFmpegParametros.ffplayHeightDisp & " 480 " & CmdInDevice
If Asincronico = 0 Then
Call ffmpeg.PlayFile("", Comandos)
Else
Shell Chr(34) & ffmpeg.ffplayPath & Chr(34) & " " & Comandos, vbNormalNoFocus
End If
End Sub

Private Function GrabGenerarComandosEntrada(Optional CambiarBufSize As Byte = 0) As String
Dim Comandos As String, OpcA As String, OpcV As String, BufferOption As String
With Form14
If .Option1.Value = True And .Text1.Text <> "" Then Comandos = "-i " & .Text1.Text
If .Option2.Value = True Then
    If .Check2.Value = 1 And .Combo1.Text <> "" Then OpcA = "audio=" & Chr(34) & .Combo1.Text & Chr(34)
    If .Check3.Value = 1 And .Combo2.Text <> "" Then
        If .Combo2.Text <> "<" & Lang.GrabarPantallaString & ">" Then
            OpcV = "video=" & Chr(34) & .Combo2.Text & Chr(34)
            If OpcA <> "" Then OpcV = ":" & OpcV
        Else
            OpcV = GenerarComandosGdiGrab
        End If
    End If
    If OpcA = "" And OpcV = "" Then Exit Function
    If realTBuffSize <> "" And realTBuffSize <> "0" And CambiarBufSize = 1 Then BufferOption = "-rtbufsize " & realTBuffSize & "M"
    If OpcA <> "" Or (OpcV <> "" And .Combo2.Text <> "<" & Lang.GrabarPantallaString & ">") Then Comandos = ffmpeg.FFmpegParametros.Contenedor & " dshow -i "
    If OpcA <> "" Then Comandos = Comandos & OpcA
    If OpcV <> "" Then Comandos = Comandos & OpcV
    Comandos = BufferOption & " " & Comandos
End If
End With
GrabGenerarComandosEntrada = Comandos
End Function

Public Sub MostrarConfiguracionGrabScreen()
With Form14
Select Case ConfiguracionScreenCapture.Opcion
    Case 0: .Option5.Value = True
    Case 1: .Option6.Value = True
    Case 2: .Option7.Value = True
End Select
.Text5.Text = ConfiguracionScreenCapture.Ancho
.Text6.Text = ConfiguracionScreenCapture.Alto
.Text8.Text = ConfiguracionScreenCapture.x
.Text12.Text = ConfiguracionScreenCapture.y
.Text5.Text = ConfiguracionScreenCapture.Ancho
.Text13.Text = ConfiguracionScreenCapture.Titulo
.Frame5.Visible = True
.Frame1.Visible = False
.Frame2.Visible = False
.Menu_Options.Enabled = False
.Check4.Value = ConfiguracionScreenCapture.NoCapturarMouse
.Check5.Value = ConfiguracionScreenCapture.fpsOpt
.Text14.Text = ConfiguracionScreenCapture.fpsVal
End With
End Sub

Public Sub GuardarConfiguracionGrabScreen()
On Error GoTo ErrH
With Form14
If .Option5.Value = True Then
    ConfiguracionScreenCapture.Opcion = 0
Else
    If .Option6.Value = True Then
        If IsNumeric(.Text5.Text) = False Then
            .Text5.SelStart = 0
            .Text5.SelLength = Len(.Text5.Text)
            .Text5.SetFocus
            Beep
            Exit Sub
        Else
            ConfiguracionScreenCapture.Ancho = .Text5.Text
        End If
        If IsNumeric(.Text6.Text) = False Then
            .Text6.SelStart = 0
            .Text6.SelLength = Len(.Text6.Text)
            .Text6.SetFocus
            Beep
            Exit Sub
        Else
            ConfiguracionScreenCapture.Alto = .Text6.Text
        End If
        If IsNumeric(.Text8.Text) = False Then
            .Text8.SelStart = 0
            .Text8.SelLength = Len(.Text8.Text)
            .Text8.SetFocus
            Beep
            Exit Sub
        Else
            ConfiguracionScreenCapture.x = .Text8.Text
        End If
        If IsNumeric(.Text12.Text) = False Then
            .Text12.SelStart = 0
            .Text12.SelLength = Len(.Text12.Text)
            .Text12.SetFocus
            Beep
            Exit Sub
        Else
            ConfiguracionScreenCapture.y = .Text12.Text
        End If
        ConfiguracionScreenCapture.Opcion = 1
    Else
        If .Text13.Text = "" Then
            .Text13.SelStart = 0
            .Text13.SelLength = Len(.Text13.Text)
            .Text13.SetFocus
            Beep
            Exit Sub
        Else
            ConfiguracionScreenCapture.Titulo = .Text13.Text
        End If
        ConfiguracionScreenCapture.Opcion = 2
    End If
End If
If .Check5.Value = 1 Then
    If IsNumeric(.Text14) = False Then
        .Text14.SelStart = 0
        .Text14.SelLength = Len(.Text14.Text)
        .Text14.SetFocus
        Beep
        Exit Sub
    End If
    ConfiguracionScreenCapture.fpsVal = .Text14.Text
End If
ConfiguracionScreenCapture.fpsOpt = .Check5.Value
ConfiguracionScreenCapture.NoCapturarMouse = .Check4.Value
.Frame5.Visible = False
.Frame1.Visible = True
.Frame2.Visible = True
.Menu_Options.Enabled = True
End With
Exit Sub
ErrH:
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly, Form1.Caption
End Sub

Private Function GenerarComandosGdiGrab() As String
Dim Ret As String
Ret = " " & ffmpeg.FFmpegParametros.Contenedor & " gdigrab"
With ConfiguracionScreenCapture
    If .NoCapturarMouse = 1 Then Ret = Ret & " -draw_mouse 0" Else Ret = Ret & " -draw_mouse 1"
    If .fpsOpt = 1 Then Ret = Ret & " " & ffmpeg.FFmpegParametros.Videofps & " " & .fpsVal
    Select Case .Opcion
        Case Is = 0
            Ret = Ret & " -i desktop"
        Case Is = 1
            Ret = Ret & " -offset_x " & .x & " -offset_y " & .y & " -video_size " & .Ancho & "x" & .Alto & " -i desktop"
        Case Is = 2
            Ret = Ret & " -i title=" & Chr(34) & .Titulo & Chr(34)
    End Select
End With
GenerarComandosGdiGrab = Ret
End Function
