title: C++ - Failed to stream to youtube with ffmpeg
body: <p>Well, I have an open source project similar to OBS but aimed at churches, the repository you can find <a href="https://github.com/ivessambex/wob/blob/main/src/wob/broadcast.cpp" rel="nofollow noreferrer">here</a>, however I'm stuck in the encoding part, <code>avcodec_send_frame</code> after a while just returns Failed to send frame - code: -11 err: Resource temporarily unavailable and never gets to the stage of receiving packets.</p>
  <p>I've tried everything including changing the output format to MP4 but the application just crashes, this time I'm challenging myself to a bigger project, I thought I had solved it by changing the codec to H.264 but I was wrong</p>
  <p>Basically I'm trying to get OpenCV images from my webcam and send them to youtube</p>
  <pre><code>void Broadcast::writeFrame(const cv::Mat &amp;image)
  {
  
      const int width = image.cols;
      const int height = image.rows;
  
      if(swConv == nullptr) {
          swConv = sws_getContext(width, height, AV_PIX_FMT_RGB24, codecCtx-&gt;width, codecCtx-&gt;height, codecCtx-&gt;pix_fmt, SWS_BILINEAR, nullptr, nullptr, nullptr);
          if(!swConv) {
              qCritical() &lt;&lt; &quot;Could not create SwContext&quot;;
              return;
          }
      }
  
      const int stride[4] = { static_cast&lt;int&gt;(image.step[0]) };
      sws_scale(swConv, &amp;image.data, stride, 0, image.rows, frame-&gt;data, frame-&gt;linesize);
      frame-&gt;pts = frame_pts++;
  
      int ret = avcodec_send_frame(codecCtx, frame);
      if(ret &lt; 0) {
          qWarning() &lt;&lt; &quot;Failed to send frame - code: &quot; &lt;&lt; ret &lt;&lt; &quot; err: &quot; &lt;&lt; av_err2str(ret);;
          return;
      }
  
      qDebug() &lt;&lt; &quot;AA &quot; &lt;&lt; ret;
  
      // Encode frames into packet
      while(ret &gt; 0) {
  
          qDebug() &lt;&lt; &quot;BB &quot; &lt;&lt; ret;
  
          ret = avcodec_receive_packet(codecCtx, pkt);
          if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
              return;
          } else if(ret &lt; 0) {
              qCritical() &lt;&lt; &quot;Error reciving packets: &quot; &lt;&lt; av_err2str(ret);
              break;
          }
  
          qDebug() &lt;&lt; &quot;CC&quot;;
  
          av_write_trailer(ofctx);
          //av_packet_unref(pkt);
  
      }
  
      qDebug() &lt;&lt; &quot;Frames &quot; &lt;&lt; frame_pts;
  
  }
  </code></pre>