ESP32-CAM and live streaming to Youtube

Hi,

I bought a ESP32-CAM and got it running following this tutorial here: ESP32-CAM Video Streaming Web Server (works with Home Assistant) | Random Nerd Tutorials

Now I want to live stream to Google. It should work, if you send the stream to a special URL:

rtmp://a.rtmp.youtube.com/live2/[your-secret-key-here]

I want to change the Arduino code to do so, and I have programming skills. But I really don't know how to start.

From this script:

static esp_err_t stream_handler(httpd_req_t *req){
  camera_fb_t * fb = NULL;
  esp_err_t res = ESP_OK;
  size_t _jpg_buf_len = 0;
  uint8_t * _jpg_buf = NULL;
  char * part_buf[64];

  res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  if(res != ESP_OK){
    return res;
  }

  while(true){
    fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Camera capture failed");
      res = ESP_FAIL;
    } else {
      if(fb->width > 400){
        if(fb->format != PIXFORMAT_JPEG){
          bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
          esp_camera_fb_return(fb);
          fb = NULL;
          if(!jpeg_converted){
            Serial.println("JPEG compression failed");
            res = ESP_FAIL;
          }
        } else {
          _jpg_buf_len = fb->len;
          _jpg_buf = fb->buf;
        }
      }
    }
    if(res == ESP_OK){
      size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
      res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
    }
    if(res == ESP_OK){
      res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
    }
    if(res == ESP_OK){
      res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
    }
    if(fb){
      esp_camera_fb_return(fb);
      fb = NULL;
      _jpg_buf = NULL;
    } else if(_jpg_buf){
      free(_jpg_buf);
      _jpg_buf = NULL;
    }
    if(res != ESP_OK){
      break;
    }
    //Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
  }
  return res;
}

What must be changed to send it to the Youtube URL? Can someone point me in the right direction please? Where to I start. :smiley:

greetings and thanks,
André

You should research motion-jpeg (ESP32CAM) and Real Time Messaging Protocol (YouTube).

Most of the cheap WiFi cameras are capable of using rtmp,
but I never really stumbled over source code of such devices.

So you will have to write (or find) a rtmp server implementation for the ESP32.

This GitHub - hpwit/Stream-video-esp32 seems to be good starting point.

If you want to stream Youtube video
you need to install youtube-dl brew install youtube-dl and then do:
./StreamVideo -i $(youtube-dl -f bestvideo -g https://youtu.be/CZ9Pu9Usk5o) -d 123x48 -a 192.168.1.22

Thanks! I will read the resources and try to figure out if and how it could work.

I also found this library, maybe it's partly helpful: GitHub - geeksville/Micro-RTSP: A RTSP video server intended for very small CPUs (ESP32 etc)

greetings,
André

Any luck with this?
I am struggling with the same to find an implementation.
I believe it should be pretty simple, but same as you, I am missing the starting point.
Any info or insights you have gained will be appreciated.
--Abhinav

Hey,

not yet and don't have much time to investigate it. :frowning: I created an issue in this library here, but got no response until now: https://github.com/geeksville/Micro-RTSP/issues/19

I put the "project" on ice until I have more time or maybe someone else has done this already.

greetings,
André

Might be nice since you have figured it out to post your code to help others.

Martin please share your sketch...I was trying similar. No luck yet.

Thanks!

martinius96:
Sorry, but I am not interested to share that source code.

I dislike that such habits very much.

martinius96:
Yes, dislike that, I spent 3 months making code and now you think I will send it to you because you simple want it... haha, NO.
If someone is interested in buying code, or binary file (will cost less), then contact me at: martinius96@gmail.com

I hope I can remember your nick, but I'm pretty confident that I will.

Now people will be motivated to duplicate your efforts and give it away for free to thank you. If an ESP32- CAM can serve up a single frame, then serving up repeating frames should be fairly easy.

Here is a link to a working Example FOR FREE https://www.hackster.io/BnBe_Club/9-rtsp-video-streamer-using-the-esp32-cam-board-8832b4

Hello,

Has any one get the solution to this exact scenario: where the stream of the esp32-cam can be post directly to youtube or any other rmtp server. I have done POC with raspberry pie and android OS but didn\t get any success for esp32-cam.

@5ran6 thanks for sharing your article but it is not showcasing solution expected in the thread as it is making the esp-32 as local streaming server which can be accessed over local wifi network, however expected is to have a sketch that will publish the stream to some server like youtube. as similar to

"ffmpeg -re -i lion-sample.mkv -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv rtmp://XX.XXX.XXX/live/key" is doing in linux