Skip to content

Instantly share code, notes, and snippets.

@olasd
Created March 28, 2014 19:58
Show Gist options
  • Select an option

  • Save olasd/9841772 to your computer and use it in GitHub Desktop.

Select an option

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"
@2LuckyLuke

Copy link
Copy Markdown

"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

saconni commented Apr 22, 2018

Copy link
Copy Markdown

@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

HaNiTLG commented Sep 11, 2018

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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
Copy Markdown

please help

@TheFreal

Copy link
Copy Markdown

@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

h-o-sein commented Jun 8, 2019

Copy link
Copy Markdown

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

@HiiZun

HiiZun commented Oct 3, 2019

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

how to run this file in ubuntu

@rajeevkrao

Copy link
Copy Markdown

How much resources does it consume???

@rajeevkrao

rajeevkrao commented Apr 20, 2020

Copy link
Copy Markdown

@bentech4u

Copy link
Copy Markdown

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
Copy Markdown

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

@ggrelet

ggrelet commented Apr 4, 2021

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

@adriel

adriel commented Dec 15, 2021

Copy link
Copy Markdown

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

@dedshit

dedshit commented Jan 8, 2022

Copy link
Copy Markdown

@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

dedshit commented Jan 8, 2022

Copy link
Copy Markdown

damn man why the hell you reported my question 🤨🤨

@adriel

adriel commented Sep 11, 2023

Copy link
Copy Markdown

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