Jump to content

Recommended Posts

Posted

 

how to view video stream from "Tee pseudo-muxer" in ffmpeg inside gui form?

capture from camera

Quote

ffmpeg -f dshow -i video="WebCamera":audio="Microphone (WebCamera)" -vcodec libx264 -framerate 30 -s 1280x720 -b:a 320k -ar 48000 -ac 2 -t 10 "E:\0-Dev-Projects\0-TestArea\Camera\ffmpeg\output.mkv"

I want to have a live view while recording from the camera.

I used WebcamDS (DirectShow webcam) but it doesn't support x265 with synchronized audio in mp4 output format

Posted

Google AI give me this:

ffmpeg -f dshow -framerate 30 -video_size 1280x720 -i video="WebCamera":audio="Microphone (WebCamera)" -filter_complex "[0:v]split=2[v1][v2]" -map "[v1]" -map 0:a -vcodec libx264 -b:a 320k -ar 48000 -ac 2 -t 10 "E:\0-Dev-Projects\0-TestArea\Camera\ffmpeg\output.mkv" -map "[v2]" -f matroska - | ffplay -i -

Posted (edited)

The problem is the display in the autoit form.

this worked

Quote

ffmpeg -f dshow -i video="WebCamera":audio="Microphone (WebCamera)" -vcodec libx264 -framerate 30 -s 1280x720 -b:a 320k -ar 48000 -ac 2 -y "E:\0-Dev-Projects\0-TestArea\Camera\ffmpeg\TEST-1.mkv" -f  matroska -c copy - | ffplay -f matroska -x 760 -y 360 -

 

Edited by Parsix
  • Solution
Posted (edited)

Try this:

;Code by UEZ build 2026-06-27

#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIError.au3>

Global $iW = 1280, $iH = 720

Global $sFFMPEG = '"...\FFMPEG\bin\ffmpeg.exe"'
Global $sMPV    = '"...\MPV\mpv.exe"'
Global $sOutFile = "C:\Temp\WebCamRec.mkv"

Global $aVid, $aAud
_GetDShowDevices($sFFMPEG, $aVid, $aAud)
If $aVid[0] = 0 Then Exit MsgBox(16, "Error", "No video capture device found")
Global $sVideo = $aVid[1]                        ; first WebCam
Global $sAudio = ($aAud[0] > 0) ? $aAud[1] : ""  ; first micro, if available
Global $bHasAudio = ($sAudio <> "")

; Input-Optionen MUESSEN vor dem -i stehen (gelten fuer den folgenden Input)
Global $sInput = '-f dshow -rtbufsize 256M -video_size 1280x720 -framerate 30 -pixel_format nv12 ' & _
        '-use_wallclock_as_timestamps 1 -i video="' & $sVideo & '"'
If $bHasAudio Then $sInput &= ' -f dshow -use_wallclock_as_timestamps 1 -i audio="' & $sAudio & '"'
Global $sAudioMap = $bHasAudio ? '-map 1:a -c:a aac -ar 48000 -ac 2 ' : ''

