Author Topic: Erweiterung auf weiter Dateiformate (mp3, smil)  (Read 7814 times)

0 Members and 1 Guest are viewing this topic.

Offline tbk

  • Pre-Newbie
  • Posts: 3
    • View Profile
Erweiterung auf weiter Dateiformate (mp3, smil)
« on: November 04, 2002, 10:20:19 PM »
Hallo!

Quote
Dabei ist man nicht nur auf die gängigen Bildformate wie JPG, GIF und PNG beschränkt, 4images lässt sich leicht für jedes beliebige Dateiformat erweitern. von 4homepages.de


Eben dies würde ich gerne machen und einen Parser für mp3 und smil-Dateien schreiben, so dass Beschreibungen aus den ID3 bzw den XML-Beschreibungen direkt gewonnen werden können. Ist dies möglich und falls ja, wo muss ich diesbezüglich ansetzen.

gruss
torsten

Offline Jan

  • Administrator
  • 4images Guru
  • *****
  • Posts: 5.024
    • View Profile
    • 4images - Image Gallery Management System
Erweiterung auf weiter Dateiformate (mp3, smil)
« Reply #1 on: November 05, 2002, 07:50:04 AM »
Grundsätzlich fügst Du so neue Dateitypen hinzu: http://www.4homepages.de/forum/viewtopic.php?p=5740#5740

Um das für mp3/smil zu erweitern, schau Dir die Funktion get_media_code() in "includes/functions.php" an.

Jan
Your first three "must do" before you ask a question:
1. Forum rules
2. FAQ
3. Search

Offline tbk

  • Pre-Newbie
  • Posts: 3
    • View Profile
Erweiterung auf weiter Dateiformate (mp3, smil)
« Reply #2 on: November 07, 2002, 11:08:06 PM »
I just finished work on this.  8) If anyone else is interested, I'll post the changed files.

torsten

Offline hyperjojo

  • Pre-Newbie
  • Posts: 3
    • View Profile
Erweiterung auf weiter Dateiformate (mp3, smil)
« Reply #3 on: January 10, 2003, 08:18:40 PM »
Hi,

i would be interested in the changed files...

I'd be happy, if you post them here..

Sorry for my bad English!


hyperjojo

Offline tbk

  • Pre-Newbie
  • Posts: 3
    • View Profile
Erweiterung auf weiter Dateiformate (mp3, smil)
« Reply #4 on: January 11, 2003, 10:44:13 AM »
Hi!

For now I just implemented the mp3-Information and this is what I changed in the includes/functions.php. Replace the Function get_media_code() with the one below and add the functions strim() and mp3_getinfo().

In your Template you'll have the additional variables song / artist / album / year / genre / comment / track. You can check, whether you have an mp3 in the detailed view by checking the variable "mp3_info" in your template, e.g.
Code: [Select]

{if mp3_info}
  <!-- ... use additional variables here-->
<table>
  <tr>
                              <td valign="top" class="row1"><b>Songtitel (Nr)</b></td>
                              <td valign="top" class="row1">{song} ({track})</td>
                            </tr>
                            <tr>
                              <td valign="top" class="row1"><b>Künstler</b></td>
                              <td valign="top" class="row1">{artist}</td>
                            </tr>
                            <tr>
                              <td valign="top" class="row1"><b>Album (Jahr)</b></td>
                              <td valign="top" class="row1">{album} ({year})</td>
                            </tr>
</table>
{endif mp3_info}


U might see, that this is not an "out-of-the-box" solution, but gives a general idea how you can integrate a detailed mp3-view in 4images. As soon as the smil-Thing is ready, I'll post it here


torsten
---------------
Code: [Select]

