php - 使用 ffmpeg 从视频生成缩略图并添加到 mysql 数据库

标签 php ffmpeg upload thumbnails

我是 php 中的菜鸟,但仍在尝试 :) 我正在制作批量视频 uploader /导入器到数据库。寻找如何从上传的视频中提取缩略图并将这些缩略图添加到每个视频的 mysql 数据库的想法......:/我正在尝试使用 ffmpeg,但我没有找到如何将它实现到我的代码的方法......

<?php 
    // Database
    include 'config/database.php'; 

    if(isset($_POST['submit'])){
        
        $url = "localhost/";
        $uploadsDir = "uploads/";
        $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
        
        // Velidate if files exist
        if (!empty(array_filter($_FILES['fileUpload']['name']))) {
            
            // Loop through file items
            foreach($_FILES['fileUpload']['name'] as $title=>$val){
                // Get files upload path
                $fileName        = $_FILES['fileUpload']['name'][$title];
                $tempLocation    = $_FILES['fileUpload']['tmp_name'][$title];
                $targetFilePath  = $uploadsDir . $fileName;
                $fileType        = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
                $withOutExtension = pathinfo($fileName, PATHINFO_FILENAME);
                $uploadDate      = date('Y-m-d H:i:s');
                $uploadOk = 1;


                if(in_array($fileType, $allowedExts)){
                        if(move_uploaded_file($tempLocation, $targetFilePath)){
                            $sqlVal = $withOutExtension;
                            $sqlVal2 = $url . $uploadsDir . $fileName;
                            $sqlVal3 = null;
                            $randomID = rand(1000, 999999);
                            $sqlVal4 = ('<p><video controls="" src="/' . $sqlVal2 . '" width="640" height="360" class="note-video-clip"></video><br></p>');
                            $slug = str_replace(' ', '-', $withOutExtension);;
                            $file = $uploadsDir . $fileName;
                            $filesize = filesize($file); // bytes
                            $filesize = round($filesize / 1024 / 1024, 1);
                            

                        } else {
                            $response = array(
                                "status" => "alert-danger",
                                "message" => "File coud not be uploaded."
                            );
                        }
                    
                } else {
                    $response = array(
                        "status" => "alert-danger",
                        "message" => "I want mp4 file."
                    );
                }
                // Add into MySQL database
                if(!empty($sqlVal)) {
                    $insert = $conn->query("INSERT INTO applications (id, title, description, custom_description, details, image, slug, file_size, license, developer, url, buy_url, type, votes, screenshots, total_votes, counter, hits, category, platform, must_have, featured, pinned, editors_choice, created_at, updated_at) VALUES ('$randomID', '$sqlVal', 'Video .mp4 Live Wallpaper. Animated wallpaper is a cross between a screensaver and desktop wallpaper. Like a normal wallpaper, an animated wallpaper serves as the background on your desktop, which is visible to you only when your workspace is empty, i.e. no program windows block it from view.', '$sqlVal3', '$sqlVal4', '99999.jpg', '$slug', '$filesize MB', 'free', 'n/a', '$sqlVal2', '$sqlVal3', '1', '0.00', '', '0', '0', '1', '22', '6', '1', '1', '0', '1', '2021-11-11 16:55:36', '2021-11-11 16:55:36')");
                    if($insert) {
                        $response = array(
                            "status" => "alert-success",
                            "message" => "Files successfully uploaded."
                        );
                    } else {
                        $response = array(
                            "status" => "alert-danger",
                            "message" => "Files coudn't be uploaded due to database error."
                        );
                    }
                }
            }

        } else {
            // Error
            $response = array(
                "status" => "alert-danger",
                "message" => "Please select a file to upload."
            );
        }
    } 
    
    
    


?>

最佳答案

关于 FFMpeg 部分,我认为一个好的开始方法是实际使用 PHP-FFMpeg 库。文档中的 Basic Usage section 包含有关如何为给定视频生成帧的示例:

require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');

$video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
      ->save('frame.jpg');
一个简化的过程如下:
  • 用户上传一个视频,之后视频被移动到另一个
    目录。
  • 现在您可以使用上面的代码片段和 frame 方法来获取视频的缩略图。
  • 图像保存完成后,您只需将其添加到您的数据库中。
  • 如果缩略图指的是表格中的 image 列,则只需插入文件名 frame.jpg (甚至是完整的文件路径 /public/path/to/frame.jpg )即可逃脱。
  • 如果缩略图引用了表格中的 screenshots 列,并且您希望视频有多个缩略图,那么您应该考虑创建一个具有一对多关系的新表格(从您的视频/应用程序到一个新表格, 例如 thumbnails )

  • 然后当用户到达应该显示图像的页面时,只需从表中选择它并使用 <img> 标记(使用 public 文件路径)显示它。

  • 我还强烈建议不要将完整的 <video> 标签保存到您的数据库中,而是将其添加到您实际想要显示视频的页面中。
    例子:
    <?php
    $result = $conn->query('SELECT ...');
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            ?>
            <video src="<?php echo $row['video-column-path']; ?>"</video>
            <?php
        }
    } else {
        ?>
        No videos here
        <?php
    }
    $conn->close();
    ?>
    

    关于php - 使用 ffmpeg 从视频生成缩略图并添加到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71134195/

    相关文章:

    javascript - ERR_EMPTY_RESPONSE(每个打开的地方)

    javascript - 数据表简单连接查询

    php - 如何在 Laravel 中过滤多对多结构

    debugging - 解密 Xuggle/ffmpeg 错误(新手)

    powershell - 将文件夹中的每个 .flac 文件转换为 320kbps .mp3,而不丢失元数据

    ffmpeg - 提取ffmpeg缩略图过滤器选择的帧的索引

    file - CakePHP 与 Azure Blob

    php - 关闭 magento 购物车中的产品 url

    android - Web API 2 无法读取通过 Retrofit 2 发送的 byte[]

    java - 是否可以通过上传/通过上传的文件来攻击服务器?