source: extensions/charlies_content/getid3/getid3/module.audio.shorten.php @ 3318

Revision 3318, 5.3 KB checked in by vdigital, 11 years ago (diff)

+ Add Charlies' content to depository

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2// +----------------------------------------------------------------------+
3// | PHP version 5                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
6// +----------------------------------------------------------------------+
7// | This source file is subject to version 2 of the GPL license,         |
8// | that is bundled with this package in the file license.txt and is     |
9// | available through the world-wide-web at the following url:           |
10// | http://www.gnu.org/copyleft/gpl.html                                 |
11// +----------------------------------------------------------------------+
12// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
13// +----------------------------------------------------------------------+
14// | Authors: James Heinrich <infoØgetid3*org>                            |
15// |          Allan Hansen <ahØartemis*dk>                                |
16// +----------------------------------------------------------------------+
17// | module.audio.shorten.php                                             |
18// | Module for analyzing Shorten Audio files                             |
19// | dependencies: module.audio-video.riff.php                            |
20// +----------------------------------------------------------------------+
21//
22// $Id$
23
24       
25       
26class getid3_shorten extends getid3_handler
27{
28
29    public function __construct(getID3 $getid3) {
30
31        parent::__construct($getid3);
32       
33        if ((bool)ini_get('safe_mode')) {
34            throw new getid3_exception('PHP running in Safe Mode - backtick operator not available, cannot analyze Shorten files.');
35        }
36
37        if (!`head --version`) {
38            throw new getid3_exception('head[.exe] binary not found in path. UNIX: typically /usr/bin. Windows: typically c:\windows\system32.');
39        }
40       
41        if (!`shorten -l`) {
42            throw new getid3_exception('shorten[.exe] binary not found in path. UNIX: typically /usr/bin. Windows: typically c:\windows\system32.');
43        }
44    }
45
46
47    public function Analyze() {
48       
49        $getid3 = $this->getid3;
50
51        $getid3->include_module('audio-video.riff');
52
53        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
54
55        $shn_header = fread($getid3->fp, 8);
56       
57        // Magic bytes: "ajkg"
58       
59        $getid3->info['fileformat']            = 'shn';
60        $getid3->info['audio']['dataformat']   = 'shn';
61        $getid3->info['audio']['lossless']     = true;
62        $getid3->info['audio']['bitrate_mode'] = 'vbr';
63
64        $getid3->info['shn']['version'] = getid3_lib::LittleEndian2Int($shn_header{4});
65
66        fseek($getid3->fp, $getid3->info['avdataend'] - 12, SEEK_SET);
67       
68        $seek_table_signature_test = fread($getid3->fp, 12);
69       
70        $getid3->info['shn']['seektable']['present'] = (bool)(substr($seek_table_signature_test, 4, 8) == 'SHNAMPSK');
71        if ($getid3->info['shn']['seektable']['present']) {
72       
73            $getid3->info['shn']['seektable']['length'] = getid3_lib::LittleEndian2Int(substr($seek_table_signature_test, 0, 4));
74            $getid3->info['shn']['seektable']['offset'] = $getid3->info['avdataend'] - $getid3->info['shn']['seektable']['length'];
75            fseek($getid3->fp, $getid3->info['shn']['seektable']['offset'], SEEK_SET);
76            $seek_table_magic = fread($getid3->fp, 4);
77       
78            if ($seek_table_magic != 'SEEK') {
79
80                throw new getid3_exception('Expecting "SEEK" at offset '.$getid3->info['shn']['seektable']['offset'].', found "'.$seek_table_magic.'"');
81            } 
82
83            $seek_table_data = fread($getid3->fp, $getid3->info['shn']['seektable']['length'] - 16);
84            $getid3->info['shn']['seektable']['entry_count'] = floor(strlen($seek_table_data) / 80);
85        }
86
87        $commandline = 'shorten -x '.escapeshellarg(realpath($getid3->filename)).' - | head -c 64';
88        $output = `$commandline`;
89
90        if (@$output && substr($output, 12, 4) == 'fmt ') {
91
92            $fmt_size = getid3_lib::LittleEndian2Int(substr($output, 16, 4));
93            $decoded_wav_format_ex = getid3_riff::RIFFparseWAVEFORMATex(substr($output, 20, $fmt_size));
94           
95            $getid3->info['audio']['channels']        = $decoded_wav_format_ex['channels'];
96            $getid3->info['audio']['bits_per_sample'] = $decoded_wav_format_ex['bits_per_sample'];
97            $getid3->info['audio']['sample_rate']     = $decoded_wav_format_ex['sample_rate'];
98
99            if (substr($output, 20 + $fmt_size, 4) == 'data') {
100
101                $getid3->info['playtime_seconds'] = getid3_lib::LittleEndian2Int(substr($output, 20 + 4 + $fmt_size, 4)) / $decoded_wav_format_ex['raw']['nAvgBytesPerSec'];
102
103            } else {
104
105                throw new getid3_exception('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
106            }
107
108            $getid3->info['audio']['bitrate'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) / $getid3->info['playtime_seconds']) * 8;
109
110        } else {
111
112            throw new getid3_exception('shorten failed to decode file to WAV for parsing');
113            return false;
114        }
115
116        return true;
117    }
118
119}
120
121?>
Note: See TracBrowser for help on using the repository browser.