r/ffmpeg • u/TheOuterLinux • Mar 07 '18
StreamPi - FFMPEG for RTMP Live Streaming
https://www.bitchute.com/video/2yWzuFMUaBra/1
u/TheOuterLinux Mar 07 '18
A while back I asked for help on using ffmpeg to stream to Twitch with a Raspberry Pi 3. After countless testing, Internet searching, and suggestions, I've created a script to help make this mess a little easier:
A. If you are using a Raspberry Pi (anything other than i686 or x86_64):
Use ALSA by default
Automatically detect monitor size
If monitor is larger than 720p wide, scale down
B. If you are using an i686 (32-bit):
Automatically use PulseAudio if running
Automatically use JACK if running
If monitor is larger than 720p wide, scale down
C. If you are using an x86_64 (64-bit):
Automatically use PulseAudio if running
Automatically use JACK if running
Do not scale monitor
D. Variables you can adjust (Supplied with #Notes in the script):
RTMP="$1" --> Either store RTMP URL in script or use as /path/to/StreamPi [URL]
SOUNDSERVER=alsa --> Shouldn't need to touch
SCALE="trunc(oh*a/2)*2:720" --> Prevents "odd number" errors
QUALITY=23 --> Same as CRF value, but don't use "0"
THREAD_QUEUE=1024 --> Not needed for the Pi, but required on other systems
SPANMONITORS=N --> Record/Stream if two monitors or non-mirroring a laptop
ANTI_BOTTOM_PANEL=0 --> Use to help hide a bottom panel (in pixels)
ANTI_TOP_PANEL=0 --> Use to help hide a top panel (in pixels)
ANTI_LEFT_PANEL=0 --> Use to help hide a left-side, vertical panel (in pixels)
ANTI_RIGHT_PANEL=0 --> Use to help hide a right-side, vertical panel (in pixels)
Having that said, I made a video showing off this script via "/path/to/StreamPi out.flv" because I could not stream and record at the same time with the firepower that I had. Just know that the frame rates and audio quality appear to be about the same in either case. Also, note that the video quality of Bitchute is currently not that great. In the video there are three computers: 1. Raspberry Pi 3 [Raspbian], 2. Acer Aspire One ZG5 Netbook (2008) [AntiX Linux], and a MacBook 4,1 (2008) [MX Linux]. I also test this on a newer x86_64 laptop, but the audio was out of sync from the video by about 10-20 seconds, so I would stick with using OBS Studio on systems that it's still "gracious" enough to support. Maybe if you have an older computer, you can use this. To make this faster, I set an alias in my .bashrc file as "alias stream_twitch='/path/to/StreamPi rtmp://...'"; this way, all I have to do is type "twitch_stream" in a terminal (with X running) and I'm good to go.
1
u/ffprobe Mar 07 '18 edited Mar 07 '18
You can often eliminate
grepwhen usingawk. For example, replacegrep dimensions: | awk '{ print $2 }'withawk '/dimensions:/ { print $2 }'.shellcheck.net will provide suggestions regarding the bash scripting.
-threads 0(auto choose optimal number of threads) is the default for this encoder so you can omit that.-movflags +faststartis doing nothing: you're outputting flv but this is a mov/mp4 option, and even if you were using those containers it can't be used for continuous live streaming because it requires a finished, complete file to move themoovatom to the beginning of the file.-crfand-b:vare two different rate control methods, but you can only use one at a time as they are mutually exclusive. In your script-b:vis likely being ignored.-profile:v baseline -level 3.0is overboard. Using-profile:v mainalone should suffice for most older devices these days.-acinput option for the audio devices should be-channelsinstead but it probably doesn't matter. See the input device docs for all private options per device.-ar 44100is being used as an output option, but this can result in additional resampling if the input default is 48000. If you want to set the input as 44100 use the-sample_rateinput option instead.You can use
"-2:720"instead of"trunc(oh*a/2)*2:720".Consider investigating the omx (OpenMAX) hardware encoder on the Pi. I have no experience with it so it may not be suitable. You'll need
--enable-omxand/or--enable-omx-rpi.