Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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"

fahhu commented Aug 15, 2015

hi, great solution! But i'm don't understand the "Source" parameter..? I would stream from my local webcam to the youtube life stream. maybe you can help me with a little tip? thanks

MaoX17 commented Oct 28, 2015

Hi olasd,
thank you for your great solution.
I've tried your solution, but my streaming start after about 15-30 seconds and stop before the end of the video that i use as source.
Can you tell me how to solve?
Thank you very much

Hi olasd,
What is the use of this file .
Can you explain me,,

@fahhu
The "Source" parameter in this specific example is a UDP multicast stream (likely from another FFMPEG enabled device)
If you would like to use another source such as your desktop see the FFMPEG guide on streaming https://trac.ffmpeg.org/wiki/Capture/Desktop
Note: -i is the operator specific the input device or source

@MaoX17
What is the upload and download speed on the connection you are using. You can run a test over at SpeedTest.net to find out.

@karthigamahe
This is a script for a unix based computer (likely Linux), to send a UDP multicast stream to YouTube.

pstoll commented Feb 27, 2016

fwiw recent version of ffmpeg now complain about that command line. you need to modify -qscale to -qscale:v. It complains about -qscale being ambiguous.

What if the source is a local usb webcam?
Or what if it is a local video file?

smac89 commented Jun 22, 2016

@fahhu, to stream from webcam, set the SOURCE to this

SOURCE='/dev/video0'

If you stream from a file, use the -re parameter. I didn't and having stuff going faster than they should is kinda awkward, especially if your stream includes a countdown.

fanks :)

it works with m3u8 as well .

9xcoder commented Dec 28, 2016

Good, worked with me
i want stream a playlist video on server, can you help me change this script ?
Thanks

Very grateful for your contribution.
And I managed to broadcast to Facebook Live

Nice, it work.
Thanks!

hohaidang commented Mar 11, 2017

Hello,
I downloaded your source code and changed a little bit as fit as my require. But in youtube website just show "It's working" and just have a black monitor I was waiting for a long time and no difference. Please help me. Something wrong?

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
URL="http://localhost:8090/feed1.ffm"
SOURCE="/home/hohaidang/Helicopter_DivXHT_ASP.divx"              # Source UDP (voir les annonces SAP)
KEY="291d-5hz9-fkku-8jvc"                                     # 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"

@hohaidang You should edit your post to remove the youtube key. Posting the youtube key will allow others to stream to your youtube account.

Also, are you running the ffmpeg stream through ffserver? ("URL="http://localhost:8090/feed1.ffm")

brrrrain commented Apr 6, 2017

Thanks! It's very helpful!

quky commented Aug 13, 2017

thanks for the script how can i stop it ones i got it running.

'q' to quit works for me.

Hi everyone,
Can you help me? I have a question.
I need to live stream some videos in 1 folder on youtube by ffmpeg.
This is code live all video (*.mp4) in 1 folder
But I need live random video all of them, and loop forever to live stream 24/7 .
For example: with 4 video ( 3-2-4-1 and loop 2-1-3-4 and loop 1-4-3-2,.....)

@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 1280×720 “rtmp://a.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx”

I need also way to stream multiple videos :/

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

@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 ?

@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

@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.

@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)

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?

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