add dead simple transcoding with ffmpeg
authorChristian Weiske <cweiske@cweiske.de>
Wed, 25 Nov 2015 23:34:10 +0000 (00:34 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 25 Nov 2015 23:34:10 +0000 (00:34 +0100)
.gitignore
src/mediatomb.php
www/index.php

index b0e72d9d31566912ca942ece608870e5cbd79964..21c789527625092ceb60cf2f5e9c88131b34c33f 100644 (file)
@@ -3,3 +3,4 @@
 /var/podcasts/*
 /README.html
 /data/config.php
 /var/podcasts/*
 /README.html
 /data/config.php
+/www/cache/
index e8bbfcb9d9c97b26c8b55d47125df186f1390f6a..11ffe5ad42f64a1171edda74df4f9bd0166998c1 100644 (file)
@@ -3,7 +3,7 @@ require_once 'Services/MediaTomb.php';
 
 function handleRequestMediatomb($fullPath, $prefix)
 {
 
 function handleRequestMediatomb($fullPath, $prefix)
 {
-    global $mediatomb;
+    global $mediatomb, $host1;
 
     extract($mediatomb);
     try {
 
     extract($mediatomb);
     try {
@@ -23,14 +23,16 @@ function handleRequestMediatomb($fullPath, $prefix)
 
         foreach ($container->getItemIterator(false) as $item) {
             $di = $item->getDetailedItem();
 
         foreach ($container->getItemIterator(false) as $item) {
             $di = $item->getDetailedItem();
+            $itemUrl = $item->url;
             if ($di->mimetype !== 'audio/mpeg') {
                 //noxon iRadio cube does not want to play .ogg files
             if ($di->mimetype !== 'audio/mpeg') {
                 //noxon iRadio cube does not want to play .ogg files
-                //FIXME: convert to mp3
-                //$di->location (on the server)
+                $itemUrl = $host1 . 'transcode'
+                    . '?mtParentId=' . $container->id
+                    . '&mtItemTitle=' . urlencode($item->title);
             }
             $listItems[] = getEpisodeItem(
                 $item->title,
             }
             $listItems[] = getEpisodeItem(
                 $item->title,
-                $item->url,
+                $itemUrl,
                 '',
                 'MP3'
             );
                 '',
                 'MP3'
             );
@@ -42,4 +44,66 @@ function handleRequestMediatomb($fullPath, $prefix)
 
     sendListItems($listItems);
 }
 
     sendListItems($listItems);
 }
+
+function transcodeMediatombItem($parentId, $title)
+{
+    global $mediatomb, $host1, $cacheDir, $cacheDirUrl;
+
+    if (!is_writable($cacheDir)) {
+        sendMessage('Cache dir not writable');
+        return;
+    }
+
+    extract($mediatomb);
+    try {
+        $smt = new Services_MediaTomb($user, $pass, $host, $port);
+        $item = $smt->getSingleItem((int) $parentId, $title, false);
+
+        $filename = $item->id . '.mp3';
+        $cacheFilePath = $cacheDir . $filename;
+        if (!file_exists($cacheFilePath)) {
+            transcodeUrlToMp3($item->url, $cacheFilePath);
+        }
+        if (!file_exists($cacheFilePath)) {
+            sendMessage('Error: No mp3 file found');
+            return;
+        }
+        $cacheFileUrl = $cacheDirUrl . $filename;
+        header('Location: ' . $cacheFileUrl);
+    } catch (Exception $e) {
+        sendMessage('Mediatomb error: ' . $e->getMessage());
+    }
+}
+
+function transcodeUrlToMp3($url, $mp3CacheFilePath)
+{
+    $tmpfile = tempnam(sys_get_temp_dir(), 'transcode');
+    exec(
+        'wget --quiet '
+        . escapeshellarg($url)
+        . ' -O ' . escapeshellarg($tmpfile),
+        $output,
+        $retval
+    );
+    if ($retval !== 0) {
+        throw new Exception('Error downloading URL');
+    }
+
+    exec(
+        'ffmpeg'
+        . ' -i ' . escapeshellarg($tmpfile)
+        . ' ' . escapeshellarg($mp3CacheFilePath)
+        . ' 2>&1',
+        $output,
+        $retval
+    );
+    unlink($tmpfile);
+    if ($retval !== 0) {
+        if (file_exists($mp3CacheFilePath)) {
+            unlink($mp3CacheFilePath);
+        }
+        //var_dump($tmpfile, $output);
+        throw new Exception('Error transcoding file');
+    }
+}
 ?>
 ?>
index d64436cad0ce3ebbe6dbb85f92562496a4f9d2d5..65f6715fd26bfe6cf9f878941c714bbb9b953aeb 100644 (file)
@@ -8,12 +8,14 @@ if (isset($_SERVER['REDIRECT_URL'])) {
 }
 $dataDir = __DIR__ . '/../data/';
 $varDir  = realpath(__DIR__ . '/../var') . '/';
 }
 $dataDir = __DIR__ . '/../data/';
 $varDir  = realpath(__DIR__ . '/../var') . '/';
+$cacheDir = __DIR__ . '/../www/cache/';
 $host1 = 'http://radio567.vtuner.com/';
 $host2 = 'http://radio5672.vtuner.com/';
 if ($_SERVER['HTTP_HOST'] !== '') {
     $host1 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
     $host2 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
 }
 $host1 = 'http://radio567.vtuner.com/';
 $host2 = 'http://radio5672.vtuner.com/';
 if ($_SERVER['HTTP_HOST'] !== '') {
     $host1 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
     $host2 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
 }
+$cacheDirUrl = $host1 . 'cache/';
 $cfgFile = $dataDir . 'config.php';
 if (file_exists($cfgFile)) {
     include $cfgFile;
 $cfgFile = $dataDir . 'config.php';
 if (file_exists($cfgFile)) {
     include $cfgFile;
@@ -41,6 +43,10 @@ if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token
 } else if ($path == '/RadioNativeFavorites.php') {
     //Favorites, defined via web interface
     sendMessage('Unsupported');
 } else if ($path == '/RadioNativeFavorites.php') {
     //Favorites, defined via web interface
     sendMessage('Unsupported');
+} else if ($path == '/transcode') {
+    require_once 'mediatomb.php';
+    transcodeMediatombItem($_GET['mtParentId'], $_GET['mtItemTitle']);
+    exit();
 }
 
 handleRequest(ltrim($path, '/'));
 }
 
 handleRequest(ltrim($path, '/'));
@@ -193,7 +199,7 @@ function getEpisodeItem($title, $fullUrl, $desc, $type)
     return '<Item>'
         . '<ItemType>ShowEpisode</ItemType>'
         . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
     return '<Item>'
         . '<ItemType>ShowEpisode</ItemType>'
         . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
-        . '<ShowEpisodeURL>' . $fullUrl . '</ShowEpisodeURL>'
+        . '<ShowEpisodeURL>' . htmlspecialchars($fullUrl) . '</ShowEpisodeURL>'
         . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
         . '<ShowMime>' . $type . '</ShowMime>'
         . '</Item>';
         . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
         . '<ShowMime>' . $type . '</ShowMime>'
         . '</Item>';