From d890fd4beb74429626b3d49b008b645889f8bbe0 Mon Sep 17 00:00:00 2001 From: TheTroll Date: Wed, 17 Mar 2010 13:20:30 +0100 Subject: Implemented startBroacast PHP files cleanup --- bin/backend.php | 44 ++++++------ bin/files.php | 172 +++++++++++++++++++++++++++++++++++++++++++++++ bin/firephp.php | 0 bin/istream.sh | 39 +++++++++++ bin/jsonapi.php | 77 +++++++++++++++++++++ bin/session.php | 118 ++++++++++++++++++++++++++++++++ bin/streaminfo.php | 54 +++++++++++++++ bin/svdrp.php | 0 bin/vdr.php | 111 ++++++++++++------------------ ram/session1/stream.m3u8 | 2 - ram/session1/thumb.png | Bin 1809 -> 0 bytes ram/session2/stream.m3u8 | 2 - ram/session2/thumb.png | Bin 1809 -> 0 bytes ram/session3/stream.m3u8 | 2 - ram/session3/thumb.png | Bin 6123 -> 0 bytes 15 files changed, 527 insertions(+), 94 deletions(-) mode change 100644 => 100755 bin/backend.php create mode 100755 bin/files.php mode change 100644 => 100755 bin/firephp.php create mode 100755 bin/istream.sh create mode 100755 bin/jsonapi.php create mode 100755 bin/session.php create mode 100755 bin/streaminfo.php mode change 100644 => 100755 bin/svdrp.php delete mode 100644 ram/session1/stream.m3u8 delete mode 100644 ram/session1/thumb.png delete mode 100644 ram/session2/stream.m3u8 delete mode 100644 ram/session2/thumb.png delete mode 100644 ram/session3/stream.m3u8 delete mode 100755 ram/session3/thumb.png diff --git a/bin/backend.php b/bin/backend.php old mode 100644 new mode 100755 index c7ff4db..eed0cfd --- a/bin/backend.php +++ b/bin/backend.php @@ -7,39 +7,43 @@ if (file_exists('../config.php')) else include ('../config_default.php'); include ('./utils.php'); +include ('./files.php'); +include ('./streaminfo.php'); include ('./vdr.php'); +include ('./session.php'); +include ('./jsonapi.php'); $action=$_REQUEST['action']; switch ($action) { case ("getGlobals"): - $tree = getGlobals(); - print $tree; - break; + $tree = getGlobals(); + print $tree; + break; case ("getTvCat"): - $tree = getTvCat(); - print $tree; - break; + $tree = getTvCat(); + print $tree; + break; case ("getFullChanList"): - $tree = getFullChanList(); - print $tree; - break; + $tree = getFullChanList(); + print $tree; + break; case ("getTvChan"): - $tree = GetTvChan($_REQUEST['cat']); - print $tree; - break; + $tree = GetTvChan($_REQUEST['cat']); + print $tree; + break; case ("getChanInfo"): - $tree = getChanInfo($_REQUEST['chan']); - print $tree; - break; + $tree = getChanInfo($_REQUEST['chan']); + print $tree; + break; case ("getRecInfo"): $tree = file_get_contents("textfiles/getRecInfo.txt"); - print $tree; + print $tree; break; case ("getVidInfo"): @@ -54,11 +58,9 @@ switch ($action) break; case ("startBroadcast"): - $type = $_REQUEST['type']; - $url = $_REQUEST['url']; - $tree = file_get_contents("textfiles/startBroadcast-" . $type . ".txt"); - print $tree; - break; + $tree = startBroadcast($_REQUEST['type'], $_REQUEST['url'], $_REQUEST['mode']); + print $tree; + break; case ("stopBroadcast"): $tree = file_get_contents("textfiles/stopBroadcast.txt"); diff --git a/bin/files.php b/bin/files.php new file mode 100755 index 0000000..7dc5baf --- /dev/null +++ b/bin/files.php @@ -0,0 +1,172 @@ +analyze($stream); + + $title = "Media:"; + $info = "Duration: " .sec2hms($fileinfo['playtime_seconds']) ."
"; + if ($fileinfo['fileformat']) + $info .= "Format: " .$fileinfo['fileformat'] ."
"; + if ($fileinfo['video']['codec']) + $info .= "Video: " .$fileinfo['video']['codec'] ."
"; + if ($fileinfo['audio']['codec']) + $info .= "Audio: " .$fileinfo['audio']['codec'] ."
"; + if ($fileinfo['video']['resolution_x']) + $info .= "Resolution: " .$fileinfo['video']['resolution_x'] ."x" .$fileinfo['video']['resolution_y'] ."
"; + + return array($title, $info); +} + +function mediagentb($stream, $dest) +{ + global $ffmpegpath; + + // Get info + $getid3 = new getID3; + $fileinfo = $getid3->analyze($stream); + + exec("rm " .$dest); + $path = dirname($stream); + + if (file_exists(substr($stream, 0, -4) .".tbn")) + $file = substr($stream, 0, -4) .".tbn"; + else if (file_exists($path ."/poster.jpg")) + $file = $path ."/poster.jpg"; + else if (file_exists($path ."/folder.jpg")) + $file = $path ."/folder.jpg"; + else + $file = ""; + + $resx = 180; + $resy = 100; + + if ($file) + { + $getid3 = new getID3; + $fileinfo = $getid3->analyze($file); + } + + if ($fileinfo['video']['resolution_y'] && $fileinfo['video']['resolution_x']) + { + if ($fileinfo['video']['resolution_y'] < $fileinfo['video']['resolution_x']) + { + $resx = 180; + $resy = round(($fileinfo['video']['resolution_y'] * 180) / $fileinfo['video']['resolution_x']); + } + else + { + $resx = round (($fileinfo['video']['resolution_x'] * 100) / $fileinfo['video']['resolution_y']); + $resy = 100; + } + } + + if ($file) + exec("cp \"" .$file ."\" ram/stream-tb-tmp.jpg; " .$ffmpegpath ." -y -i ram/stream-tb-tmp.jpg -s " .$resx ."x" .$resy ." " .$dest ." ; rm ram/stream-tb-tmp.jpg"); + else + exec($ffmpegpath ." -y -i \"" .$stream ."\" -an -ss 00:00:05.00 -r 1 -vframes 1 -s " .$resx ."x" .$resy ." -f mjpeg " .$dest); + + if (!file_exists($dest)) + exec('cp logos/nologoMEDIA.png ' .$dest); +} + +function mediagetwidth($file) +{ + + $getid3 = new getID3; + $fileinfo = $getid3->analyze($file); + + return $fileinfo['video']['resolution_x']; +} + +function mediagettype($file) +{ + global $videotypes, $audiotypes; + + // Get file extension + $fileext = end(explode(".", $file)); + $file = str_replace("\\'", "'", $file); + + if (is_dir($file)) + return 3; + else if (preg_match("$/$", $fileext)) + return 0; + else if (preg_match("/" .$fileext ." /", $videotypes)) + return 'tv'; + else if (preg_match("/" .$fileext ." /", $audiotypes)) + return 'rec'; + else + return 'vid'; +} + +function mediadirhasaudio($dir) +{ + global $audiotypes; + + $audioextarray = explode(' ', $audiotypes); + + foreach ($audioextarray as $num => $audioext) + { + if (glob($dir .'*.' .$audioext)) + return 1; + } + + return 0; +} + +function mediagetmusicinfo($file ="") +{ + // Get info + $getid3 = new getID3; + $fileinfo = $getid3->analyze($file); + + $name = $fileinfo['tags']['id3v2']['title'][0]; + if ($name == "") + { + $name = $fileinfo['tags']['id3v1']['title'][0]; + if ($name == "") + { + $name = $fileinfo['filename']; + if ($name == "") + $name = "unknown"; + } + } + + if (!is_utf8($name)) + $name = utf8_encode($name); + + $duration = $fileinfo['playtime_string']; + + return array ($name, $duration); +} + +function generatelogo($type, $name, $dest) +{ + switch ($type) + { + case 'tv': + $channoslash = preg_replace("$/$", " ", $name); + $logopath = "logos/" .$channoslash .".png"; + if (!file_exists($logopath)) + $logopath = "logos/nologoTV.png"; + exec("cp \"" .$logopath ."\" " .$dest); + break; + case 'rec': + $channoslash = preg_replace("$/$", " ", $name); + $logopath = "logos/" .$channoslash .".png"; + if (!file_exists($logopath)) + $logopath = "logos/nologoREC.png"; + exec("cp \"" .$logopath ."\" " .$dest); + break; + case 'vid': + // Generate TB + mediagentb($name, $dest); + break; + } +} + +?> diff --git a/bin/firephp.php b/bin/firephp.php old mode 100644 new mode 100755 diff --git a/bin/istream.sh b/bin/istream.sh new file mode 100755 index 0000000..6c0985f --- /dev/null +++ b/bin/istream.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +STREAM=$1 +VRATE=$2 +ARATE=$3 +ACHANNELS=$4 +XY=$5 +HTTP_PATH="$6ram/" + +SEGDUR=10 # Length of Segments produced (between 10 and 30) +SEGWIN=$7 # Amount of Segments to produce +FFPATH=$8 +SEGMENTERPATH=$9 +SESSION=${10} + +if [ $# -eq 0 ] +then +echo "Format is : ./istream.sh source video_rate audio_rate audio_channels 480x320 httppath segments_number ffmpeg_path segmenter_path" +exit 1 +fi + +############################################################# +# start dumping the TS via Streamdev into a pipe for ffmpeg +# and store baseline 3.0 mpegts to outputfile +# sending it to the segmenter via a PIPE +############################################################## + +cd ../ram/$SESSION + +2> /dev/null rm stream*.ts + +# Shutdown FTP +/usr/local/bin/fwnoftp + +2> /dev/null $FFPATH -i "$STREAM" -deinterlace -f mpegts -acodec libmp3lame -ab $ARATE -ac $ACHANNELS -s $XY -vcodec libx264 -b $VRATE -flags +loop \ + -cmp \+chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 \ + -sc_threshold 40 -i_qfactor 0.71 -bt $VRATE -maxrate $VRATE -bufsize $VRATE -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 \ + -qmin 10 -qmax 51 -qdiff 4 -level 30 -g 30 -async 2 -threads 4 - | \ +$SEGMENTERPATH - $SEGDUR stream stream.m3u8 $HTTP_PATH$SESSION/ $SEGWIN & diff --git a/bin/jsonapi.php b/bin/jsonapi.php new file mode 100755 index 0000000..35ec623 --- /dev/null +++ b/bin/jsonapi.php @@ -0,0 +1,77 @@ + diff --git a/bin/session.php b/bin/session.php new file mode 100755 index 0000000..8b998d2 --- /dev/null +++ b/bin/session.php @@ -0,0 +1,118 @@ + $qp) + { + if ($qn == $mode) + { + $qparams = $qp; + break; + } + } + + // Create session + exec('mkdir ../ram/' .$session); + $url = str_replace("\\'", "'", $url); + switch ($type) + { + case 'tv': + $cmd = "export SHELL=\"/bin/sh\";printf \"./istream.sh \\\"" .$url ."\\\" " .$qparams ." " .$httppath ." 2 " .$ffmpegpath ." " .$segmenterpath ." " .$session ." \" | at now"; + break; + case 'rec': + $cmd = "export SHELL=\"/bin/sh\";printf \"cat \\\"" .$url ."\\\"/0* | ./istream.sh - " .$qparams ." " .$httppath ." 1260 " .$ffmpegpath ." " .$segmenterpath ." " .$session ." \" | at now"; + break; + case 'vid': + $cmd = "export SHELL=\"/bin/sh\";printf \"./istream.sh \\\"" .$url ."\\\" " .$qparams ." " .$httppath ." 1260 " .$ffmpegpath ." " .$segmenterpath ." " .$session ." \" | at now"; + break; + default: + $cmd = ""; + } + + $cmd = str_replace('%', '%%', $cmd); + exec ('echo "' .$cmd .'" > /tmp/a'); + exec ($cmd); + + // Extract $channame if needed + switch ($type) + { + case 'tv': + $urlarray = explode("/", $url); + $channum = $urlarray[count($urlarray)-1]; + $channame = vdrgetchanname($channum); + break; + case 'rec': + list($channame, $title, $desc) = vdrgetrecinfo($url); + break; + default: + $channame = ""; + break; + } + + // Write streaminfo + writeinfostream($session, $type, $mode, $url, $channame); + + // Create logo + if ($type == 'vid') + generatelogo($type, $url, '../ram/' .$session .'/logo.png'); + else + generatelogo($type, $channame, '../ram/' .$session .'/logo.png'); + + return $session; +} + +function sessiondelete($session) +{ + if ($session == 'all') + { + $dir_handle = @opendir('../ram/'); + if ($dir_handle) + { + while ($session = readdir($dir_handle)) + { + if($session == "." || $session == ".." || $session == 'lost+found') + continue; + + if (!is_dir('../ram/' .$session)) + continue; + + // Get info + list($type, $mode, $url, $channame) = readinfostream($session); + + if ($type) + sessiondeletesingle($session); + } + } + } + else + return sessiondeletesingle($session); +} + +function sessiondeletesingle($session) +{ + $ram = "../ram/" .$session ."/"; + + // Get segmenter PID if any + if (file_exists($ram ."segmenter.pid")) + $cmd = "/usr/local/bin/fw;kill `cat " .$ram ."segmenter.pid`; rm " .$ram ."segmenter.pid; "; + + $cmd .= "rm -rf " .$ram; + exec ($cmd); +} + +?> diff --git a/bin/streaminfo.php b/bin/streaminfo.php new file mode 100755 index 0000000..f102226 --- /dev/null +++ b/bin/streaminfo.php @@ -0,0 +1,54 @@ + diff --git a/bin/svdrp.php b/bin/svdrp.php old mode 100644 new mode 100755 diff --git a/bin/vdr.php b/bin/vdr.php index fc03c12..0b401a8 100755 --- a/bin/vdr.php +++ b/bin/vdr.php @@ -1,73 +1,6 @@