Skip to content

Instantly share code, notes, and snippets.

@kndt84
Created March 23, 2021 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kndt84/2f4b78fcff7a63f32c2ba4d5f584fbdf to your computer and use it in GitHub Desktop.
Save kndt84/2f4b78fcff7a63f32c2ba4d5f584fbdf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import gi
import argparse
import pathlib
parser = argparse.ArgumentParser(description='Create a RTSP stream from a video file')
parser.add_argument('-f', '--file')
parser.add_argument('-p', '--port', default='554')
args = parser.parse_args()
abs_video_path = pathlib.Path(args.file).resolve()
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)
class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self):
GstRtspServer.RTSPMediaFactory.__init__(self)
def do_create_element(self, url):
src_demux = "filesrc location={} ! qtdemux name=demux".format(abs_video_path)
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(args.port)
factory = TestRtspMediaFactory()
factory.set_shared(True)
mountPoints = self.rtspServer.get_mount_points()
mountPoints.add_factory("/stream1", factory)
self.rtspServer.attach(None)
if __name__ == '__main__':
s = GstreamerRtspServer()
loop.run()
@kndt84
Copy link
Author

kndt84 commented Mar 23, 2021

Usage:

Install required packages.

sudo apt-get install gstreamer-1.0 gst-rtsp-server-1.0

Execute the script

python3 video2rtsp.py -f ./videoplayback.mp4 -p 554

Receive RTSP stream from the following endpoint

rtsp://[server's ip address]:554/stream1

@jrichardsz
Copy link

I ran python3 main4.py -f /tmp/foo.mp4 -p 8554

And then if I try to view the video with

ffplay rtsp://127.0.0.1:8554/stream1

I got this error:

method DESCRIBE failed: 503 Service Unavailable
rtsp://127.0.0.1:8554/stream1: Server returned 5XX Server Error reply

I researched this error without success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment