[[定点カメラ]] - カメラ画像を日にち単位に集める function move_file($yesterday, $yesterday2, $DBY, $camera) { $srcdir = "/mnt/dsk4/www/html/" . $camera . "/"; $destdir = "/mnt/dsk4/picture/" . $camera . "/" . $yesterday; $dir_pre= "snapshot_"; $file_pre= "motion-"; $file_dir = $dir_pre.$yesterday; // snapshot_20160316 $file_dir2 = $dir_pre.$DBY; // snapshot_20160315 $file_name = $file_pre.$yesterday2; // motion-2016-03-16 // ディレクトリの作成 mkdir($destdir, 0777); // カメラ画像を日にち単位に集める $dh = opendir($srcdir); while(false !== ($fn = readdir($dh))){ if($fn !== '.' && $fn !== '..' && is_dir($srcdir.'/'.$fn)){ // 対象となるディレクトリかのチェック if(strpos($fn, $file_dir, 0)===0 || strpos($fn, $file_dir2, 0)===0){ $targetdir = $srcdir.$fn; $dh2 = opendir($targetdir); while(false !== ($fn2 = readdir($dh2))){ if($fn2 !== '.' && $fn2 !== '..' && !is_dir($srcdir.'/'.$fn2)){ // 対象となるファイルかのチェック if(strpos($fn2, $file_name, 0)===0){ $srcfile = $srcdir.$fn.'/'.$fn2; $destfile = $destdir.'/'.$fn2; if(filesize($srcfile)){ copy($srcfile, $destfile); } } } } closedir($dh2); } } } closedir($dh); } - 画像にファイル名称を埋め込む。 動画の内容が何時に起きたかを確認するために、時間情報が必要です。~ 画像名称は motion-2015-03-15-11-30-43.jpg という感じに ”年月日時分秒”で作成されているので、タイムコードの代わりに埋め込みます。~ Windows では、IrfanView を使用していましたが、コマンドライン処理に対応していないため、ImageMagick に変更し、php から呼び出しを行っています。~ function fill_filename($yesterday, $camera) { $srcdir = "/mnt/dsk4/picture/".$camera."/".$yesterday; $destdir = "/mnt/dsk4/picture/".$camera."/".$yesterday."/movie"; $file_pre= "motion-"; $file_post= ".jpg"; // ディレクトリの作成 mkdir($destdir, 0777); // ファイル名で整頓する $dh = opendir($srcdir); $array_file = array(); while(false !== ($fn = readdir($dh))){ if($fn !== '.' && $fn !== '..' && !is_dir($srcdir.'/'.$fn)){ if(filesize($srcdir.'/'.$fn) != 0){ array_push($array_file, $fn); } } } closedir($dh); sort($array_file); // ファイル名を埋め込む $destno = 1; $destno5 = sprintf("%05d", $destno); foreach($array_file as $val){ $srcfile = $srcdir.'/'.$val; $destfile = $destdir . '/' . $file_pre . $destno5 . $file_post; $cmd = 'convert ' . $srcfile . ' -fill white -pointsize 12 -gravity SouthWest -annotate 0 %f ' . $destfile ; shell_exec($cmd); $destno ++; $destno5 = sprintf("%05d", $destno); } } - 静止画を連結し、mp4 動画を作成する。 function make_movie($yesterday, $camera) { /* 連番jpeg を avi file に変換 ffmpeg -i /mnt/dsk4/picture/qwatch2/20171125/movie/motion-%05d.jpg -r 29 -vcodec copy /mnt/dsk4/picture/qwatch2/20171125/movie/20171125.avi avi file を mp4 file に変換 ffmpeg -i /mnt/dsk4/picture/qwatch2/20171125/movie/20171125.avi -pix_fmt yuv420p -crf 18 /mnt/dsk1/www/html/qwatch2/tmp/20171125.mp4 */ $srcdir = "/mnt/dsk4/picture/".$camera."/".$yesterday; $destdir = "/mnt/dsk4/picture/".$camera."/".$yesterday."/movie"; $movie_dir = "/mnt/dsk1/www/html/".$camera."/tmp"; $src_file = "motion-%05d.jpg"; $avi_post = ".avi"; $mp4_post = ".mp4"; // ムービーにする $cmd = 'ffmpeg -i ' . $destdir . '/' . $src_file . ' -r 29 -vcodec copy ' . $destdir . '/' . $yesterday . $avi_post; shell_exec($cmd); $cmd = 'ffmpeg -i ' . $destdir . '/' . $yesterday . $avi_post . ' -pix_fmt yuv420p -crf 18 ' . $movie_dir . '/' . $yesterday . $mp4_post; shell_exec($cmd); } - 作業の終わった jpeg を削除。 function delete_file($yesterday, $yesterday2, $DBY, $camera) { $srcdir = "/mnt/dsk4/www/html/" . $camera . "/"; $destdir = "/mnt/dsk4/picture/" . $camera . "/" . $yesterday; $dir_pre= "snapshot_"; $file_pre= "motion-"; $file_dir = $dir_pre.$yesterday; // snapshot_20160316 $file_dir2 = $dir_pre.$DBY; // snapshot_20160315 $file_name = $file_pre.$yesterday2; // motion-2016-03-16 $cmd = 'cd ' . $srcdir ; shell_exec($cmd); $cmd = 'rm -Rf ' . $file_dir2 . "*" ; shell_exec($cmd); $cmd = 'rm -Rf ' . $destdir ; shell_exec($cmd); }