# Using django as a mjpeg streaming server

**URL:** https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881
**Category:** Forms & APIs
**Created:** [March 29, 2022, 9:35am UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881 "2022-03-29T09:35:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![goncaloLopesMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goncalolopesmarques/32/4207_2.png) [@goncaloLopesMarques](https://forum.djangoproject.com/u/goncaloLopesMarques)
#### Post date: [March 29, 2022, 9:35am UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/1 "2022-03-29T09:35:32Z")

</div>

Hi guys,

I´m working on a project a bit strange. I have an mjpeg cam stream who i need to fetch and refeed to a user, and give some funcionalities like pause and go forward and backward.

What i did:

When user send GET to the server for the player page i create one thread who is always fill one circular buffer, for ever.  
Next when the user hits the pause button i copy the buffer to a new auxililiar buffer and send the last pic, when the user hits the backward i grab the auxiliar buffer and send the pics bacward.

In the development server works mor or less. Some times get stuck, but i try on ubuntu with gunicorn with 1 worker and nginx and the server streams the mjpeg but do nothing more, i send a bunch more GETs to the server bue he is stuck with the StreamingHttpResponse().

I need some help. probabily i´m using the Django in a way that wasn’t supposed to.

Best regards Gonçalo Marques

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [March 29, 2022, 11:27am UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/2 "2022-03-29T11:27:34Z")

</div>

You’re right, you really don’t want to try to do this in Django. Typically, this is handled by some JavaScript running in the browser, taking advantage of facilities available in a common web server (e.g. nginx)

See [HTTP Live Streaming - Wikipedia](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) for some more detailed information and possible solutions. You can also do a web search for HTML5 Video players for more alternatives.

---

<div class="post-metadata">

### Author: ![goncaloLopesMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goncalolopesmarques/32/4207_2.png) [@goncaloLopesMarques](https://forum.djangoproject.com/u/goncaloLopesMarques)
#### Post date: [March 29, 2022, 12:33pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/3 "2022-03-29T12:33:35Z")

</div>

that’s what i thought. The problem is that it’s in mjpeg, I haven’t found any library or software to do this kind of thing.

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [March 29, 2022, 12:36pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/4 "2022-03-29T12:36:11Z")

</div>

That seems surprising to me based on what I’m seeing here: [Motion JPEG - Wikipedia](https://en.wikipedia.org/wiki/Motion_JPEG)

---

<div class="post-metadata">

### Author: ![goncaloLopesMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goncalolopesmarques/32/4207_2.png) [@goncaloLopesMarques](https://forum.djangoproject.com/u/goncaloLopesMarques)
#### Post date: [March 29, 2022, 12:45pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/5 "2022-03-29T12:45:59Z")

</div>

I found nothing to js. i´m playing the stream using the src image = stream url. but its a img and have no methods or code to help me. I thought grabing the stream in the browser and parse the stream frame by frame but no luck in that either.  
Its hard to find js player to play mjoeg stream. i tried a bunch of open source player, none work =(

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [March 29, 2022, 12:47pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/6 "2022-03-29T12:47:42Z")

</div>

You should be looking at the `video` tag for streaming media. See [Live streaming web audio and video - Developer guides | MDN](https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery/Live_streaming_web_audio_and_video)

---

<div class="post-metadata">

### Author: ![goncaloLopesMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goncalolopesmarques/32/4207_2.png) [@goncaloLopesMarques](https://forum.djangoproject.com/u/goncaloLopesMarques)
#### Post date: [March 29, 2022, 12:51pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/7 "2022-03-29T12:51:13Z")

</div>

First thing i tried, no luck, dont support mjpeg

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [March 29, 2022, 12:55pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/8 "2022-03-29T12:55:38Z")

</div>

How are you doing the streaming on the server-side?

Quoting directly from the referenced page:

> In order to stream live audio and video, you will need to run specific streaming software on your server or use third-party services.

---

<div class="post-metadata">

### Author: ![goncaloLopesMarques](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goncalolopesmarques/32/4207_2.png) [@goncaloLopesMarques](https://forum.djangoproject.com/u/goncaloLopesMarques)
#### Post date: [March 29, 2022, 1:02pm UTC](https://forum.djangoproject.com/t/using-django-as-a-mjpeg-streaming-server/12881/9 "2022-03-29T13:02:45Z")

</div>

There is no audio. So what i have is very simple.  
Server side - I´m using u **rllib.request.urlopen** and a while True, to fetch the stream, i´m parse the stream and store img by img on a deqeue.  
Client side i´m using img tag with src url tha do a GET to server side.  
Server side - using while true, StreamingHttpResponse, and content\_type=‘multipart/x-mixed-replace;boundary=FRAME’.  
Its work just fine, the things get complicated when i execute actions on the stream, like pause and backward
