Commit e464f8c6 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

added moving srcs, reading result extensions from exif, scanning 1 dir down,...

added moving srcs, reading result extensions from exif, scanning 1 dir down, help message for command line usage
parent 23d05586
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
/*!******************************************************************************* /**
*! FILE NAME : split_mov.php * @file extract_images.php
*! DESCRIPTION: splits a *.mov file into frames naming them by the timestamp * @brief split mov/bin/img's (by exif header) into single image files - looks one dir down from the specified path
*! Copyright (C) 2011 Elphel, Inc * @copyright Copyright (C) 2017 Elphel Inc.
*! -----------------------------------------------------------------------------** * @author Elphel Inc. <support-list@support.elphel.com>
*! This program is free software: you can redistribute it and/or modify *
*! it under the terms of the GNU General Public License as published by * @par <b>License</b>:
*! the Free Software Foundation, either version 3 of the License, or * This program is free software: you can redistribute it and/or modify
*! (at your option) any later version. * it under the terms of the GNU General Public License as published by
*! * the Free Software Foundation, either version 3 of the License, or
*! This program is distributed in the hope that it will be useful, * (at your option) any later version.
*! but WITHOUT ANY WARRANTY; without even the implied warranty of *
*! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * This program is distributed in the hope that it will be useful,
*! GNU General Public License for more details. * but WITHOUT ANY WARRANTY; without even the implied warranty of
*! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*! The four essential freedoms with GNU GPL software: * GNU General Public License for more details.
*! * the freedom to run the program for any purpose *
*! * the freedom to study how the program works and change it to make it do what you wish * You should have received a copy of the GNU General Public License
*! * the freedom to redistribute copies so you can help your neighbor * along with this program. If not, see <http://www.gnu.org/licenses/>.
*! * the freedom to distribute copies of your modified versions to others
*!
*! You should have received a copy of the GNU General Public License
*! along with this program. If not, see <http://www.gnu.org/licenses/>.
*! -----------------------------------------------------------------------------**
*! $Log: split_mov.php,v $
*!
*/ */
set_time_limit(60*60*24); set_time_limit(60*60*24);
$chunksize=10000000; // 10MB $chunksize=10000000; //10MB
$startMarkerWithExif=chr(hexdec("ff")).chr(hexdec("d8")).chr(hexdec("ff")).chr(hexdec("e1")); $startMarkerWithExif=chr(hexdec("ff")).chr(hexdec("d8")).chr(hexdec("ff")).chr(hexdec("e1"));
$input_exts = array("img","bin","mov");
echo "<pre>\n"; // use current dir
$path=".";
$destination = "0";
// if (!isset($_GET['path']) || !isset($_GET['ext'])) { $move_processed = false;
// echo "Usage split_mov.php?path=&lt;path_of_the_mov_file&gt;&ext=&lt;extension&gt;"; $processed_subdir = "processed";
// //echo "Usage split_mov.php?path=path_of_the_mov_file&ext=extension";
// echo "</pre>\n";
// exit (1);
// }
if (isset($_GET['path'])) $path=$_GET['path']; $forced_ext = "";
else if ($argc == 2)
$path = $argv[1];
else
$path=".";
if (isset($_GET['ext'])) $extension = $_GET['ext']; function print_help(){
else $extension = "jp4"; global $argv;
if (isset($_GET['dest_path'])) $destination = $_GET['dest_path']; echo <<<"TXT"
else $destination = "result"; Help:
* Usage:
~$ {$argv[0]} path=[path-to-dir] dest_path=[dest-subdir] move_processed=[move-processed-files] ext=[forced-ext]
where:
* path-to-dir - string - scan this path + 1 dir down
* dest-subdir - string - save results to "path-to-dir/dest-subdir/"
* move-processed-files - 0(default) or 1 - if not 1 - will not move the processed files
* forced-ext - string - override extensions from exifs with this one
$files = scandir("$path"); * Examples:
** Split all *.img, *.bin and *.mov files in the current dir and 1 dir down, puts results to '0/':
~$ {$argv[0]}
** Split in /data/test + 1 dir down, create and move processed files to /data/test/processed for files in path and /data/test/any-found-subdir/processed for any files found in /data/test/any-found-subdir
~$ {$argv[0]} path=/data/test move_processed=1
** Split all *.img, *.bin and *.mov files in the current dir and 1 dir down, puts results to 'results/', override extensions with 'jpg':
~$ {$argv[0]} dest_path=results ext=jpg
if (!is_dir("$path/$destination")) mkdir("$path/$destination",0777); TXT;
}
foreach ($files as $file) { if ($argv){
$ext = get_file_extension($file); foreach($argv as $k=>$v){
if ($ext == "img" || $ext == "bin" || $ext == "mov") { if ($k==0) continue;
echo "Splitting $path/$file into {$extension}s\n"; $args = explode("=",$v);
split_mov("$path",$file,$destination,$extension,$startMarkerWithExif,$chunksize); if (isset($args[1])) $_GET[$args[0]] = $args[1];
if ($v=="-h"||$v=="--help"||$v=="help") {
print_help();
die();
} }
}
}
if (isset($_GET['path'])){
$path=$_GET['path'];
} }
// put results to $path/$destination/
// no need for argv
if (isset($_GET['dest_path'])){
$destination = $_GET['dest_path'];
}
if (isset($_GET['move_processed'])){
$move_processed = $_GET['move_processed'];
}
function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) { if ($move_processed=="1"){
$move_processed = true;
}else{
$move_processed = false;
}
$path_with_name = "$path/$mov_file"; // enforce extension
if (isset($_GET['ext'])){
$forced_ext = $_GET['ext'];
}
if (!is_file($path_with_name)) { $list = preg_grep('/^([^.])/', scandir($path));
return -1;
if (!is_dir("$path/$destination")) mkdir("$path/$destination",0777);
foreach ($list as $item) {
if (is_dir("$path/$item")){
if ($item==$processed_subdir) continue;
$sublist = preg_grep('/^([^.])/', scandir("$path/$item"));
foreach($sublist as $subitem){
if (split_file("$item/$subitem")==0){
if ($move_processed){
if (!is_dir("$path/$item/$processed_subdir")){
mkdir("$path/$item/$processed_subdir",0777);
}
rename("$path/$item/$subitem","$path/$item/$processed_subdir/$subitem");
}
}
} }
}else{
if (split_file("$item")==0){
if ($move_processed){
if (!is_dir("$path/$processed_subdir")){
mkdir("$path/$processed_subdir",0777);
}
rename("$path/$item","$path/$processed_subdir/$item");
}
}
}
}
$file=fopen($path_with_name,'r'); function split_file($file){
$markers=array(0); global $path;
$offset=0; global $destination;
global $startMarkerWithExif;
global $chunksize;
global $input_exts;
while (!feof($file)) { if (in_array(get_ext("$path/$file"),$input_exts)) {
fseek($file,$offset); echo "Splitting $path/$file\n";
$s = fread($file,$chunksize); //split_mov("$path",$file,$destination,$extension,$startMarkerWithExif,$chunksize);
$index=0;
$markers=array();
$offset =0;
$f=fopen("$path/$file","r");
//first scan
while (!feof($f)) {
$pos=0; $pos=0;
while (true) { $index=0;
fseek($f,$offset);
$s = fread($f,$chunksize);
while(true){
$pos=strpos($s,$startMarkerWithExif,$pos); $pos=strpos($s,$startMarkerWithExif,$pos);
if ($pos === false) break; if ($pos === false) break;
$markers[count($markers)]=$offset+$pos; $markers[count($markers)]=$offset+$pos;
...@@ -97,27 +163,43 @@ function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) { ...@@ -97,27 +163,43 @@ function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) {
$markers[count($markers)]=$offset+strlen($s); // full length of the file $markers[count($markers)]=$offset+strlen($s); // full length of the file
echo " images found: ".(count($markers)-1)."\n";
//second scan
for ($i=1;$i<(count($markers)-1);$i++) { for ($i=1;$i<(count($markers)-1);$i++) {
fseek($file,$markers[$i]);
$s = fread($file,$markers[$i+1]-$markers[$i]);
$old_file_name= "$path/tmp.".$ext; fseek($f,$markers[$i]);
$s = fread($f,$markers[$i+1]-$markers[$i]);
$outFile=fopen($old_file_name,'w'); $tmp_name = "$path/$destination/image.tmp";
fwrite($outFile,$s); file_put_contents($tmp_name,$s);
fclose($outFile);
//read exif & rename $result_name = elphel_specific_result_name($tmp_name);
$exif_data = exif_read_data($old_file_name);
rename($tmp_name,"$path/$destination/$result_name");
}
return 0;
}else{
return -1;
}
}
function elphel_specific_result_name($file){
$exif = exif_read_data($file);
$ext = elphel_specific_result_ext($exif);
//converting GMT a local time GMT+7 //converting GMT a local time GMT+7
$DateTimeOriginal_local=strtotime($exif_data['DateTimeOriginal']);/*-25200;*/ $timestamp_local=strtotime($exif['DateTimeOriginal']);/*-25200;*/
$tmp = explode("_",$exif_data['Model']); $subsecs = $exif['SubSecTimeOriginal'];
$tmp = explode("_",$exif['Model']);
if (count($tmp)==2){ if (count($tmp)==2){
$model = intval(trim($tmp[1])); $model = intval(trim($tmp[1]));
$chn = intval($exif_data['PageNumber'])+1; $chn = intval($exif['PageNumber'])+1;
if ($model==1001) { if ($model==1001) {
$k=$chn; $k=$chn;
}else if ($model==1002) { }else if ($model==1002) {
...@@ -125,18 +207,38 @@ function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) { ...@@ -125,18 +207,38 @@ function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) {
}else if ($model==1003) { }else if ($model==1003) {
$k=$chn+6; $k=$chn+6;
} }
} else { }else{
$k = intval($exif_data['PageNumber'])+1; $k = intval($exif['PageNumber'])+1;
} }
$new_file_name = $DateTimeOriginal_local."_".$exif_data['SubSecTimeOriginal']."_".$k.".".$ext;
rename($old_file_name,"$path/$dest/$new_file_name"); return "{$timestamp_local}_{$subsecs}_$k.$ext";
}
return 0;
} }
function get_file_extension($filename) { function get_ext($filename) {
return pathinfo($filename, PATHINFO_EXTENSION); return pathinfo($filename, PATHINFO_EXTENSION);
} }
/**
* read image format and return extension, elphel elphel_specific
* @param array $exif Array returned from the PHP's built-in exif_read_data function
* @return string extension - jpeg or jp4
*/
function elphel_specific_result_ext($exif){
global $forced_ext;
//default value
$ext = "jpeg";
if ($forced_ext==""){
if (isset($exif['MakerNote'][10])){
$record = ($exif['MakerNote'][10]>>4)&0xf;
if ($record==5) $ext = "jp4";
}
}else{
$ext = $forced_ext;
}
return $ext;
}
?> ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment