本文介绍了记录RTMP流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一堂课,它在ExoPLayer的帮助下观看rtmp流:

I have a class, which watching rtmp stream with help of ExoPLayer:

    player = ExoPlayerFactory.newSimpleInstance(context)
    val rtmpDataSourceFactory = RtmpDataSourceFactory()
    val videoSource = ProgressiveMediaSource.Factory(rtmpDataSourceFactory)
            .createMediaSource(Uri.parse(streamURL))

    player.prepare(videoSource)
    player.setVideoTextureView(playerView)
    player.playWhenReady = true

playerView是TextureView,而不是SurfaceView,因为我还需要从流中获取屏幕截图.

playerView is TextureView, picked instead of SurfaceView, because i also need to take screenshots from stream.

据我所知,ExoPlayer没有用于流记录的方法,只能下载,所以问题是-我如何记录rtmp流?我搜索了很多图书馆,并提出了一些问题,但仍然找不到干净,正常的解决方案.

As far as i know, ExoPlayer does not have methods for stream recording, only downloading, so problem is - how can i record rtmp stream? I searched a lot of libraries, and Stack questions but still cant find clean, normal solution.

此刻,我正在尝试通过基本的MediaRecorder记录流,并借助Android开发人员的帮助文档,但我仍然不了解MediaRecorder如何获取流数据或至少获取表面数据.

At the moment i am trying to record stream by basic MediaRecorder, with help Android developer documentation, but i still dont understand, how MediaRecorder acquire stream data or at least surface.

val path = "${Environment.getExternalStorageDirectory()}${File.separator}${Environment.DIRECTORY_DCIM}${File.separator}${"FILE_NAME"}"

        recorder = MediaRecorder().apply {
            setVideoSource(MediaRecorder.VideoSource.SURFACE)
            setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            setOutputFile(path)

            start()
        }

推荐答案

我通过使用FFMpeg库找到了解决方案.如果有人需要-添加此Gradle依赖项:implementation 'com.writingminds:FFmpegAndroid:0.3.2

I found the solution by using FFMpeg library. If someone needs it - add this Gradle dependency:implementation 'com.writingminds:FFmpegAndroid:0.3.2

这是代码:

        // Build path for recorded video
        val title = "/" + System.currentTimeMillis().toString() + ".mp4"
        val targetFile = File(getExternalStoragePublicDirectory(DIRECTORY_DCIM).toString() + title)

        // FFMpeg command for stream recording
        val command = arrayOf("-i", streamURL, "-acodec", "copy", "-vcodec", "copy", targetFile.toString())

        try {
            // Load the binary
            ffmpeg.loadBinary(object : LoadBinaryResponseHandler() {})
        } catch (e: FFmpegNotSupportedException) {
            e.printStackTrace()
        }

        try {
            // Execute command
            ffmpeg.execute(command, object : ExecuteBinaryResponseHandler() {})
        } catch (e: FFmpegCommandAlreadyRunningException) {
            e.printStackTrace()
        }

这篇关于记录RTMP流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 23:07