Skip to content

Instantly share code, notes, and snippets.

@olasd
Created March 28, 2014 19:58
Star You must be signed in to star a gist
Save olasd/9841772 to your computer and use it in GitHub Desktop.
Stream video to youtube via ffmpeg
#! /bin/bash
#
# Diffusion youtube avec ffmpeg
# Configurer youtube avec une résolution 720p. La vidéo n'est pas scalée.
VBR="2500k" # Bitrate de la vidéo en sortie
FPS="30" # FPS de la vidéo en sortie
QUAL="medium" # Preset de qualité FFMPEG
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2" # URL de base RTMP youtube
SOURCE="udp://239.255.139.0:1234" # Source UDP (voir les annonces SAP)
KEY="...." # Clé à récupérer sur l'event youtube
ffmpeg \
-i "$SOURCE" -deinterlace \
-vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:v $VBR \
-acodec libmp3lame -ar 44100 -threads 6 -qscale 3 -b:a 712000 -bufsize 512k \
-f flv "$YOUTUBE_URL/$KEY"
@dokomade9921
Copy link

@p4ulolol how to keep script repeatedly? for example 1 folder there are 10 videos. then 10 videos are playing and will be played back until I stopped ffmpeg process by myself. How ?

@Cyxo
Copy link

Cyxo commented Oct 18, 2017

@dokomade9921 well I don't really know. You can either add an infinite while loop arround the ffmpeg command, but I think you won't be ablento stop it until you stop the script using a command from another terminal. Else you can add a while loop or for loop around these lines :

