| Autor | Zpráva | ||
|---|---|---|---|
| msigmund Profil |
#1 · Zasláno: 3. 3. 2011, 11:17:06
Dobrý den,
snažím se zprovoznit převod flv do mp3, ale nějak se mi to nedaří. Dostal jsem se k tomu, že se mi soubor uloží do složky vodeos, ale k převodu nedojde. Už si nevím rady jak dál, proto prosím o pomoc. Je to na adrese "mp3.topgam.cz" Index: <?php
// On form submission...
if ($_POST['submit'])
{
// Instantiate converter class
include 'YouTubeToMp3Converter.class.php';
$converter = new YouTubeToMp3Converter();
// Print "please wait" message and preview image
$vidID = $vidTitle = '';
$urlQueryStr = parse_url(trim($_POST['youtubeURL']), PHP_URL_QUERY);
if ($urlQueryStr !== false && !empty($urlQueryStr))
{
$kvPairs = explode('&', $urlQueryStr);
foreach ($kvPairs as $v)
{
$kvPair = explode('=', $v);
if ($kvPair[0] == 'v')
{
$vidID = $kvPair[1];
break;
}
}
echo '<div id="preview" style="display:block"><p>...Please wait while I try to convert:</p>';
echo '<p><img src="http://img.youtube.com/vi/'.$vidID.'/1.jpg" alt="preview image" /></p>';
echo '<p>'.$converter->ExtractSongTrackName(trim($_POST['youtubeURL']), 'url').'</p></div>';
flush();
}
// Main Program Execution
if ($converter->DownloadVideo(trim($_POST['youtubeURL'])))
{
echo ($converter->GenerateMP3($_POST['quality'])) ? '<p>Success!</p>' : '<p>Error generating MP3 file!</p>';
}
else
{
echo '<p>Error downloading video!</p>';
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter a valid YouTube.com video URL:</p>
<p><input type="text" name="youtubeURL" /></p>
<p><i>(i.e., "<span style="color:red">http://www.youtube.com/watch?v=HMpmI2F2cMs</span>")</i></p>
<p style="margin-top:20px">Choose the audio quality (better quality results in larger files):</p>
<p style="margin-bottom:25px"><input type="radio" value="64" name="quality" />Low <input type="radio" value="128" name="quality" checked="checked" />Medium <input type="radio" value="320" name="quality" />High</p>
<p><input type="submit" name="submit" value="Create MP3 File" /></p>
</form>
<script type="text/javascript">
window.onload = function()
{
if (document.getElementById('preview'))
{
document.getElementById('preview').style.display = 'none';
}
};
</script>Třída: // Conversion Class
class YouTubeToMp3Converter
{
// Private Fields
private $_songFileName = '';
private $_flvUrl = '';
private $_audioQualities = array(64, 128, 320);
private $_tempVidFileName;
private $_vidSrcTypes = array('source_code', 'url');
// Constants
const _TEMPVIDDIR = 'videos/';
const _SONGFILEDIR = 'mp3/';
const _FFMPEG = 'ffmpeg.exe';
#region Public Methods
function __construct()
{
}
function DownloadVideo($youTubeUrl)
{
$file_contents = file_get_contents($youTubeUrl);
if ($file_contents !== false)
{
$this->SetSongFileName($file_contents);
$this->SetFlvUrl($file_contents);
if ($this->GetSongFileName() != '' && $this->GetFlvUrl() != '')
{
return $this->SaveVideo($this->GetFlvUrl());
}
}
return false;
}
function GenerateMP3($audioQuality)
{
$qualities = $this->GetAudioQualities();
$quality = (in_array($audioQuality, $qualities)) ? $audioQuality : $qualities[1];
$exec_string = self::_FFMPEG.' -i '.$this->GetTempVidFileName().' -y -acodec libmp3lame -ab '.$quality.'k '.$this->GetSongFileName();
exec($exec_string);
$this->DeleteTempVid();
return is_file($this->GetSongFileName());
}
function ExtractSongTrackName($vidSrc, $srcType)
{
$name = '';
$vidSrcTypes = $this->GetVidSrcTypes();
if (in_array($srcType, $vidSrcTypes))
{
$vidSrc = ($srcType == $vidSrcTypes[1]) ? file_get_contents($vidSrc) : $vidSrc;
if ($vidSrc !== false && eregi('eow-title',$vidSrc))
{
$name = end(explode('eow-title',$vidSrc));
$name = current(explode('">',$name));
$name = ereg_replace('[^-_a-zA-Z,"\' :0-9]',"",end(explode('title="',$name)));
}
}
return $name;
}
#endregion
#region Private "Helper" Methods
private function SaveVideo($url)
{
$this->SetTempVidFileName(time());
$file = fopen($this->GetTempVidFileName(), 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE);
curl_exec($ch);
curl_close($ch);
fclose($file);
return is_file($this->GetTempVidFileName());
}
private function DeleteTempVid()
{
if (is_file($this->GetTempVidFileName()))
{
unlink($this->GetTempVidFileName());
}
}
#endregion
#region Properties
public function GetSongFileName()
{
return $this->_songFileName;
}
private function SetSongFileName($file_contents)
{
$vidSrcTypes = $this->GetVidSrcTypes();
$trackName = $this->ExtractSongTrackName($file_contents, $vidSrcTypes[0]);
$this->_songFileName = (!empty($trackName)) ? self::_SONGFILEDIR . preg_replace('/_{2,}/','_',preg_replace('/ /','_',preg_replace('/[^A-Za-z0-9 _-]/','',$trackName))) . '.mp3' : '';
}
public function GetFlvUrl()
{
return $this->_flvUrl;
}
private function SetFlvUrl($file_contents)
{
$vidUrl = '';
if (eregi('fmt_url_map',$file_contents))
{
$vidUrl = end(explode('&fmt_url_map=',$file_contents));
$vidUrl = current(explode('&',$vidUrl));
$vidUrl = current(explode('%2C',$vidUrl));
$vidUrl = urldecode(end(explode('%7C',$vidUrl)));
}
$this->_flvUrl = $vidUrl;
}
public function GetAudioQualities()
{
return $this->_audioQualities;
}
private function GetTempVidFileName()
{
return $this->_tempVidFileName;
}
private function SetTempVidFileName($timestamp)
{
$this->_tempVidFileName = self::_TEMPVIDDIR . $timestamp .'.flv';
}
public function GetVidSrcTypes()
{
return $this->_vidSrcTypes;
}
#endregion
}Děkuji předem za pomoc. |
||
| __construct Profil |
#2 · Zasláno: 3. 3. 2011, 11:39:26
msigmund:
Bude to tým že metóda GenerateMP3() spúšťa na PC aplikáciu ffmpeg.exe, ktorá robí konverziu. Pravdepodobne tam tú aplikáciu nemáš.
|
||
| msigmund Profil |
#3 · Zasláno: 3. 3. 2011, 12:12:43 · Upravil/a: msigmund
Podpora mi tam tu aplikaci nahrála a mám tam soubor ffmpeg.exe v kořenovém adresáři.
|
||
| Tori Profil |
#4 · Zasláno: 3. 3. 2011, 12:33:38
A přístupová práva (k ffmpeg.exe i složkám) jsou správně nastavená a s absolutními cestami to rovněž nefunguje?
Funkce exec by měla vracet poslední řádek výstupu: nehlásí chybu? |
||
| msigmund Profil |
#5 · Zasláno: 3. 3. 2011, 15:48:55
Ano vše je jak píšete a chybu to právě nehlásí žádnou.
|
||
| Kalda Profil |
#6 · Zasláno: 4. 3. 2011, 00:20:34
Zkuste zavolat FFMPEG přes PHP a zjistit tak, zda buď máte oprávnění ffmpeg spustit, nebo zda po spuštění ffmpeg nehlásí nějakou chybu (máte vhodné kodeky).
Ideálně si na ten test to volání přes nějaký exec/popen si napište přímo sám "natvrdo". Jinak jak psal Tori, že exec vrací poslední řádek - tak při tom standardním volání, jak je v té funkci, exec nevrací naprosto nic, takže z toho ani nevíte, zda to "voláte blbě" (nějak obecně ani nedojde ke spuštění toho FFMPEGU), nebo zda je chyba v parametrech/nastavení toho FFMPEGu. |
||
| __construct Profil |
#7 · Zasláno: 4. 3. 2011, 08:29:31
msigmund:
Keď pozerám na to čo sa spúšťa - cez parameter si tá aplikácia načítava kodek k MP3 - libmp3lame. Má k nemu prístup?
|
||
| msigmund Profil |
#8 · Zasláno: 4. 3. 2011, 09:37:43
Děkuji všem, ale asi jsem si ukrojil pro mě moc velký krajíc chleba.
Nepomohl by mi někdo i za finanční odměnu. Děkuji. |
||
| msigmund Profil |
#9 · Zasláno: 4. 3. 2011, 10:49:01
Promiňte nenapsal jsem kontakt "msigmund zavináč seznam tečka cz"
|
||
|
Časová prodleva: 15 let
|
|||
0