bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.57k stars 1.58k forks source link

Stream to facebook #491

Closed y4nnick closed 8 years ago

y4nnick commented 8 years ago

I tried the RecordActivity from the examples to stream to facebook live.

I just edited the url with: private String ffmpeg_link = "rtmp://rtmp-api.facebook.com:80/rtmp/<KEY HERE>";

and updated the settings to the facebook standarts:

` //--------------------------------------- // initialize ffmpeg_recorder //--------------------------------------- private void initRecorder() { Log.w(LOG_TAG,"init recorder");

    if (RECORD_LENGTH > 0) {
        imagesIndex = 0;
        images = new Frame[RECORD_LENGTH * frameRate];
        timestamps = new long[images.length];
        for (int i = 0; i < images.length; i++) {
            images[i] = new Frame(imageWidth, imageHeight, Frame.DEPTH_UBYTE, 2);
            timestamps[i] = -1;
        }
    } else if (yuvImage == null) {
        yuvImage = new Frame(imageWidth, imageHeight, Frame.DEPTH_UBYTE, 2);
        Log.i(LOG_TAG, "create yuvImage");
    }

    Log.i(LOG_TAG, "ffmpeg_url: " + ffmpeg_link);
    recorder = new FFmpegFrameRecorder(ffmpeg_link, imageWidth, imageHeight, 2);

      /*
        From https://www.facebook.com/facebookmedia/get-started/live

        Video Format:
        1) We accept video in maximum 720p (720 x 1280) resolution, at 30 frames per second. (or 1 key frame every 2 seconds).
        2) You must send an I-frame (keyframe) at least once every two seconds throughout the stream.
        3) Recommended max bit rate is 4000 Kbps.
        4) Titles must be less than 255 characters otherwise the stream will fail.
        5) The Live API accepts H264 encoded video and AAC encoded audio only.

        Advanced Settings
        6) Pixel Aspect Ratio: Square.
        7) Frame Types: Progressive Scan.
        8) Audio Sample Rate: 44.1 KHz.
        9) Audio Bitrate: 128 Kbps stereo.
        10) Bitrate Encoding: CBR.
    */

    // 1) Satisfied by imageWidth = 320, imageHeight = 240 as Frame size. Frame rate is set to 30 fps
    recorder.setFrameRate(frameRate);

    // 2) You must send an I-frame (keyframe) at least once every two seconds throughout the stream.
    // Key frame interval, in our case every 2 seconds -> 30 (fps) * 2 = 60
    // (gop length)
    recorder.setGopSize(60);

    // 3) Bitrate <  4000 Kbps
    recorder.setVideoBitrate(2000000);

    // 4) Title can not be set here, must be set in the publishing tool or while creating the live video over the API

    // 5) The Live API accepts H264 encoded video and AAC encoded audio only.
    recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
    recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);

    // 6) Pixel Aspect Ratio: Square. TODO! How should we set this?
    // Maybe over : recorder.setPixelFormat(); but with what parameter?

    // 7) Frame Types: Progressive Scan. TODO! How should we set this?

    // 8) Audio Sample Rate: 44.1 KHz.
    recorder.setSampleRate(44100);

    // 9) Audio Bitrate: 128 Kbps stereo.
    recorder.setAudioBitrate(128000);
    recorder.setAudioChannels(2);

    // 10) Bitrate Encoding: CBR. TODO How should we set this?

    /*
        Additional Settings from the orginal RecordActivity.java and  WebcamAndMicrophoneCapture.java (there the stream to rtmp)
     */

    // decrease "startup" latency in FFMPEG (see:
    // https://trac.ffmpeg.org/wiki/StreamingGuide)
    recorder.setVideoOption("tune", "zerolatency");

    // tradeoff between quality and encode speed
    // possible values are ultrafast,superfast, veryfast, faster, fast,
    // medium, slow, slower, veryslow
    // ultrafast offers us the least amount of compression (lower encoder
    // CPU) at the cost of a larger stream size
    // at the other end, veryslow provides the best compression (high
    // encoder CPU) while lowering the stream size
    // (see: https://trac.ffmpeg.org/wiki/Encode/H.264)
    recorder.setVideoOption("preset", "ultrafast");

    // Constant Rate Factor (see: https://trac.ffmpeg.org/wiki/Encode/H.264)
    recorder.setVideoOption("crf", "28");

    // 2000 kb/s, reasonable "sane" area for 720
    recorder.setFormat("flv");

    // We don't want variable bitrate audio
    recorder.setAudioOption("crf", "0");

    // TODO What is this for?
    recorder.setInterleaved(true);

    // We don't want variable bitrate audio
    recorder.setAudioOption("crf", "0");

    // Highest quality
    recorder.setAudioQuality(0);

    Log.i(LOG_TAG, "recorder initialize success");

    audioRecordRunnable = new AudioRecordRunnable();
    audioThread = new Thread(audioRecordRunnable);
    runAudioThread = true;
}`

The facebook guide can be found here: https://www.facebook.com/facebookmedia/get-started/live The facebook preview of the livestream just says that it is offline.

I summarized the settings rom facebbook into 10 points. You can see them above as a comment in the code.

I don't know how to set the settings for 6,7 and 10. I assume that they are the reason that it is not working.

Can someone please explain how i could set it them?

Has anyone ever manged to stream to facebook live here, would be nice if someone has a code sample?

PS: i am currently trying this over the android emulator. But that shouldn't be a problem since i have a active internet connection in the emulator.

y4nnick commented 8 years ago

I figured it out, the code above is fine, just make sure that you set RECORD_LENGTH = 0. if desired i could publish an example application including the loggin etc.

saudet commented 8 years ago

Ok, cool :) Thanks for sharing

saudet commented 8 years ago

And a sample application project would be great. Please send your pull request here: https://github.com/bytedeco/sample-projects