【问题标题】:How to find missing .mp4 files in a directory?如何在目录中查找丢失的 .mp4 文件?
【发布时间】:2018-07-29 08:10:21
【问题描述】:

我的服务器上有这样的文件:

aaaa.flv
aaaa.mp4
bbbb.flv
bbbb.mp4
cccc.flv
dddd.flv
dddd.mp4

此代码不起作用:

$dir=$_SERVER{'DOCUMENT_ROOT'}."/test/";
foreach(glob($dir . "*.flv") as $file)
{
    $strip_ext=substr($file, 0, strrpos($file, "."));
    $mp4_ext=$strip_ext.".mp4";
    if (!file_exists($dir . $mp4_ext)) {
        echo "Non-matching pair! ".$strip_ext."<BR>";
    } 
}

如何找到我的.flv 文件没有匹配的.mp4

我宁愿在目录中放置一个脚本并运行它,让它吐出所有不匹配 .mp4 的文件名,这会告诉我需要转换的文件。

【问题讨论】:

  • 这些不是上传的文件。它们已经上传到服务器。
  • 使用globarray_reduce
  • “我会尝试使用 glob,但有成千上万个 unqiue 文件名” - 那么?您仍然需要以某种方式获取文件。
  • 你可以写一个简单的shell脚本或php脚本。如果你找到了,那又怎样?

标签: php comparison filenames glob file-conversion


【解决方案1】:

与其对给定的.mp4 文件是否存在进行迭代目录检查,我更愿意尝试(看看它是否阻塞):

  1. 提取所有 .flv 文件
  2. 提取所有 .mp4 文件
  3. 隔离丢失的.mp4 文件
  4. 开始转换

代码:(Mock Demo)

chdir($_SERVER{'DOCUMENT_ROOT'}."/test/");  // change working directory so that glob's output excludes the path
$flvs=array_map(function($f){return basename($f,'.flv');},glob("*.flv"));  // store .flv's and strip suffixes
$mp4s=array_map(function($f){return basename($f,'.mp4');},glob("*.mp4"));  // store .mp4's and strip suffixes
$need_conversion=array_diff($flvs,$mp4s);  // run ffmpeg() on these suffix-free files
// $need_conversion = array( 4 => 'cccc' )

【讨论】:

  • 如果这个“不起作用”,请描述它是如何失败的。您是否成功更改了chdir() 的目录?您需要不区分大小写的文件后缀匹配吗?它只是对目录卷造成崩溃吗?它会以某种方式生成不正确的列表吗?质量反馈至关重要。
猜你喜欢
  • 2013-01-22
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
相关资源
最近更新 更多