diff --git a/ffmpeg/tests/test_facebook.py b/ffmpeg/tests/test_facebook.py index 83c3d17..9ac6cf0 100644 --- a/ffmpeg/tests/test_facebook.py +++ b/ffmpeg/tests/test_facebook.py @@ -1,45 +1,54 @@ from __future__ import unicode_literals -from builtins import bytes -from builtins import range -from builtins import str -import ffmpeg + import os -import pytest import random import re import subprocess +from builtins import bytes, range, str + +import ffmpeg +import pytest +from ffmpeg import nodes try: import mock # python 2 except ImportError: from unittest import mock # python 3 - def pip_to_facebook(stream_key: str): - """ Capture Facetime camera and screen in macOS and stream it to facebook - """ - facetime_camera_input = ( - ffmpeg - .input('FaceTime:1', format='avfoundation', pix_fmt='uyvy422', framerate=30, s='320x240', probesize='200M') - ) - audio = facetime_camera_input.audio +def pip_to_rtmp(url: str): + """ Capture Facetime camera and screen in macOS and stream it to facebook + """ + facetime_camera_input = ( + ffmpeg + .input('FaceTime:1', format='avfoundation', pix_fmt='uyvy422', framerate=30, s='320x240', probesize='200M') + ) - video = ( - ffmpeg - .header(thread_queue_size='512', vsync='2') - .input('1:1', format='avfoundation', - pix_fmt='uyvy422', framerate=30, probesize='200M') - .overlay(facetime_camera_input) + audio = facetime_camera_input.audio - ) - output = ( - ffmpeg - .output(video, audio, 'rtmps://live-api-s.facebook.com:443/rtmp/{}'.format(stream_key), vsync='2', s='1280x720', pix_fmt='yuv420p', video_bitrate='1500000', f='flv', - vcodec='libx264', preset='superfast', x264opts='keyint=15', g='30', ac='2', ar='48000', acodec="aac", audio_bitrate="128000") - ) + # ffmpeg in MacOS AVFoundation MUST increase the thread_queue_size in order to handle PIP + video = ( + ffmpeg + .header(thread_queue_size='512', vsync='2') + .input('1:1', format='avfoundation', + pix_fmt='uyvy422', framerate=30, probesize='200M') + .overlay(facetime_camera_input) - output.overwrite_output().run() + ) + output = ( + ffmpeg + .output(video, audio, url, vsync='2', s='1280x720', pix_fmt='yuv420p', video_bitrate='1500000', f='flv', + vcodec='libx264', preset='fast', x264opts='keyint=15', g='30', ac='2', ar='48000', acodec="aac", audio_bitrate="128000") + ) + + return output.overwrite_output().compile() -def test_fluent_equality(): - pip_to_facebook('351682605501180?s_bl=1&s_ps=1&s_sml=1&s_sw=0&s_vt=api-s&a=AbzMoYVOuxBYwxs7') +def test_rtmp(): + expected_result = ['ffmpeg', '-thread_queue_size', '512', '-vsync', '2', '-f', 'avfoundation', '-framerate', '30', '-pix_fmt', 'uyvy422', '-probesize', '200M', '-i', '1:1', '-f', 'avfoundation', '-framerate', '30', '-pix_fmt', 'uyvy422', '-probesize', '200M', '-s', '320x240', '-i', 'FaceTime:1', '-filter_complex', '[0][1]overlay=eof_action=repeat[s0]', + '-map', '[s0]', '-map', '1:a', '-f', 'flv', '-b:v', '1500000', '-b:a', '128000', '-ac', '2', '-acodec', 'aac', '-ar', '48000', '-g', '30', '-pix_fmt', 'yuv420p', '-preset', 'fast', '-s', '1280x720', '-vcodec', 'libx264', '-vsync', '2', '-x264opts', 'keyint=15', 'rtmps://live-api-s.facebook.com:443/rtmp/input_your_facebook_stream_key_here', '-y'] + # Your facebook key should look like: 123456789012345?s_bl=1&s_ps=1&s_sml=1&s_sw=0&s_vt=api-s&a=AbCdEfGhiJK12345 + your_facebook_key = 'input_your_facebook_stream_key_here' + facebook_stream_url = 'rtmps://live-api-s.facebook.com:443/rtmp/{}'.format(your_facebook_key) + result = pip_to_rtmp(facebook_stream_url) + assert result == expected_result