add dead simple transcoding with ffmpeg
[noxon-gateway.git] / src / mediatomb.php
1 <?php
2 require_once 'Services/MediaTomb.php';
3
4 function handleRequestMediatomb($fullPath, $prefix)
5 {
6     global $mediatomb, $host1;
7
8     extract($mediatomb);
9     try {
10         $smt = new Services_MediaTomb($user, $pass, $host, $port);
11
12         $path = substr($fullPath, strlen($prefix));
13         $container = $smt->getContainerByPath($path);
14         $listItems = array();
15         addPreviousItem($listItems, $fullPath);
16
17         foreach ($container->getContainers() as $subContainer) {
18             $listItems[] = getDirItem(
19                 $subContainer->title,
20                 pathEncode($fullPath . $subContainer->title) . '/'
21             );
22         }
23
24         foreach ($container->getItemIterator(false) as $item) {
25             $di = $item->getDetailedItem();
26             $itemUrl = $item->url;
27             if ($di->mimetype !== 'audio/mpeg') {
28                 //noxon iRadio cube does not want to play .ogg files
29                 $itemUrl = $host1 . 'transcode'
30                     . '?mtParentId=' . $container->id
31                     . '&mtItemTitle=' . urlencode($item->title);
32             }
33             $listItems[] = getEpisodeItem(
34                 $item->title,
35                 $itemUrl,
36                 '',
37                 'MP3'
38             );
39         }
40     } catch (Exception $e) {
41         sendMessage('Mediatomb error: ' . $e->getMessage());
42         return;
43     }
44
45     sendListItems($listItems);
46 }
47
48 function transcodeMediatombItem($parentId, $title)
49 {
50     global $mediatomb, $host1, $cacheDir, $cacheDirUrl;
51
52     if (!is_writable($cacheDir)) {
53         sendMessage('Cache dir not writable');
54         return;
55     }
56
57     extract($mediatomb);
58     try {
59         $smt = new Services_MediaTomb($user, $pass, $host, $port);
60         $item = $smt->getSingleItem((int) $parentId, $title, false);
61
62         $filename = $item->id . '.mp3';
63         $cacheFilePath = $cacheDir . $filename;
64         if (!file_exists($cacheFilePath)) {
65             transcodeUrlToMp3($item->url, $cacheFilePath);
66         }
67         if (!file_exists($cacheFilePath)) {
68             sendMessage('Error: No mp3 file found');
69             return;
70         }
71         $cacheFileUrl = $cacheDirUrl . $filename;
72         header('Location: ' . $cacheFileUrl);
73     } catch (Exception $e) {
74         sendMessage('Mediatomb error: ' . $e->getMessage());
75     }
76 }
77
78 function transcodeUrlToMp3($url, $mp3CacheFilePath)
79 {
80     $tmpfile = tempnam(sys_get_temp_dir(), 'transcode');
81     exec(
82         'wget --quiet '
83         . escapeshellarg($url)
84         . ' -O ' . escapeshellarg($tmpfile),
85         $output,
86         $retval
87     );
88     if ($retval !== 0) {
89         throw new Exception('Error downloading URL');
90     }
91
92     exec(
93         'ffmpeg'
94         . ' -i ' . escapeshellarg($tmpfile)
95         . ' ' . escapeshellarg($mp3CacheFilePath)
96         . ' 2>&1',
97         $output,
98         $retval
99     );
100     unlink($tmpfile);
101     if ($retval !== 0) {
102         if (file_exists($mp3CacheFilePath)) {
103             unlink($mp3CacheFilePath);
104         }
105         //var_dump($tmpfile, $output);
106         throw new Exception('Error transcoding file');
107     }
108 }
109 ?>