function testDurationSetterAndGetter() { $i = new Naf_Media_Info(); $i->setDuration(10); // set number of seconds $this->assertEqual(10, $i->getDuration()); $this->assertEqual('00:00:10.0', $i->getDurationString()); $hours = 1; $minutes = 9; $seconds = 9.1; $time = sprintf("%02d:%02d:%04.1f", $hours, $minutes, $seconds); $i->setDuration($time); // set duration string $this->assertEqual($hours * 3600 + $minutes * 60 + $seconds, $i->getDuration()); $this->assertEqual($time, $i->getDurationString()); // check for exception throw on illegal tiem string try { $i->setDuration('illegal'); $this->fail("Naf_Media_Exception should be raised"); } catch (Naf_Media_Exception $e) { } }
/** * Read media file information. * * @param string filename * @return Naf_Media_Info */ function info($filename) { $i = new Naf_Media_Info($filename); $cmd = new Naf_ShellCmd($this->command); $cmd->addOption('-i', $filename); $infoText = $cmd->exec(true); // need to suppress errors due to ffmpeg producing error in case no output file is specified if (!preg_match("~Input\\s#0,\\s([^,]+)~m", $infoText, $matches1) || !preg_match("~\\s+Duration\\:\\s+(\\d{2}\\:\\d{2}\\:\\-?\\d{1,2}\\.\\d)(?:,\\s+start\\:\\s+\\d+\\.\\d+)?,\\s+bitrate\\:\\s+\\-?(\\d+|N/A)(?:\\s+kb/s)?\\s*~m", $infoText, $matches2)) { throw new Naf_Media_Exception("Unable to read movie info from {$infoText}"); } $i->setFormat($matches1[1]); $i->setDuration($matches2[1]); $bitrate = (int) $matches2[2]; if (!$bitrate) { $bitrate = 200; } $i->setBitrate($bitrate); if (preg_match("~.+Audio\\:\\s+([a-zA-Z0-9\\-\\_]+),\\s+(\\d+)\\s+Hz(?:,\\s+(?:stereo|mono))(?:,\\s+[a-zA-Z0-9_\\-])?,\\s+(\\d+)\\s+kb/s~m", $infoText, $matches)) { $i->setHasAudio(true); $i->setAudioCodec($matches[1]); $i->setSamplingRate($matches[2]); $i->setAudioBitrate($matches[3]); } /*if (preg_match("~.+Video\:\s+(\S+)(?:[,/]\s+\S+)?,\s+(\d+)x(\d+)(?:,\s+\d+\s+kb/s)?(?:,\s+(\d+\.\d+)\s+fps\(r\))?~m", $infoText, $matches)) { $i->setHasVideo(true); $i->setVideoCodec($matches[1]); $i->setPixelSize($matches[2], $matches[3]); $i->setFps($matches[4]); }*/ if (preg_match("~\n([^\n]+Video\\:\\s+(\\S+)[^\n]+)\n~m", $infoText, $matches)) { $videoInfo = $matches[1]; $i->setHasVideo(true); $i->setVideoCodec($matches[2]); if (preg_match("~(\\d{2,})x(\\d+)~", $videoInfo, $matches)) { $i->setPixelSize($matches[1], $matches[2]); } if (preg_match("~(\\d+\\.\\d+)\\s+fps\\(r\\)~", $videoInfo, $matches)) { $i->setFps($matches[1]); } } return $i; }
/** * Read media file information. * * @param string filename * @return Naf_Media_Info */ function info($filename) { $i = new Naf_Media_Info($filename); $cmd = new Naf_ShellCmd($this->command); $cmd->addOption('-identify'); $cmd->addOption('-frames', 0); $cmd->addOption('-ao', 'null'); $cmd->addOption('-vo', 'null'); $cmd->setTarget($filename); $infoText = $cmd->exec(); $w = $h = 0; foreach (preg_split("/\r\n|\r|\n/", $infoText) as $line) { if (preg_match("/^ID_([^\\=]+)=(.+)\$/", $line, $matches)) { $key = $matches[1]; $value = $matches[2]; if (0 === strpos($key, 'VIDEO')) { $i->setHasVideo(true); } elseif (0 === strpos($key, 'AUDIO')) { $i->setHasAudio(true); } switch ($key) { case "VIDEO_WIDTH": $w = (int) $value; break; case "VIDEO_HEIGHT": $h = (int) $value; break; case "VIDEO_FPS": $i->setFps($value); break; case "AUDIO_BITRATE": $i->setAudioBitrate($value / 1000); break; case "AUDIO_RATE": $i->setSamplingRate($value); break; case "LENGTH": $i->setDuration($value); break; case "AUDIO_CODEC": $i->setAudioCodec($value); break; case "VIDEO_CODEC": $i->setVideoCodec($value); break; default: break; } } elseif (preg_match("/^([^\\s]+) file format detected/", $line, $matches)) { $i->setFormat($matches[1]); } } if ($i->hasVideo()) { $i->setPixelSize($w, $h); $duration = $i->getDuration(); if (0 == $duration) { // experimentally it has been found that 5 seconds will be ok here... $duration = 5; } $i->setBitrate(filesize($filename) / 1024 * 8 / $duration); } return $i; }