scivision / PyLivestream

Pure Python FFmpeg-based live video / audio streaming to YouTube, Facebook, Periscope, Twitch, and more
Apache License 2.0
710 stars 157 forks source link

Possible to use this for radio station? #1

Closed ghost closed 6 years ago

ghost commented 6 years ago

code below is candidate code for broadcasting multiple mp4 to youtube live

#! /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"

from: https://gist.github.com/olasd/9841772

Is it possible to use ffmpeg-youtube-live instead?

scivision commented 6 years ago

How about https://github.com/scivision/ffmpeg-youtube-live/blob/master/FileGlob2YouTubeLive.py

ghost commented 6 years ago

@scivision Awesome. I can't wait to use that :) Will it be possible to stream just mp3 files?

For now, I am using Icecast to stream my mp3 files, and use ffmpeg to send youtube live. avconv -loop 1 -i /MUSIC/xt-hd.png -i http://localhost:8080/mp3 -r 30 -acodec aac -strict -2 -c:v libx264 -strict experimental -b:a 128k -pix_fmt yuvj444p -b:v 256k -minrate 128k -maxrate 512k -bufsize 768k -f flv rtmp://a.rtmp.youtube.com/live2/[KEY]

From: http://akos.me/2017/stream-icecast-stream-to-youtube/

Or I am trying to implement this too with Icecast

ffmpeg -re -loop 1 -framerate 2 -i test1.jpg -i https://xxxxxxxxxxx:8080/mp3 -c:a aac -s 2560x1440 -ab 128k -vcodec libx264 -pix_fmt yuv420p -maxrate 2048k -bufsize 2048k -framerate 30 -g 2 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/[KEY]

I can't determine which way would be more stable.. Can i ask you how do you think about this?

scivision commented 6 years ago

I am tied up till later this month. I'll try to run test live stream of several such cases on Windows and Linux.

Basically you can think of the Python script as a command generator--it computes the "best" options or at least "good" options for several different YouTube Live scenarios, optionally running ffmpeg or avconv for you with those options.

ghost commented 6 years ago

@scivision Good. It would be awesome to create my own stream channel with just one simple command! let me know if there is what I can :)

scivision commented 6 years ago

yes I agree that's the goal of the program, to find the best command for your given computer configuration.

ghost commented 6 years ago

Hi again! I have used your code but seems not working :(

root@SERVER:~/radio# python3.6 FileGlob2YouTubeLive.py "/root/radio" "*.mp3"
streaming to YouTube Live these files. Be sure list is correct!

/root/radio/5.mp3
/root/radio/9.mp3
/root/radio/3.mp3
/root/radio/2.mp3
/root/radio/8.mp3
/root/radio/1.mp3
/root/radio/7.mp3
/root/radio/4.mp3
/root/radio/6.mp3
YouTube Live Stream ID:

 ffmpeg -i /root/radio/5.mp3 -f pulse -ac 2 -i default -vcodec libx264 -pix_fmt yuv420p -preset veryfast -b:v 1500k -g 60 -acodec libmp3lame -ar 48000 -threads 0 -bufsize 3000k -f flv

ffmpeg version 3.4-4~16.04.york0 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.5) 20160609
  configuration: --prefix=/usr --extra-version='4~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
[mp3 @ 0x7f6afed888a0] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '/root/radio/5.mp3':
  Metadata:
    album           : TEst
    artist          : Test
    album_artist    : Test
    comment         : cccTest
    composer        : Test
    disc            : 1/1
    genre           : Dance
    title           : Test
    track           : 01/01
    date            : 2013
  Duration: 00:03:10.67, start: 0.000000, bitrate: 324 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
    Stream #0:1: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 511x463 [SAR 96:96 DAR 511:463], 90k tbr, 90k tbn, 90k tbc
    Metadata:
      comment         : Cover (front)
default: No such process
Traceback (most recent call last):
  File "FileGlob2YouTubeLive.py", line 40, in <module>
    youtubelive(P)
  File "/root/ffmpeg-youtube-live/youtubelive_ffmpeg/__init__.py", line 160, in youtubelive
    stdout=sp.DEVNULL)
  File "/usr/lib/python3.6/subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['ffmpeg', '-i', '/root/radio/5.mp3', '-f', 'pulse', '-ac', '2', '-i', 'default', '-vcodec', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'veryfast', '-b:v', '1500k', '-g', '60', '-acodec', 'libmp3lame', '-ar', '48000', '-threads', '0', '-bufsize', '3000k', '-f', 'flv', 'rtmp://a.rtmp.youtube.com/live2/', 'here-yout-ubec-ode']' returned non-zero exit status 1.
root@SERVER:~/radio# ls
1.mp3  2.mp3  3.mp3  4.mp3  5.mp3  6.mp3  7.mp3  8.mp3  9.mp3  FileGlob2YouTubeLive.py

What would be the problem?

scivision commented 6 years ago

The code has been updated to handle several more use cases such as yours. Now with the latest code, you can stream a bunch of audio-only .mp3 files. You do need to pick a static image (say sunrise.jpg or mylogo.png etc.) as a background

You would type

python FileGlob2YouTubeLive.py /root/radio "*.mp3" -i mylogo.jpg
ghost commented 6 years ago

@scivision Just can't wait to use that! I'll try it soon and feedback for you. thanks for your works and keep moving!

scivision commented 6 years ago

Great! You can also add the -loop option to endlessly repeat the globbed list of files (endless live stream).

ghost commented 6 years ago

@scivision And always Thanks for your code! I finally tested new code here, and in conclusion, it is working quite nice. But I got some few weird situation. One of them was that at some point, it just keeps loop at a specific range (after nice stream period almost 2hr), but youtube showing green status bar. The FFmpeg's encoding speed was 3x, so I thought that was the problem. What I did to fix that was changing thumbnail to more big one (to reduce encoding speed), and get rid of DVR option from youtube live dashboard. And now It's going quite well, almost 24hr. but as the low encoding speed, sometimes it's quite laggy. I think I should find some ways to stream with fast encoding speed but without DVR for now 🤔

scivision commented 6 years ago

@transifex3 this issue with excessive encoding speed was recently solved.

amith-vp commented 3 years ago

Now with the latest code, you can stream a bunch of audio-only .mp3 files. You do need to pick a static image (say sunrise.jpg or mylogo.png etc.) as a background

Any way to stream multiple mp3 files (loop) with the animated background? I can't loop music with the animated backgroud.