B4J Question How to grab RTSP in console B4J app under Linux ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All
It's needed to grab frames from IP-cam RTSP-stream under Linux, in console server app without window interface.
And to save JPG-frames.

Any solution now in 2022 in B4J under Linux ?
 

peacemaker

Expert
Licensed User
Longtime User
capture 10 fps
Yes, thanks.
It works (tried under Windows now). 485 images per 48 seconds. Naming files is an issue. The result quality is also somehow strange...will check.
But it seems ... it's workable !

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
    Private Button2 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Sub Button1_Click
    Get_Frame
End Sub


Sub Get_Frame()
    Dim URL As String = "rtsp://192.168.1.168:554/ch01/0"
  
    'Dim command As String = "/usr/bin/ffmpeg -rtsp_transport tcp -i " & URL & " -r 1 -vsync 1 -qscale 1 -frames:v 1 -f image2 images_$(date +%F_%H-%M-%S).jpg"
  
    If DetectOS = "linux" Then
        Dim ff As String = "/usr/bin/ffmpeg"
    Else
        Dim ff As String = File.Combine(File.GetFileParent(File.DirApp), "/ffmpeg/bin/ffmpeg.exe")
    End If
    Dim Args As List
    Args.Initialize
    Args.Add("-rtsp_transport")
    Args.Add("tcp")
    Args.Add("-i")
    Args.Add(QUOTE & URL & QUOTE)
    Args.Add("-r")
    Args.Add("4") 'try for 10fps
    
    Args.Add("images_%01d.jpg")
          
              
    Dim sh As Shell
    sh.Initialize("ffmpeg", ff, Args)
    sh.WorkingDirectory = File.DirApp
  
    sh.Run(-1)
    'Dim res As ShellSyncResult =
  
    '    #if Debug
    '    Log(res.Success & "; ExitCode = " & res.ExitCode)
    '    Log("StdOut = " &  res.StdOut)
    '    Log("StdErr = " &  res.StdErr)
    '    #end if
End Sub

Sub Get_Stamp As String
    Dim b As String, d, E As Long, f As Float
    DateTime.DateFormat = "yyyy-MM-dd"
    DateTime.TimeFormat = "HH-mm-ss"

    d = DateTime.Now
    E = d - DateTime.TimeParse(DateTime.Time(d))
    f = E/DateTime.TicksPerSecond
    b = NumberFormat(f, 0, 3)

    Dim stamp As String = DateTime.DAte(d) & " " & DateTime.Time(d) & b
    Return stamp
End Sub

Private Sub Button2_Click
    Dim ff As String = "taskkill.exe"
    Dim Args As List
    Args.Initialize
    Args.Add("/F")
    Args.Add("/IM")
    Args.Add("ffmpeg.exe")

          
              
    Dim sh As Shell
    sh.Initialize("tk", ff, Args)
    sh.WorkingDirectory = "c:\windows\system32"
  
    sh.Run(-1)
End Sub

Sub DetectOS As String
    Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
    If os.Contains("win") Then
        Return "windows"
    Else If os.Contains("mac") Then
        Return "mac"
    Else
        Return "linux"
    End If
End Sub
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
Yes, thanks.
It works (tried under Windows now). 485 images per 48 seconds. Naming files is an issue. The result quality is also somehow strange...will check.
But it seems ... it's workable !
You can play with quality with other options of ffmpeg.

Also as for image names... they are options too... you can loop overwriting them... or you can create a timer and a sub transferring somewhere or delete them after time... file.listfiles can be your friend here...
 
Upvote 0
Top