본문 바로가기

gstreamer

[python] mp4 file streaming with gstreamer

gstreamer을 이용해 file을 streaming 하는 코드이다. 

 

gstmeamer 설치후, 

아래 내용을 python으로 돌리자. 

 

GstRTSPServer 을 통해 mp4를 실시간 스트리밍 할 수 있다. 

 

gi를 설치하는데 상당히 애를 먹었었다. 

>> pip install PyGObjec 

에러 뜨면, 에러를 자세히 읽은 다음,  설치하란거 설치하면 된다.

 

아래 코드의 내용은, 

GstRTSPServer을 통해,  mp4파일을

"rtsp://localhost:{PORT}/{MOUNTPOINT}" 의 url을 통해 송출한다. 

 

아래 파일실행후, 아래 명령어를 통해서 스트림이 잘 나오는지 확인할 수 있다. 

 ffprobe -v verbose -show_packets "rtsp://localhost:8554/test"

 

#!/usr/bin/env python

import sys
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib

loop = GLib.MainLoop()
Gst.init(None)

PORT = sys.argv[1]


class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self):
        GstRtspServer.RTSPMediaFactory.__init__(self)

    def do_create_element(self, url):
        #set mp4 file path to filesrc's location property
        src_demux = "filesrc location=/home/user/yj/rex2.mp4 ! qtdemux name=demux"
        h264_transcode = "demux.video_0"
        #uncomment following line if video transcoding is necessary
        #h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
        pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
        print ("Element created: " + pipeline)
        return Gst.parse_launch(pipeline)

class GstreamerRtspServer():
    def __init__(self):
        self.rtspServer = GstRtspServer.RTSPServer()
        self.rtspServer.set_service(PORT)   # rtsp 스트리밍 포트 번호
        factory = TestRtspMediaFactory()
        factory.set_shared(True)
        mountPoints = self.rtspServer.get_mount_points()
        mountPoints.add_factory("/test", factory)
        self.rtspServer.attach(None)

if __name__ == '__main__':
    s = GstreamerRtspServer()
    loop.run()

 

'gstreamer' 카테고리의 다른 글

[명령어 정리] live stream 상태 체크 방법 (w. ffprobe 간단!)  (0) 2022.11.10
gstreamer : muxer  (0) 2022.11.10
gstreamer : muxer  (0) 2022.11.03
미디어 관련내용 정리  (0) 2022.11.03
Gstreamer Memory monit with valgrind  (0) 2022.01.26