How can we transcode live rtmp stream to live hls stream using ffmpeg?

后端 未结 4 820
花落未央
花落未央 2020-12-07 09:03

I am trying to convert a live rtmp stream to hls stream on real time.

I got some idea after reading

http://sonnati.wordpress.com/2011/08/30/ffmpeg-%E2%80%93-

4条回答
  •  [愿得一人]
    2020-12-07 09:50

    If you already have the RTMP live stream ready and playing as HLS then you can simply add .m3u8 after the stream name and make RTMP link to http. For example you have RTMP link like this:

    rtmp://XY.Y.ZX.Z/hls/chid

    You have to just make the url like this:

    http://XY.Y.ZX.Z/hls/chid.m3u8

    and it will play smoothly in iOS. I have tried following code and it is working fine.

    func setPlayer()
    {
        // RTMP URL rtmp://XY.Y.ZX.Z/hls/chid be transcripted like this http://XY.Y.ZX.Z/hls/chid.m3u8 it will play normally.
    
        let videoURL = URL(string: "http://XY.Y.ZX.Z/hls/chid.m3u8")
    
        let playerItem = AVPlayerItem(url: videoURL!)
        let adID = AVMetadataItem.identifier(forKey: "X-TITLE", keySpace: .hlsDateRange)
        let metadataCollector = AVPlayerItemMetadataCollector(identifiers: [adID!.rawValue], classifyingLabels: nil)
        //metadataCollector.setDelegate(self, queue: DispatchQueue.main)
        playerItem.add(metadataCollector)
    
    
        let player = AVPlayer(playerItem: playerItem)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        self.player = player
        player.play()
    }
    

    But it will be slow and laggy because of the high resolution video stream upload. If you make the resolution to low when uploading the video stream, it will work smooth in low bandwidth network as well.

    Please note: It is not by FFMPEG as we have already RTMP running by FFMPEG so I did like this.

提交回复
热议问题