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'];
else $extension = "jp4";
if (isset($_GET['dest_path'])) $destination = $_GET['dest_path'];
else $destination = "result";
function print_help(){
global $argv;
echo <<<"TXT"
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
* 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
$files = scandir("$path"); TXT;
if (!is_dir("$path/$destination")) mkdir("$path/$destination",0777); }
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'];
}
function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) { if (isset($_GET['move_processed'])){
$move_processed = $_GET['move_processed'];
}
$path_with_name = "$path/$mov_file"; if ($move_processed=="1"){
$move_processed = true;
}else{
$move_processed = false;
}
if (!is_file($path_with_name)) { // enforce extension
return -1; if (isset($_GET['ext'])){
} $forced_ext = $_GET['ext'];
}
$file=fopen($path_with_name,'r'); $list = preg_grep('/^([^.])/', scandir($path));
$markers=array(0); if (!is_dir("$path/$destination")) mkdir("$path/$destination",0777);
$offset=0;
while (!feof($file)) { foreach ($list as $item) {
fseek($file,$offset); if (is_dir("$path/$item")){
$s = fread($file,$chunksize); if ($item==$processed_subdir) continue;
$index=0; $sublist = preg_grep('/^([^.])/', scandir("$path/$item"));
$pos=0; foreach($sublist as $subitem){
while (true) { if (split_file("$item/$subitem")==0){
$pos=strpos($s,$startMarkerWithExif,$pos); if ($move_processed){
if ($pos === false) break; if (!is_dir("$path/$item/$processed_subdir")){
$markers[count($markers)]=$offset+$pos; mkdir("$path/$item/$processed_subdir",0777);
$pos++; }
} rename("$path/$item/$subitem","$path/$item/$processed_subdir/$subitem");
$offset+=(strlen($s)-strlen($startMarkerWithExif)+1); // so each marker will appear once }
} }
}
}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");
}
}
}
}
$markers[count($markers)]=$offset+strlen($s); // full length of the file function split_file($file){
global $path;
global $destination;
global $startMarkerWithExif;
global $chunksize;
global $input_exts;
if (in_array(get_ext("$path/$file"),$input_exts)) {
echo "Splitting $path/$file\n";
//split_mov("$path",$file,$destination,$extension,$startMarkerWithExif,$chunksize);
$markers=array();
$offset =0;
$f=fopen("$path/$file","r");
//first scan
while (!feof($f)) {
$pos=0;
$index=0;
fseek($f,$offset);
$s = fread($f,$chunksize);
while(true){
$pos=strpos($s,$startMarkerWithExif,$pos);
if ($pos === false) break;
$markers[count($markers)]=$offset+$pos;
$pos++;
}
$offset+=(strlen($s)-strlen($startMarkerWithExif)+1); // so each marker will appear once
}
$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($f,$markers[$i]);
fseek($file,$markers[$i]); $s = fread($f,$markers[$i+1]-$markers[$i]);
$s = fread($file,$markers[$i+1]-$markers[$i]);
$old_file_name= "$path/tmp.".$ext; $tmp_name = "$path/$destination/image.tmp";
file_put_contents($tmp_name,$s);
$outFile=fopen($old_file_name,'w'); $result_name = elphel_specific_result_name($tmp_name);
fwrite($outFile,$s);
fclose($outFile);
//read exif & rename rename($tmp_name,"$path/$destination/$result_name");
$exif_data = exif_read_data($old_file_name); }
//converting GMT a local time GMT+7 return 0;
$DateTimeOriginal_local=strtotime($exif_data['DateTimeOriginal']);/*-25200;*/ }else{
return -1;
$tmp = explode("_",$exif_data['Model']); }
}
if (count($tmp)==2){
$model = intval(trim($tmp[1])); function elphel_specific_result_name($file){
$chn = intval($exif_data['PageNumber'])+1;
if ($model==1001) { $exif = exif_read_data($file);
$k=$chn;
}else if ($model==1002) { $ext = elphel_specific_result_ext($exif);
$k=$chn+4;
}else if ($model==1003) { //converting GMT a local time GMT+7
$k=$chn+6; $timestamp_local=strtotime($exif['DateTimeOriginal']);/*-25200;*/
}
} else { $subsecs = $exif['SubSecTimeOriginal'];
$k = intval($exif_data['PageNumber'])+1;
} $tmp = explode("_",$exif['Model']);
$new_file_name = $DateTimeOriginal_local."_".$exif_data['SubSecTimeOriginal']."_".$k.".".$ext; if (count($tmp)==2){
$model = intval(trim($tmp[1]));
rename($old_file_name,"$path/$dest/$new_file_name"); $chn = intval($exif['PageNumber'])+1;
} if ($model==1001) {
return 0; $k=$chn;
}else if ($model==1002) {
$k=$chn+4;
}else if ($model==1003) {
$k=$chn+6;
}
}else{
$k = intval($exif['PageNumber'])+1;
}
return "{$timestamp_local}_{$subsecs}_$k.$ext";
} }
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;
} }
?> ?>
\ No newline at end of file
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