Split AVI into n Tif Stacks give an aberrant results

Hi There

I am using a macro to split an AVI file into n Tiff files of i Slices each.
It works well except that for some unknown reasons sometimes it gives files of unexpected length.
Here an example: I want to split an AVI file composed of 65536 slices (or frames since it is a video). I want to split it in 6 files of 10000 slices each and a 7th file of 5536 slices.

For some reasons the output file #4 is 5905 slices instead of 10 000 slices.

I can’t find if it is a macro problem. Here is the macro below.

/////////////////////// Initial Dialog Box
Dialog.create("Information");
Dialog.setInsets(0, 0, 0);
Dialog.addMessage("ImageJ will :"
+"\n- Prompt to select a folder containing AVI videos to split"
+"\n- Prompt for:"
+"\n- Maximum Nb of Slice per output file"
+"\n- Open AVI videos 1 by 1 from the selected folder"
+"\n- Split them into smaller files"
+"\n- Save them as TIF in a new folder");
Dialog.addNumber("Max Number of Slice:",10000);
Dialog.show();
/////////////////////// Initial Dialog Box
MaxNbSlices=Dialog.getNumber();


///////////////////// Functions
function SelectInputDir(){
  /////////////////////// Select Input Directory
  setOption("JFileChooser", true);
  InputDirPath=getDirectory("Select the FOLDER containing AVI Files to process.");
  setOption("JFileChooser", false);
  InputFileList=getFileList(InputDirPath);
  ParentDir=File.getParent(InputDirPath)+File.separator;
  InputDirName=File.getName(InputDirPath);
  /////////////////////// Select Input Directory
}


function CreateOutputDir(FolderSuffix){
  OutputDirPath=ParentDir+InputDirName+FolderSuffix+File.separator;
  n=1;
  while(File.exists(OutputDirPath)!=0){
    n++;
    OutputDirPath=ParentDir+InputDirName+FolderSuffix+"_"+n+File.separator;
  }
  File.makeDirectory(OutputDirPath);
  OutputDirName=File.getName(OutputDirPath);
}

SelectInputDir();
CreateOutputDir("_Processed");
var InputDirPath;
var InputFileList;
var ParentDir;
var InputDirName;
var OutputDirPath;
var OutputDirName;
var MaxNbSlices;
setBatchMode(true);

for (FileI=0; FileI < InputFileList.length; FileI++){
  InputFileNamei=InputFileList[FileI];
  InputFileNameiNoExt=replace(InputFileNamei, ".avi", "");
  InputFilePathi=InputDirPath+InputFileNamei;

// Open the file using virtual to get the file dimensions
run("AVI...", "open=&InputFilePathi use convert");
getDimensions(ImageWidth, ImageHeight, NbChannels, NbSlices, NbFrames); // Get dimensions of the Image
close();

// Calculate the Number of outputfiles
NbOutputFiles=floor(NbSlices/MaxNbSlices);
if(NbOutputFiles==(NbSlices/MaxNbSlices)){
  NbOutputFiles=NbOutputFiles;
}else{
  NbOutputFiles=NbOutputFiles+1;//Add one for the leftover
}

// Create the output files
for(OutputFileI=0; OutputFileI<NbOutputFiles;OutputFileI++){
  FirstSlice=(OutputFileI*MaxNbSlices)+1;
  LastSlice=(OutputFileI*MaxNbSlices)+MaxNbSlices;
  if(LastSlice>NbSlices){
    LastSlice=NbSlices;
  }

run("AVI...", "open=&InputFilePathi first=&FirstSlice last=&LastSlice convert");
 NbOutputFileI=OutputFileI+1;
saveAs("tiff",OutputDirPath+InputFileNameiNoExt+"_Part_"+NbOutputFileI+".tif");
 close();
}// End for OutputFileI
} // End for FIleI

Hi @nstifani,

I just tried your macro with a test video and cannot reproduce your issue. It works fine. I would suggest, you enter these two lines in the last for loop (just before run("Avi...",...) to trace what the program is actually doing:

print("firstslice " + FirstSlice);
print("lastslice " + LastSlice);

And of course, you may want to print more variables to actually find out whats wrong…

Cheers,
Robert

Thanks a lot

I will run it with the variable printing. I will update the post with new information very soon.

Hi there

I have printed the variable and it all looks good.

I have reproduced the problem.

It seems to be located between frames 35900 and 36000 only when opening the AVI as a NON-virtual stack.

Instead of opening 101 frames it opens only 6.

So I think I will just add “use” in the run (AVI…) command.