Commit e464f8c6 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev
Browse files

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
Loading
Loading
Loading
Loading
+211 −109
Original line number Diff line number Diff line
#!/usr/bin/env php
<?php
/*!*******************************************************************************
*! FILE NAME  : split_mov.php
*! DESCRIPTION: splits a *.mov file into frames naming them by the timestamp
*! Copyright (C) 2011 Elphel, Inc
*! -----------------------------------------------------------------------------**
*!  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
*!  the Free Software Foundation, either version 3 of the License, or
*!  (at your option) any later version.
*!
*!  This program is distributed in the hope that it will be useful,
*!  but WITHOUT ANY WARRANTY; without even the implied warranty of
*!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*!  GNU General Public License for more details.
*!
*!  The four essential freedoms with GNU GPL software:
*!  * 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
*!  * the freedom to redistribute copies so you can help your neighbor
*!  * 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 $
*! 
/**
 * @file extract_images.php
 * @brief split mov/bin/img's (by exif header) into single image files - looks one dir down from the specified path
 * @copyright Copyright (C) 2017 Elphel Inc.
 * @author Elphel Inc. <support-list@support.elphel.com>
 *
 * @par <b>License</b>:
 *  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
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

set_time_limit(60*60*24);

$chunksize=10000000; //10MB 
$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'])) {
// 	echo "Usage split_mov.php?path=&lt;path_of_the_mov_file&gt;&ext=&lt;extension&gt;";
// 	//echo "Usage split_mov.php?path=path_of_the_mov_file&ext=extension";
// 	echo "</pre>\n";
// 	exit (1);
// }
$move_processed = false;
$processed_subdir = "processed";

if (isset($_GET['path'])) $path=$_GET['path'];
else if ($argc == 2)
	$path = $argv[1];
else
	$path=".";
$forced_ext = "";

if (isset($_GET['ext'])) $extension = $_GET['ext']; 
else                     $extension = "jp4";
function print_help(){
  global $argv;
  
if (isset($_GET['dest_path'])) $destination = $_GET['dest_path']; 
else                           $destination = "result";
  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
    
$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) {
	$ext = get_file_extension($file);
    if ($ext == "img" || $ext == "bin" || $ext == "mov") {
	echo "Splitting $path/$file into {$extension}s\n";
	split_mov("$path",$file,$destination,$extension,$startMarkerWithExif,$chunksize);
}

if ($argv){
  foreach($argv as $k=>$v){
    if ($k==0) continue;
    $args = explode("=",$v);
    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)) {
		return -1;
// enforce extension
if (isset($_GET['ext'])){
  $forced_ext = $_GET['ext'];
}

$list = preg_grep('/^([^.])/', scandir($path));

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");
      }
    }
  }
}

function split_file($file){
  
	$file=fopen($path_with_name,'r');
  global $path;
  global $destination;
  global $startMarkerWithExif;
  global $chunksize;
  global $input_exts;
  
	$markers=array(0);
  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;
    
	while (!feof($file)) {
		fseek($file,$offset);
		$s = fread($file,$chunksize);
		$index=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;
@@ -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
   
    echo "  images found: ".(count($markers)-1)."\n";
   
    //second scan
    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]);

      $tmp_name = "$path/$destination/image.tmp";
      file_put_contents($tmp_name,$s);

      $result_name = elphel_specific_result_name($tmp_name);

		$outFile=fopen($old_file_name,'w');
		fwrite($outFile,$s);
		fclose($outFile);
      rename($tmp_name,"$path/$destination/$result_name");
    }
    
    return 0;
  }else{
    return -1;
  }
}

		//read exif & rename
		$exif_data = exif_read_data($old_file_name);
function elphel_specific_result_name($file){

  $exif = exif_read_data($file);
  
  $ext = elphel_specific_result_ext($exif);
  
  //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){
    $model = intval(trim($tmp[1]));
                  $chn = intval($exif_data['PageNumber'])+1;
    $chn = intval($exif['PageNumber'])+1;
    if        ($model==1001) {
      $k=$chn;
    }else  if ($model==1002) {
@@ -126,17 +208,37 @@ function split_mov($path,$mov_file,$dest,$ext,$startMarkerWithExif,$chunksize) {
      $k=$chn+6;
    }
  }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 0;
  return "{$timestamp_local}_{$subsecs}_$k.$ext";
  
}

function get_file_extension($filename) {
function get_ext($filename) {
  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;
}

?>