function get_media_code($media_file_name, $image_id = 0, $cat_id = 0, $image_name = "", $mode = "", $show_link = 0, $detailed_view = 0) {
  global $site_template, $site_sess, $lang, $mode;

  if (!get_file_path($media_file_name, "media", $cat_id, 0, 0)) {
    $media = "<img src=\"".ICON_PATH."/404.gif\" border=\"0\" alt=\"\" />";
    $site_template->register_vars("iptc_info", "");
  }
  else {
    $media_src = get_file_path($media_file_name, "media", $cat_id, 0, 1);
    $file_extension = get_file_extension($media_file_name);
    $media_icon = "<img src=\"".ICON_PATH."/".$file_extension.".gif\" border=\"0\" alt=\"".$image_name."\" />";
    if ($show_link) {
      $media_icon = "<a href=\"".$site_sess->url(ROOT_PATH."details.php?".URL_IMAGE_ID."=".$image_id.((!empty($mode)) ? "&amp;mode=".$mode : ""))."\">".$media_icon."</a>";
    }
    $width_height = "";
    $width = "";
    $height = "";
    $iptc_info = "";
    $src = (!file_exists($media_src) && file_exists(preg_replace("/\/{2,}/", "/", get_document_root()."/".$media_src))) ? preg_replace("/\/{2,}/", "/", get_document_root()."/".$media_src) : $media_src;
    if ($image_info = @getimagesize($src, $info)) {
      $width_height = " ".$image_info[3];
      $width = $image_info[0];
      $height = $image_info[1];
      if ($detailed_view && isset($info['APP13'])) {
        $iptc_array = get_iptc_info($info['APP13']);
        $bgcounter = 0;
        foreach ($iptc_array as $key => $val) {
          $row_bg_number = ($bgcounter++ % 2 == 0) ? 1 : 2;
          $site_template->register_vars(array(
            "iptc_value" => $val,
            "iptc_name" => $lang['iptc_'.$key],
            "row_bg_number" => $row_bg_number
          ));
          $iptc_info .= $site_template->parse_template("iptc_bit");
        }
      }
    }
   
    // Hier analysieren wir mp3-Dateien
    if ($file_extension == "mp3" && $detailed_view)
    {
      $mp3info = mp3_getinfo($src,$update=1,$advanced=0);
      $site_template->register_vars("simplepic_info", "");
      $site_template->register_vars( $mp3info );
     
      //$site_template->register_vars(array(
      //  "id3_song" => $mp3info['song']
      //  ));
     
      //var_dump( $mp3info );
      //echo "\n<br />";
    }
    else if ($detailed_view)
    {
      $site_template->register_vars("mp3_info", "");
      $site_template->register_vars("simplepic_info", "yes");
    }
   
   
    $site_template->register_vars(array(
      "media_src" => $media_src,
      "media_icon" => $media_icon,
      "image_name" => $image_name,
      "width_height" => $width_height,
      "width" => $width,
      "height" => $height,
      "iptc_info" => $iptc_info
    ));
    $media = $site_template->parse_template("media/".$file_extension);
  }
  return $media;
}


///////////////////////////////////////////////////////////////////////
////// I D 3 //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

/**
 * Genommen aus Code ursprünglich geschrieben von
 *  Kristof Hamann für sein Programm MusicServer
 *          www.korelstar.de
 */

// strim trimmt auch binäre 0
function strim($str)
{
$buf=""; $ret="";
$str = trim($str);
for($i=0;$i<strlen($str);$i++)
{
if(ord($str[$i])==0)
{
if(strlen($ret)!=0)
{
$buf .= $str[$i];
}
}
else
{
if($buf)
{
$ret .= $buf;
$buf = "";
}
$ret .= $str[$i];
}
}
return $ret;
}


