I'm not sure what I did but somehow when I ran my code it deleted my video files, that is, it reduced each video file to a mere 20-40Kb.
I'm using a directory tree iterator to go through the selected folder and iterate all the files and subfolders in there, with commands for each. I'm only doing the files at this stage, and the code simply
- renames the video file so that it has no spaces in it's name
- extracts a thumbnail using ffmpeg and places it in the videos directory
- inserts data in to database for displaying on a website.
Now I was able to use it once, and it worked save for 2 video files that I had been using for testing. For some reason those files were deleted. I tried running it again and it deleted all the videos.
What might be the problem?
$mysqli = new mysqli("localhost", "refit_2_admin", "asd123", "refit_2");
if($mysqli->connect_error)
{
exit('Error db');
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli->set_charset("utf8mb4");
$_SESSION['mysqli'] = $mysqli;
// DIRECTORY
$dir = 'videos';
$it = new RecursiveTreeIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
foreach($it as $path => $branch)
{
if(is_dir($path)) // if is a directory
{
// echo something
}
if(is_file($path)) // if is a file
{
$schism = pathinfo($path);
$string = str_replace(' ', '_', $schism['filename']);
//$string = str_replace($replace, $with, $string);
rename($path, $dir.'\\'.$string.'.mp4');
$ffmpeg = 'ffmpeg.exe';
$video = dirname(__FILE__) . '\\'.$dir.'\\'.$string.'.mp4';
//echo ''.$video.'<br>';
$image = dirname(__FILE__) . '\\'.$dir.'\\'.$string.'.jpg';
//echo ''.$image.'<br>';
$second = 1;
/*$cmd = "$ffmpeg -i $video 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($total - 1));
}*/
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
$return = `$cmd`;
$video_title = substr($schism['filename'], 0, -5);
$video_filename = $schism['filename'].'.mp4';
$video_thumbnail = $schism['filename'].'.jpg';
$video_include = basename(__DIR__);
// Insert
$stmt = $mysqli->prepare("INSERT INTO videos (video_title, video_filename, video_include, video_tags, video_thumbnail) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $video_title, $video_filename, $dir, $video_title, $video_thumbnail);
$stmt->execute();
$stmt->close();
}
}