Skip to content

Instantly share code, notes, and snippets.

@navink07
Forked from BusterNeece/broadcast.sh
Created May 31, 2021 18:51
Show Gist options
  • Save navink07/45f7ac34698a86effe23bbd5ef94848b to your computer and use it in GitHub Desktop.
Save navink07/45f7ac34698a86effe23bbd5ef94848b to your computer and use it in GitHub Desktop.

Revisions

  1. @BusterNeece BusterNeece created this gist May 13, 2017.
    24 changes: 24 additions & 0 deletions broadcast.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #! /bin/bash

    VBR="1500k"
    FPS="30"
    QUAL="veryfast"

    YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2"
    KEY=""

    VIDEO_SOURCE="/home/ubuntu/video3.mp4"
    AUDIO_SOURCE="http://localhost:8000/stream/;stream.mp3"
    NP_SOURCE="/home/ubuntu/nowplaying.txt"

    ffmpeg \
    -re -f lavfi -i "movie=filename=$VIDEO_SOURCE:loop=0, setpts=N/(FRAME_RATE*TB)" \
    -thread_queue_size 512 -i "$AUDIO_SOURCE" \
    -map 0:v:0 -map 1:a:0 \
    -map_metadata:g 1:g \
    -vf drawtext="fontfile=/home/ubuntu/unifont.ttf: fontsize=55: \
    box=0: [email protected]: boxborderw=20: \
    textfile=$NP_SOURCE: reload=1: [email protected]: x=315: y=960" \
    -vcodec libx264 -pix_fmt yuv420p -preset $QUAL -r $FPS -g $(($FPS * 2)) -b:v $VBR \
    -acodec libmp3lame -ar 44100 -threads 6 -qscale:v 3 -b:a 320000 -bufsize 512k \
    -f flv "$YOUTUBE_URL/$KEY"
    81 changes: 81 additions & 0 deletions nowplaying.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <?php
    // Now playing script.

    $start_time = time();

    while(time() < ($start_time + 59)) {

    $np_raw = file_get_contents('http://localhost:8000/stats');
    $np = xml_to_array($np_raw);

    $np_text = $np['SHOUTCASTSERVER']['SONGTITLE'];
    $np_text = wordwrap($np_text, 80);

    if (empty($np_text)) {
    $np_text = 'Funky Way FM';
    }

    atomic_put_contents('nowplaying.txt', $np_text);

    sleep(10);
    }

    function atomic_put_contents($filename, $data)
    {
    file_put_contents($filename.'.new', $data);
    rename($filename.'.new', $filename);
    }

    function xml_to_array($xml)
    {
    $values = $index = $array = [];
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parse_into_struct($parser, $xml, $values, $index);
    xml_parser_free($parser);
    $i = 0;
    $name = $values[$i]['tag'];
    $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
    $array[$name] = _struct_to_array($values, $i);

    return $array;
    }

    function _struct_to_array($values, &$i)
    {
    $child = [];
    if (isset($values[$i]['value'])) {
    array_push($child, $values[$i]['value']);
    }

    while ($i++ < count($values)) {
    switch ($values[$i]['type']) {
    case 'cdata':
    array_push($child, $values[$i]['value']);
    break;

    case 'complete':
    $name = $values[$i]['tag'];
    if (!empty($name)) {
    $child[$name] = ($values[$i]['value']) ? ($values[$i]['value']) : '';
    if (isset($values[$i]['attributes'])) {
    $child[$name] = $values[$i]['attributes'];
    }
    }
    break;

    case 'open':
    $name = $values[$i]['tag'];
    $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
    $child[$name][$size] = _struct_to_array($values, $i);
    break;

    case 'close':
    return $child;
    break;
    }
    }

    return $child;
    }