PHPでflvファイルを操作してスクリーンショットを取得する方法

墨辰丷
リリース: 2023-03-31 19:52:02
オリジナル
1344 人が閲覧しました

この記事では主に、PHP を使用して FLV ファイルのスクリーンショットを取得する方法を紹介します。必要な友人は参考にしてください。

# この記事の例では、PHP が FLV ファイルからビデオ プレビューを取得する方法を説明します。具体的な実装方法は次のとおりです。

コードは次のとおりです。

<?php
// references http://www.longtailvideo.com/support/forum/Modules/12661/External-PHP-with-FFmpeg-using-readfile-
// generate a preview image from an FLV file on-the-fly, or to save
// call with: ffmpeg_image.php?file=video.flv&time=00:00:05&browser=true
// call with: ffmpeg_image.php?file=video.flv&percent=75.3&browser=true
// no time defaults to "00:00:01" (one second), no browser defaults to "true"
$videofile = (isset($_GET[&#39;file&#39;])) ? strval($_GET[&#39;file&#39;]) : &#39;video.flv&#39;;
$image = substr($videofile, 0, strlen($videofile) - 4);
$time = (isset($_GET[&#39;time&#39;])) ? strval($_GET[&#39;time&#39;]) : &#39;00:00:01&#39;;
// debug ("  File: ", $videofile);
// debug (" Image: ", $image);
// debug ("  Time: ", $time);
// check time format
if (!preg_match(&#39;/\d\d:\d\d:\d\d/&#39;, $time))
{
  $time = "00:00:00";
}
if (isset($_GET[&#39;percent&#39;]))
{
  $percent = $_GET[&#39;percent&#39;];
// debug (" Percent: ", $percent);
  ob_start();
  exec("/usr/bin/ffmpeg -i \"". $videofile . "\" 2>&1");
  $duration = ob_get_contents();
  ob_end_clean();
  // debug ("Duration: ", $duration);
  preg_match(&#39;/Duration: (.*?),/&#39;, $duration, $matches);
  $duration = $matches[1];
// debug ("Duration: ", $duration);
  $duration_array = split(&#39;:&#39;, $duration);
  $duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
  $time = $duration * $percent / 100;
// debug (" Time: ", $time);
  $time = intval($time/3600) . ":" . intval(($time-(intval($time/3600)*3600))/60) . ":" . sprintf("%01.3f", ($time-(intval($time/60)*60)));
// debug (" Time: ", $time);
}
$browser = (isset($_GET[&#39;browser&#39;])) ? strval($_GET[&#39;browser&#39;]) : &#39;true&#39;;
// debug (" Browser: ", $browser);
if ($browser == "true")
{
  header(&#39;Content-Type: image/png&#39;);
  exec("/usr/bin/ffmpeg -vcodec png -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -");
//header(&#39;Content-Type: image/jpeg&#39;);
//exec("/usr/bin/ffmpeg -vcodec mjpeg -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 -");
}
else
{
  exec("/usr/bin/ffmpeg -vcodec png -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 \"" . $image . "\"%d.png");
//exec("/usr/bin/ffmpeg -vcodec mjpeg -i \"" . $videofile . "\" -ss " . $time . " -vframes 1 -f image2 \"" . $image . "\"%d.jpg");
}
?>
ログイン後にコピー

概要: 以上がこの記事の全内容です。みんなの学習に役立ちます。

関連する推奨事項:

PHP のストリームの基本知識

PHP の static キーワード、遅延バインディング、self の定義key 単語の違い

PHP シングルトンモードの概念と特徴

以上がPHPでflvファイルを操作してスクリーンショットを取得する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!