Skip to content

Instantly share code, notes, and snippets.

@olasd
Created March 28, 2014 19:58
Show Gist options
  • Save olasd/9841772 to your computer and use it in GitHub Desktop.
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"
@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