for f in $FOLDER/*.mp4
...
done

with a lot of iterations, that should generate a very long playlist, probably longer than you need. But in this case I don't know what is the character limit of a variable or of a command if there is one.

That's also why I hope people will improve it 🙂
By the way people, you can fork it right here (and also comment for help, if you don't want to flood this gist) :
https://gist.github.com/p4ulolol/e4192f58e738446f3491d839eaa50ed7

@dokomade9921
Copy link

@p4ulolol I also can not overcome this. because I do not understand the script so I am confused. so can you load this script to run on linux?

: Batch Render Youtube Video Reup
: Settings for x264 video and mp3 Audio

@echo OFF
Setlocal EnableDelayedExpansion

: path to ffmpeg
set ffmpeg=C:\FMPEG\bin\ffmpeg.exe

: path to ffmpeg
set INPUT=C:\input
set OUTPUT=C:\output

: encode video
for %%i in (*.mp4) DO "%ffmpeg%" -re -i "%%i" -vcodec libx264 -preset veryfast -maxrate 2500k -bufsize 3368k -vf "format=yuv420p" -g 60 -acodec libmp3lame -b:a 198k -ar 44100 -metadata title="" -metadata artist="" -metadata album_artist="" -metadata album="" -metadata date="" -metadata track="" -metadata genre="" -metadata publisher="" -metadata encoded_by="" -metadata copyright="" -metadata composer="" -metadata performer="" -metadata TIT1="" -metadata TIT3="" -metadata disc="" -metadata TKEY="" -metadata TBPM="" -metadata language="eng" -metadata encoder="" -f flv -s 1280x720 "rtmp://a.rtmp.youtube.com/live2/3xd5-9yeg-mw0z-29k7"

please help me so i can make sh file for linux. because i am a linux user not windows.

@Cyxo
Copy link

Cyxo commented Oct 19, 2017

@dokomade9921 Well your script does almost the same thing as mine, just in different way. All you have to do is to set FOLDER to ".". In any case, your script won't keep the live stream alive forever, it will only loop once through all the files and play them (I even wonder if it doesn't stop the stream and restart it between each file, which is not great)

@Rietveldd
Copy link

Mine keeps saying the stream is starting but never does. when i look at the "health" of the YouTube stream it says its not receiving data. at the start of the stream the "health" of it is green the a short time later is gray. Any idea on how to fix this?

@n4hte
Copy link

n4hte commented Dec 4, 2017

what is the source for the screen to be casted or is it default

@robmorin
Copy link

What about if i want to use my shoutcast radio stream, rather than a video source or file source? Is this possible?

@amankumarjain
Copy link

Hi, my source url is another rtmp server, how to send stream to youtube from another rtmp server, please help.

ffmpeg -i rtmp://{url}/live/ -f flv rtmp://a.rtmp.youtube.com/live2/key is not working

@2LuckyLuke
Copy link

"Mine keeps saying the stream is starting but never does. when i look at the "health" of the YouTube stream it says its not receiving data. at the start of the stream the "health" of it is green the a short time later is gray. Any idea on how to fix this?"
@Rietveldd

Same problem here. Can someone help?

@saconni
Copy link

saconni commented Apr 22, 2018

@2LuckyLuke I had the same problem and I realized that, in my case, youtube didn't like non-audio streams. If you are using a non-audio source (screen capturing, or testsrc) add a null audio source. These are my ffmpeg arguments (I'm on windows):

ffmpeg ^
  -re -f lavfi -i testsrc=size=768x432:rate=30 -deinterlace ^
  -f lavfi -i anullsrc ^
  -vcodec libx264 -pix_fmt yuv420p -preset veryfast -r 30 -g 60 -b:v "1000k" ^
  -acodec libmp3lame -ar 44100 -b:a 128k ^
  -f flv "rtmp://a.rtmp.youtube.com/live2/key"

@HaNiTLG
Copy link

HaNiTLG commented Sep 11, 2018

Why is the speed of the stream so high?
The speed is always 3-5 times faster than normal, how can I fix this?

@alexey-v-paramonov
Copy link

I wonder if there is away to hide YouTube key from the command line. Running ffmpeg like this can make your key visible to other system users, they can do ps -Af and see your key (if they have enough privileges on the system) in the process list.
A possible solution could be reading the target YouTube url from a file, is it possible?

@joseph488
Copy link

hi i need to know how to stream or go live to youtube with a downloaded or recorded video 24*7 but without turning the computer on for 24 hours (i.e offline)

@joseph488
Copy link

please help

@TheFreal
Copy link

@joseph488 This thread already has all you need. If you don't want to run the stream from your computer 24/7, get a VPS and run the script from there.

@h-o-sein
Copy link

h-o-sein commented Jun 8, 2019

Stream no sound. How can I fix this problem? @olasd

@HiiZun
Copy link

HiiZun commented Oct 3, 2019

i have try the code of @Cyxo but i have a error a anybody to help me ?

Please use -q:a or -q:v, -qscale is ambiguous
Stream mapping:
Stream #0:0 (h264) -> concat:in0:v0
Stream #0:1 (aac) -> concat:in0:a0
Stream #1:0 (h264) -> concat:in1:v0
Stream #1:1 (aac) -> concat:in1:a0
concat:out:v0 -> Stream #0:0 (libx264)
concat:out:a0 -> Stream #0:1 (libmp3lame)
Press [q] to stop, [?] for help
[Parsed_concat_0 @ 0x55652e0b4ca0] Input link in1:v0 parameters (size 1920x1080, SAR 1:1) do not match the corresponding output link in0:v0 parameters (640x480, SAR 1:1)
[Parsed_concat_0 @ 0x55652e0b4ca0] Failed to configure output pad on Parsed_concat_0
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #1:0
Conversion failed!

edit: i have gess !

@storytellerr
Copy link

hey @Cyxo

Hey everyone ( @dedaloodak , @shikagemew ) ! For those who want to make a youtube radio, I made a little script which plays all .mp4 files in a folder.
For a radio, you could heavily imporve it by randomizing the order of the files in the for loop (I don't know if it's feasible), or by having a continuous stream with a fifo or something (I don't master it really well). By the way, here is where I found the trick : https://trac.ffmpeg.org/wiki/Concatenate#filter

Here is the script :

#! /bin/bash
#
# Diffusion youtube avec ffmpeg

# Configurer youtube avec une résolution 720p. La vidéo n'est pas scalée.

VBR="2500k"                                    # Bitrate de la vidéo en sortie
FPS="30"                                       # FPS de la vidéo en sortie
QUAL="medium"                                  # Preset de qualité FFMPEG
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2"  # URL de base RTMP youtube

FOLDER="videos"                                    # Dossier source
KEY="PUT YOUR KEY THERE"                                     # Clé à récupérer sur l'event youtube

SOURCE=""
n=0
filter=""

for f in $FOLDER/*.mp4
do
  SOURCE="$SOURCE -i $f"
  filter="$filter [$n:v:0] [$n:a:0]"
  ((n++))
done

filter="$filter concat=n=$n:v=1:a=1 [v] [a]"

echo "ffmpeg $SOURCE -filter_complex '$filter'"

ffmpeg \
    $SOURCE -filter_complex "$filter" \
-map "[v]" -map "[a]" -deinterlace \
    -vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:v $VBR \
    -acodec libmp3lame -ar 44100 -threads 6 -qscale 3 -b:a 712000 -bufsize 512k \
    -f flv "$YOUTUBE_URL/$KEY"

Let me know if you do any improvement, I'll be glad to use them

i am getting this error
[concat @ 0x5573f4b41040] Value 0.000000 for parameter 'n' out of range [1 - 2.14748e+09] Last message repeated 1 times [concat @ 0x5573f4b41040] Error setting option n to value 0. [Parsed_concat_0 @ 0x5573f4b2f240] Error applying options to the filter. [AVFilterGraph @ 0x5573f4b24de0] Error initializing filter 'concat' with args 'n=0:v=1:a=1' Error initializing complex filters. Numerical result out of range

@likhith11
Copy link

how to run this file in ubuntu

@rajeevkrao
Copy link

How much resources does it consume???

@rajeevkrao
Copy link

rajeevkrao commented Apr 20, 2020

@bentech4u
Copy link

HI

is it possible to set my source as another live video? eg: live video url from Facebook something like https://www.facebook.com/groupname/videos/590993838218530/

@ldijkman
Copy link

looks cool
but cannot get it going yet
restream video from
http://Arduino.TK

@ggrelet
Copy link

ggrelet commented Apr 4, 2021

is it possible to set my source as another live video? eg: live video url from Facebook something like https://www.facebook.com/groupname/videos/590993838218530/

Interested by any answer too, I tried with piping from youtube-dl with no luck.

@BitcoinJake09
Copy link

is it possible to set my source as another live video? eg: live video url from Facebook something like https://www.facebook.com/groupname/videos/590993838218530/

Interested by any answer too, I tried with piping from youtube-dl with no luck.

im here looking for answers too, im currently trying to write code using FFMPEG & youtube-dl, and i seem to have that part ok, but i really want to stream from a URL like @bentech4u wants to do also.. hopefully someone in this community can figure it out. best of luck all.

@faraamcode
Copy link

Please how can I stream a website URL (like an ongoing video conference url ) to youtube using FFmpeg

@adriel
Copy link

adriel commented Dec 15, 2021

@faraamcode you could (if supported) first get the feed down via streamlink, then pipe it into ffmpeg.

@dedshit
Copy link

dedshit commented Jan 8, 2022

@bentech4u Copy M3U8 from facebook live thats all also if both video , audio streams are libx264, aac respectively then no need for re-encoding stream to youtube rtmp

if not then you need to encode codecs suggested by youtube

@dedshit
Copy link

dedshit commented Jan 8, 2022

damn man why the hell you reported my question 🤨🤨

@adriel
Copy link

adriel commented Sep 11, 2023

It can be as simple as ffmpeg -i "<YOUR_SOURCE>" -f flv "rtmp://a.rtmp.youtube.com/live2/<YOUR_STREAM_KEY>"

The other stuff just adds extra control. You may or may not need it depending on your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment