Search code examples
javaandroidkotlinrtsp

android Record a RTSP stream in a file from SurfaceView


I'm showing a RTPS live stream (from a camera) into a SurfaceView (my_surface_view) using camera's IP. For this i'm using the follow library -> implementation 'com.github.pedroSG94.vlc-example-streamplayer:libvlc:2.5.14v3'

I need to implement a feature so the user can record the stream into a File but when i try to record my video from my surface view using mediaRecorder i get : java.lang.IllegalArgumentException: not a PersistentSurface

Here is my code :

    mediaRecorder.setInputSurface(my_surface_view.holder.hurface)
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT)
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
    mediaRecorder.setVideoEncodingBitRate(512 * 1000)
    mediaRecorder.setVideoFrameRate(30)
    mediaRecorder.setVideoSize(640, 480)
    mediaRecorder.setOutputFile(File(getVideosDirectory(), "TEST.mp4").path)
    mediaRecorder.prepare()
    mediaRecorder.start()

I ve look on the internet but i didn t found any library or example. And right now i'm stuck and have no ideea


Solution

  • I've manage to do it using FFmpeg ->

    implementation 'com.arthenica:mobile-ffmpeg-full-gpl:4.2.2.LTS'

    For starting the recording (make sure it s not on main thread), and the file have the .mkv extension

       runOnIoThread {
                    FFmpeg.execute("-i $url -acodec copy -bsf:a aac_adtstoasc -vcodec copy ${file.path}")
                }
    

    and to stop it -> FFmpeg.cancel()