统计
  • 建站日期:2021-03-10
  • 文章总数:381 篇
  • 评论总数:365 条
  • 分类总数:9 个
  • 最后更新:5月13日
文章 未分类

Thinkphp6实现视频在线去重功能

创新博客
首页 未分类 正文

这里我们用thinkphp来实现视频去重
前端使用Bootstrap框架
首先设计一个简单的页面,包括上传视频和展示去重结果的区域。
html代码,页面放在哪里得话取决于你自己

Thinkphp6实现视频在线去重功能
-创新博客-专注于资源分享的blog
-第1
张图片

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>视频去重</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container mt-5">
        <h1 class="text-center mb-5">视频去重</h1>
        <form id="upload-form" enctype="multipart/form-data">
            <div class="form-group">
                <label for="video-file">选择视频文件:</label>
                <input type="file" class="form-control-file" id="video-file" name="video-file">
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary">上传</button>
            </div>
        </form>
        <div id="result" class="mt-5"></div>
    </div>
</body>

</html>

后端代码实现
使用ThinkPHP6框架,实现视频去重功能。首先需要安装FFmpeg和FFprobe,使用命令行执行以下命

sudo apt-get install ffmpeg
sudo apt-get install ffprobe

然后,编写Controller的代码,包括上传文件和去重的方法。

<?php
namespace appindexcontroller;
use thinkController;
use thinkfacadeRequest;
use thinkfacadeFilesystem;
use thinkfacadeConfig;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function upload()
    {
        $file = Request::file('video-file');
        if (!$file) {
            return json(['code' => 1, 'msg' => '请选择视频文件']);
        }
        $validate = [
            'size' => Config::get('upload.max_file_size'),
            'ext' => Config::get('upload.allow_file_ext')
        ];
        $info = $file->validate($validate)->move(Config::get('upload.save_path'));
        if (!$info) {
            return json(['code' => 1, 'msg' => $file->getError()]);
        }
        $filename = $info->getSaveName();
        $filepath = Config::get('upload.save_path') . $filename;
        $filesize = $file->getSize();
        $duration = $this->getVideoDuration($filepath);
        $this->deleteDuplicate($filepath);
        return json(['code' => 0, 'msg' => '上传成功', 'data' => [
            'filename' => $filename,
            'filesize' => $filesize,
            'duration' => $duration
        ]]);
    }

    private function getVideoDuration($filepath)
    {
        $ffprobe = Config::get('app.ffprobe_path');
        $cmd = "$ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $filepath";
        $duration = shell_exec($cmd);
        return round($duration);
    }

    private function deleteDuplicate($filepath)
    {
        $ffmpeg = Config::get('app.ffmpeg_path');
        $cmd = "$ffmpeg -i $filepath -vn -f wav - | md5sum";
        $hash = shell_exec($cmd);
        $hash = explode(' ', trim($hash))[0];
        $hashfile = Config::get('upload.hash_path') . $hash;
        if (Filesystem::has($hashfile)) {
            Filesystem::delete($filepath);
        } else {
            Filesystem::put($hashfile, '');
        }
    }
}

在配置文件中,需要设置上传文件的保存路径、允许上传的文件扩展名、最大文件大小、FFmpeg和FFprobe的路径。

这里呢配置文件大家可以分开来

return [
    'save_path' => '/var/www/html/uploads/',
    'hash_path' => '/var/www/html/hash/',
    'allow_file_ext' => 'mp4,flv,avi,mov,wmv',
    'max_file_size' => 1024 * 1024 * 100, // 100MB
     'ffmpeg_path' => '/usr/bin/ffmpeg',
    'ffprobe_path' => '/usr/bin/ffprobe',
    ];

前端页面交互
使用jQuery实现前端页面的交互,包括上传文件、展示去重结果。

$(function() {
    $('#upload-form').submit(function(e) {
        e.preventDefault();
        var formData = new FormData(this);
        $.ajax({
            url: '/index/upload',
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(res) {
                if (res.code === 0) {
                    var data = res.data;
                    var html = '<p>文件名:' + data.filename + '</p>' +
                        '<p>文件大小:' + formatFileSize(data.filesize) + '</p>' +
                        '<p>视频时长:' + formatTime(data.duration) + '</p>';
                    $('#result').html(html);
                } else {
                    alert(res.msg);
                }
            }
        });
    });
});

function formatFileSize(size) {
    var units = ['B', 'KB', 'MB', 'GB', 'TB'];
    var i = 0;
    while (size >= 1024 && i < units.length - 1) {
        size /= 1024;
        i++;
    }
    return size.toFixed(2) + units[i];
}

function formatTime(time) {
    var hours = Math.floor(time / 3600);
    var minutes = Math.floor((time - hours * 3600) / 60);
    var seconds = time % 60;
    return (hours > 0 ? hours + ':' : '') +
        (minutes < 10 ? '0' : '') + minutes + ':' +
        (seconds < 10 ? '0' : '') + seconds;
}

如你在使用过程中遇到问题可以与我一起讨论,在定制开发出联系我

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

-- 展开阅读全文 --
bootstrap模板分享可用于企业站等多种网站都实用
« 上一篇
视频去重方法无保留教学
下一篇 »
为了防止灌水评论,登录后即可评论!

HI ! 请登录
注册会员,享受下载全站资源特权。

最新文章

热门文章


网站定制|App定制|小程序定制|AI导航系统|AI提示词系统,一站式为您服务! 联系定制