// liest infos aus MP3
function mp3_getinfo($datei,$update=0,$advanced=1)
{
global $MP3DIR;
$mp3info = array();
$mp3info["size"] = @filesize($MP3DIR.$datei);
$mp3info["datei"] = $datei;

// Sicherheitschecks
if(strtolower(substr($datei,-4))!=".mp3" || strstr($datei,"..") || substr($datei,0,1)=="/" || !is_file($MP3DIR.$datei) || !is_readable($MP3DIR.$datei))
{
if($GLOBALS["DEBUG"]) echo "Keine g&uuml;ltige MP3 gew&auml;hlt.<br />";
return false;
}

// auslesen der datei
$fp = fopen($MP3DIR.$datei,"r");
if(!$fp)
{
if($GLOBALS["DEBUG"]) echo "Konnte Datei nicht oeffnen.<br />";
return false;
}

fseek($fp,0);
$mp3head_asc = fread($fp,4);
// ID3v2-Tag
if(substr($mp3head_asc,0,3)=="ID3")
{
// lese restlichen IDv2-header aus
$mp3v2head_asc = $mp3head_asc . fread($fp,6);
$mp3v2head = asc2bin($mp3v2head_asc);
if($GLOBALS["DEBUG"]) echo "<b>IDv2-Header:</b> ".chunk_split($mp3v2head,8," ")."<br />";
// Laenge des ID3v2-Headers
$mp3v2headext_len = bindec(substr($mp3v2head,49,7).substr($mp3v2head,57,7).substr($mp3v2head,65,7).substr($mp3v2head,73,7));
if($GLOBALS["DEBUG"]) echo "Laenge des ID3v2-Headers: $mp3v2headext_len<br />";
// checke ob Erweiterten Headers ???? !!!! ????
if($mp3v2head[49])
{
if($GLOBALS["DEBUG"]) echo "Erweiterter Header, noch nicht programmiert.<br />";
}
// Version, kenne nur die 3.
$mp3info["id3v2"] = bindec(substr($mp3v2head,24,8));
if($mp3info["id3v2"]==3)
{
// Lese ID3v2-Informationen aus (alle Frames)
fseek($fp,10);
$mp3v2data = fread($fp,$mp3v2headext_len);
$curpos = 0;
while($curpos < $mp3v2headext_len)
{
$mp3v2frameid    = substr($mp3v2data,$curpos + 0,4);
if(!trim($mp3v2frameid)) break;
$mp3v2framesize  = bindec(asc2bin(substr($mp3v2data,$curpos + 4,4)));
$mp3v2frameflags = asc2bin(substr($mp3v2data,$curpos + 8,2));
$mp3v2framedata  = substr($mp3v2data,$curpos + 10,$mp3v2framesize);
$curpos += 10 + $mp3v2framesize;
if($GLOBALS["DEBUG"]) echo "<b>$mp3v2frameid</b> (Len: $mp3v2framesize, Flags: ".chunk_split($mp3v2frameflags,8," ").") = $mp3v2framedata <small>[".chunk_split(asc2bin($mp3v2framedata),8," ")."]</small><br />";
switch($mp3v2frameid)
{
case "TPE1": $mp3info["artist"] = $mp3v2framedata; break;
case "TIT2": $mp3info["song"] = $mp3v2framedata; break;
case "TRCK": $mp3info["track"] = $mp3v2framedata; break;
case "TYER": $mp3info["year"] = $mp3v2framedata; break;
case "TALB": $mp3info["album"] = $mp3v2framedata; break;
case "COMM": $mp3info["comment"] = substr($mp3v2framedata,4); break;
case "TCON": if(substr($mp3v2framedata,1,1)=="(") $mp3info["genre"] = (int)substr($mp3v2framedata,2,3); break;
}
}
}
elseif($GLOBALS["DEBUG"]) echo "ID3v2-Tag-Version ist nicht 3. Kenne ich leider nicht.<br />";
// Lese MP3-Header fuer Advanced-Mod aus
if($advanced)
{
$mp3v2_startdata = $mp3v2headext_len + 10;
if($GLOBALS["DEBUG"]) echo "MP3 beginnt bei Byte ".$mp3v2_startdata.".<br />";
fseek($fp,$mp3v2_startdata);
$mp3head_asc = fread($fp,4);
}
}

// auslesen erweiterter informationen (bitrate, etc.)
if($advanced)
{
$mp3headinfo = array();
if($GLOBALS["DEBUG"]) echo "<br /><b>Erweiterte Infos zur Datei (DEBUG-Modus):</b><br />";
$mp3head = asc2bin($mp3head_asc);
if($GLOBALS["DEBUG"]) echo "<b>1. Frame-Header:</b> ".chunk_split($mp3head,8," ")."<br /><br />";
// lese techn. infos aus
$mp3headinfo["mpeg"]      = $mp3head[12];
$mp3headinfo["layer"]     = substr($mp3head, 13, 2);
$mp3headinfo["protect"]   = $mp3head[15];
$mp3headinfo["bitrate"]   = substr($mp3head, 16, 4);
$mp3headinfo["frequency"] = substr($mp3head, 20, 2);
$mp3headinfo["padding"]   = $mp3head[22];
$mp3headinfo["private"]   = $mp3head[23];
$mp3headinfo["mode"]      = substr($mp3head, 24, 2);
$mp3headinfo["modeext"]   = substr($mp3head, 26, 2);
$mp3headinfo["copy"]      = $mp3head[28];
$mp3headinfo["home"]      = $mp3head[29];
$mp3headinfo["emphasis"]  = substr($mp3head, 30, 2);
// wandle infos in mp3info um
switch($mp3headinfo["layer"])
{
case "01" : $mp3info["layer"]=3; break;
case "10" : $mp3info["layer"]=2; break;
case "11" : $mp3info["layer"]=1; break;
default   : $mp3info["layer"]="[fehler]"; break;
}
if($mp3headinfo["mpeg"])
{
switch($mp3headinfo["frequency"])
{
case "00" : $mp3info["frequency"]=44100; break;
case "01" : $mp3info["frequency"]=48000; break;
case "10" : $mp3info["frequency"]=32000; break;
default   : $mp3info["frequency"]=""; break;
}
}
else
{
switch($mp3headinfo["frequency"])
{
case "00" : $mp3info["frequency"]=22050; break;
case "01" : $mp3info["frequency"]=24000; break;
case "10" : $mp3info["frequency"]=16000; break;
default   : $mp3info["frequency"]=""; break;
}
}
switch($mp3headinfo["mode"])
{
case "00" : $mp3info["mode"]="Stereo"; break;
case "01" : $mp3info["mode"]="Joint Stereo"; break;
case "10" : $mp3info["mode"]="Dual Channel"; break;
case "11" : $mp3info["mode"]="Mono"; break;
default   : $mp3info["mode"]="[fehler]"; break;
}
switch($mp3headinfo["emphasis"])
{
case "00" : $mp3info["emphasis"]="none"; break;
case "01" : $mp3info["emphasis"]="50/15ms"; break;
case "10" : $mp3info["emphasis"]="not set"; break;
case "11" : $mp3info["emphasis"]="CCITT j.17"; break;
default   : $mp3info["emphasis"]="[fehler]"; break;
}
$mp3info["bitrate"] = $GLOBALS["BITRATE"][$mp3headinfo["mpeg"]][$mp3headinfo["layer"]][$mp3headinfo["bitrate"]];
if((int)$mp3info["frequency"]) $mp3info["framesize"] = floor((144 * $mp3info["bitrate"] * 1000 / $mp3info["frequency"]) + $mp3headinfo["padding"]);
if((int)$mp3info["framesize"]) $mp3info["frames"] = floor($mp3info["size"] / $mp3info["framesize"]);
if((int)$mp3info["bitrate"]) $mp3info["length"] = floor($mp3info["size"] / ($mp3info["bitrate"] * 1000 / 8));
$mp3info["mpeg"] = $mp3headinfo["mpeg"] ? 1 : 2;
$mp3info["copyright"] = $mp3headinfo["copy"] ? "yes" : "no";
$mp3info["private"] = $mp3headinfo["private"] ? "yes" : "no";
$mp3info["home"] = $mp3headinfo["home"] ? "yes" : "no";

if($GLOBALS["DEBUG"])
{
echo "<pre>\$mp3headinfo: ";
print_r($mp3headinfo);
echo "<br />\$mp3info: ";
print_r($mp3info);
echo "</pre>";
}
}


// beginn des ID3v1-tags bei 128byte von hinten
fseek($fp,$mp3info["size"]-128);
$ID3 = fread($fp,128);
if(strtoupper(substr($ID3,0,3))=="TAG")
{
$mp3info["id3v1"] = 1;
if($GLOBALS["DEBUG"]) echo "<b>ID3v1-Tag found</b>.<br />";
$mp3info["id3song"] = strim(substr($ID3,3,30));
$mp3info["id3artist"] = strim(substr($ID3,33,30));
$mp3info["id3album"] = strim(substr($ID3,63,30));
$mp3info["id3year"] = strim(substr($ID3,93,4));
$mp3info["id3genre"] = ord($ID3[127]);
// der trick: falls vorletztes comment-char 0 ist,
// und das letzte ungleich 0, so gibt das letzte die
// tracknummer an
if(ord($ID3[125])==0 && ord($ID3[126])!=0)
{
$mp3info["id3comment"] = trim(substr($ID3,97,28));
$mp3info["id3track"] = ord($ID3[126]);
}
else
{
$mp3info["id3comment"] = trim(substr($ID3,97,30));
}
if(empty($mp3info["song"])) $mp3info["song"] = $mp3info["id3song"];
if(empty($mp3info["artist"])) $mp3info["artist"] = $mp3info["id3artist"];
if(empty($mp3info["album"])) $mp3info["album"] = $mp3info["id3album"];
if(empty($mp3info["year"])) $mp3info["year"] = $mp3info["id3year"];
if(empty($mp3info["genre"])) $mp3info["genre"] = $mp3info["id3genre"];
if(empty($mp3info["comment"])) $mp3info["comment"] = $mp3info["id3comment"];
if(empty($mp3info["track"]) && !empty($mp3info["id3track"])) $mp3info["track"] = $mp3info["id3track"];
}
fclose($fp);

return $mp3info;
}
Code: [Select]