通过 FFmpeg 和 PHP 创建目录中所有文件的视频缩略图

发布于 2025-01-01 10:05:02 字数 572 浏览 3 评论 0原文

我已经搜索遍了Google和StackOverFlow,但仍然没有找到解决方案。

我想生成目录中所有 mp4 视频文件的视频缩略图,并将缩略图命名为“filename.mp4”.jpg

我的服务器上安装了 ffmpeg 和 ffmpeg-php。我还成功地一次创建一个文件的缩略图。

所以情况就是这样,我有一个名为 uploads 的目录,其中有很多 mp4 视频。 现在,当我运行脚本时,应自动创建大小为 100x100 的缩略图并将其放置在另一个文件夹“skrin”中。例如: xxx.mp4 应该有 xxx.mp4.jpg 有拇指名称。

重要提示:我的文件名中包含空格、单引号、方括号等。所以脚本应该能够处理这个问题。

有人可以帮助我吗?我在 php 中使用以下 shell 命令,使用 exec 生成单个视频的缩略图。

exec("/usr/local/bin/ffmpeg -itsoffset -105 -i 'xxx haha.mp4' -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 'xxx haha.mp4.jpg'");

I have searched all over the Google and StackOverFlow, but still did not find a solution for this.

I want to generate video thumbnail of all mp4 video files in a directory and name the thumbnails as "filename.mp4".jpg

I have ffmpeg and ffmpeg-php installed on my server. I also succeeded in creating thumbnails of one file at a time.

So this is the situation, I have a directory named uploads which has lots of mp4 videos.
Now, when I run the script, thumbnail of size 100x100 shoud be created automatically and placed in another folder "skrin". Eg: xxx.mp4 should have xxx.mp4.jpg has the thumb name.

IMPORTANT: My filenames have spaces, single quotes, brackets etc in their file names. So the script should be able to handle this.

Could some one help me ? I use the following shell command in php using exec to generate thumb of an individual video.

exec("/usr/local/bin/ffmpeg -itsoffset -105 -i 'xxx haha.mp4' -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 'xxx haha.mp4.jpg'");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2025-01-08 10:05:02

这只是一个快速的:

$videos_dir = 'path/to/videos';
$videos_dir = opendir($videos_dir);
$output_dir = 'path/to/output/dir/';
while (false !== ($file = readdir($videos_dir))) {
    if ($file != '.' && $file != '..'){
        $in = $videos_dir.'/'.$file;
        $out = $output_dir.$file.'.jpg';
        exec("/usr/local/bin/ffmpeg -itsoffset -105 -i ".$in." -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 ".$out);
    }
}

It's just a quick one:

$videos_dir = 'path/to/videos';
$videos_dir = opendir($videos_dir);
$output_dir = 'path/to/output/dir/';
while (false !== ($file = readdir($videos_dir))) {
    if ($file != '.' && $file != '..'){
        $in = $videos_dir.'/'.$file;
        $out = $output_dir.$file.'.jpg';
        exec("/usr/local/bin/ffmpeg -itsoffset -105 -i ".$in." -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 ".$out);
    }
}
小耗子 2025-01-08 10:05:02

试试这个

try
    {
        $directory = 'your directory name';
        $dir = new RecursiveDirectoryIterator($directory);
        $it = new RecursiveIteratorIterator($dir);
        while($it->valid()) {

            if (!$it->isDot()) {
                //echo 'SubPathName: ' . $it->getSubPathName() . "\n";
                //echo 'SubPath:     ' . $it->getSubPath() . "\n";
                //echo 'Key:         ' . $it->key() . "\n\n";
                echo $name = $it->key(),"\n";
                exec("/usr/local/bin/ffmpeg -itsoffset -105 -i $name -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 $name.'.jpg'");    
            }

            $it->next();
        }
    }
    catch(Exception $e)
    {
        echo 'No files Found!<br />';
    }

try this

try
    {
        $directory = 'your directory name';
        $dir = new RecursiveDirectoryIterator($directory);
        $it = new RecursiveIteratorIterator($dir);
        while($it->valid()) {

            if (!$it->isDot()) {
                //echo 'SubPathName: ' . $it->getSubPathName() . "\n";
                //echo 'SubPath:     ' . $it->getSubPath() . "\n";
                //echo 'Key:         ' . $it->key() . "\n\n";
                echo $name = $it->key(),"\n";
                exec("/usr/local/bin/ffmpeg -itsoffset -105 -i $name -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 $name.'.jpg'");    
            }

            $it->next();
        }
    }
    catch(Exception $e)
    {
        echo 'No files Found!<br />';
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文