Global $hGUI = GUICreate("WebCam + Live Preview + Record", $iW, $iH + 40, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
Global $idStop = GUICtrlCreateButton("Stop", 10, $iH + 6, 80, 26)

Global $hChild = GUICreate("", $iW, $iH, 0, 0, $WS_CHILD, -1, $hGUI)
GUISetState(@SW_SHOW, $hChild)
GUISwitch($hGUI)
GUISetState(@SW_SHOW, $hGUI)

Global $wid = Number($hChild)

If FileExists($sOutFile) Then FileDelete($sOutFile)

Global $sUDP = "udp://127.0.0.1:1234?pkt_size=1316"

Global $sCmd =  $sFFMPEG & ' ' & $sInput & ' ' & _
                '-filter_complex "[0:v]split[rec][tmp];[tmp]fps=30[prev]" ' & _
                '-map "[rec]" -c:v libx264 -pix_fmt yuv420p ' & $sAudioMap & '"' & $sOutFile & '" ' & _
                '-map "[prev]" -c:v libx264 -preset ultrafast -tune zerolatency -g 10 -an ' & _
                '-fflags +nobuffer -flags +low_delay -muxdelay 0 -muxpreload 0 -flush_packets 1 ' & _
                '-f mpegts ' & $sUDP

Global $iPID = Run($sCmd, "", @SW_HIDE, $STDIN_CHILD)

Global $sMpvPipe = "\\.\pipe\mpv_preview"
Global $sMpvCmd = $sMPV & ' --wid=' & $wid & ' --no-audio --no-osc --force-window=yes ' & _
                  '--profile=low-latency --untimed --no-cache --demuxer-readahead-secs=0 ' & _
                  '--vd-queue-enable=no --demuxer-lavf-o=fflags=+nobuffer ' & _
                  '--demuxer-lavf-format=mpegts --input-ipc-server=' & $sMpvPipe & ' "' & $sUDP & '"'
                  
Global $iPIDmpv = Run($sMpvCmd, "")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $idStop
            _StopAll($iPID, $iPIDmpv)
            ExitLoop
    EndSwitch
WEnd
GUIDelete($hGUI)

Func _GetDShowDevices($sFFMPEG, ByRef $aVideo, ByRef $aAudio)
    Local $sExe = StringReplace($sFFMPEG, '"', '')
    Local $iPID = Run('"' & $sExe & '" -hide_banner -list_devices true -f dshow -i dummy', "", @SW_HIDE, $STDERR_CHILD)
    Local $sOut = ""
    While 1
        $sOut &= StderrRead($iPID)
        If @error Then ExitLoop
    WEnd
    Local $aV[1] = [0], $aA[1] = [0], $sType = ""
    Local $aLines = StringSplit(StringStripCR($sOut), @LF), $sLine, $aM
    For $i = 1 To $aLines[0]
        $sLine = $aLines[$i]
        If StringInStr($sLine, "(video)") Then $sType = "v"
        If StringInStr($sLine, "(audio)") Then $sType = "a"
        If StringInStr($sLine, "Alternative name") Then ContinueLoop
        $aM = StringRegExp($sLine, '"([^"]+)"', 1)
        If @error Then ContinueLoop
        If $sType = "v" Then
            _ArrayAdd_($aV, $aM[0])
        ElseIf $sType = "a" Then
            _ArrayAdd_($aA, $aM[0])
        EndIf
    Next
    $aVideo = $aV
    $aAudio = $aA
EndFunc

Func _ArrayAdd_(ByRef $a, $v)
    ReDim $a[$a[0] + 2]
    $a[0] += 1
    $a[$a[0]] = $v
EndFunc

Func _StopAll($iPID, $iPIDmpv)
    If ProcessExists($iPID) Then
        StdinWrite($iPID, "q")
        StdinWrite($iPID)            
        ProcessWaitClose($iPID, 10)  
        If ProcessExists($iPID) Then ProcessClose($iPID) 
    EndIf

    Local $h = FileOpen($sMpvPipe, 2)
    If $h <> -1 Then
        FileWrite($h, '{"command":["quit"]}' & @LF)
        FileClose($h)
    EndIf
    ProcessWaitClose($iPIDmpv, 2)
    If ProcessExists($iPIDmpv) Then ProcessClose($iPIDmpv)
EndFunc

mpv can be found here: https://sourceforge.net/projects/mpv-player-windows/files/

 

Adjust the pathes to ffmpeg and mpv!

Edited by UEZ
Code update

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted
39 minutes ago, Parsix said:
Invalid input file index: 1.
Failed to set value '1:a' for option 'map': Invalid argument
Error parsing options for output file C:\temp\WebCamRec.mkv.
Error opening o

 

Made some updates - please try again.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

 

this error :

[in#0 @ 000001ff3283f740] Could not set video options
[in#0 @ 000001ff32845780] Error opening input: I/O error
Error opening input file video=WebCamera.
Error opening input files: I/O error

i modify to this and not have error (ffmpeg)

Global $sAudioMap = ($sAudio <> "") ? '-map 0:a -c:a aac -ar 48000 -ac 2 ' : ''
$sInput = '-f dshow -i video="Camera":audio="Microphone (Camera)" -rtbufsize 256M -video_size 1280x720 -framerate 30 -pixel_format nv12  -use_wallclock_as_timestamps 1'
Global $sCmd = $sFFMPEG & ' ' & $sInput & ' ' & _
               '-filter_complex "[0:v]split[rec][tmp];[tmp]fps=30[prev]" ' & _
               '-map "[rec]" -c:v libx264 -pix_fmt yuv420p ' & $sAudioMap & '"' & $sOutFile & '" ' & _
               '-map "[prev]" -c:v mpeg2video -q:v 5 -an -f mpegts ' & $sUDP

 

but mpv.exe not work

image.thumb.png.2fcd1f59b003e7e8cfd399f7a2f563b5.png

Posted (edited)

Sorry, access was blocked by the firewall. The playback was successful, but the playback speed is very slow and there is a lot of delay.

Thank you Dear UEZ, all the best to you always.

 

Edited by Parsix
Posted (edited)

The delay is still noticeable. It doesn't record the last few seconds before pressing the stop button. 

 

Edited by Parsix
Posted
6 hours ago, Parsix said:

The delay is still noticeable. It doesn't record the last few seconds before pressing the stop button. 

 

Last try - see above.

Personally, I don't like this approach of using ffmpeg and mpv to display and record the webcam.

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...