安卓 在 SurfaceView 中将 RTSP 流录制到文件中

huangapple go评论126阅读模式
英文:

android Record a RTSP stream in a file from SurfaceView

问题

我正在将一个来自摄像头的 RTPS 实时流展示在一个 SurfaceView(my_surface_view)中,使用摄像头的 IP 地址。
为此,我正在使用以下库 -> implementation 'com.github.pedroSG94.vlc-example-streamplayer:libvlc:2.5.14v3'

我需要实现一个功能,使用户可以将流记录到文件中,但是当我尝试使用 mediaRecorder 从我的 surface view 录制视频时,我收到以下错误:java.lang.IllegalArgumentException: not a PersistentSurface

以下是我的代码:

mediaRecorder.setInputSurface(my_surface_view.holder.surface)
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'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

答案1

得分: 0

我已经成功使用FFmpeg完成了这个操作 ->

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

开始录制(确保不在主线程上),文件的扩展名为.mkv

在IO线程上运行以下代码:

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

停止录制的方法为 -> FFmpeg.cancel()

英文:

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()

huangapple
  • 本文由 发表于 2020年4月10日 20:38:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定