anchor.diff:  
   :FFMPEGOK
   rem Create temporary directories
  -mkdir %FRAMESTEMPDIR%
  -mkdir %OUTTEMPDIR%
  +set FRAMESTEMP=%MYTEMPDIR%\%FRAMESTEMPDIR%
  +set STITCHEDTEMP=%MYTEMPDIR%\%STITCHEDTEMPDIR%
  +mkdir %FRAMESTEMP%
  +mkdir %STITCHEDTEMP%
   
   rem Execute commands (as simple as it is)
   echo Converting video to images...
  -"%FFMPEGPATH%/ffmpeg.exe" -y -i %1 %FRAMESTEMPDIR%/%IMAGETMPLDEC%
  +"%FFMPEGPATH%/ffmpeg.exe" -y -i %1 %FRAMESTEMP%/%IMAGETMPLDEC%
   if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
  +
  +rem Detect video size and match Hugin template file
  +TMPVIDSIZE=%MYTEMPDIR%\vidsize.tmp
  +"%FFMPEGPATH%/ffprobe.exe" -v error ^
  +                           -of csv ^
  +                           -select_streams v:0 ^
  +                           -show_entries stream=height,width ^
  +                           %INNAME% > %TMPVIDSIZE%
  +
  +for /f "tokens=1-18* delims=," %%A in (%TMPVIDSIZE%) do (
  +  VIDSIZE=%%~B:%%~C
  +)
  +del %TMPVIDSIZE%
  +
  +if "%VIDSIZE%"=="4096:2048" set PTOTMPL=%PTOTMPL4096%
  +if "%VIDSIZE%"=="3840:1920" set PTOTMPL=%PTOTMPL3840%
  +if "%VIDSIZE%"=="2560:1280" set PTOTMPL=%PTOTMPL2560%
  +
  +rem Detect framerate
  +TMPFPS=%MYTEMPDIR%\vidfps.tmp
  +"%FFMPEGPATH%/ffprobe.exe" -v error ^
  +                           -of csv ^
  +                           -select_streams v:0 ^
  +                           -show_entries stream=r_frame_rate ^
  +                           %INNAME% > %TMPFPS%
  +
  +for /f "tokens=1-18* delims=," %%A in (%TMPFPS%) do (
  +  VIDFPS=%%~B
  +)
  +del %TMPFPS%
   
   rem Stitching
   echo Stitching frames...
  -for %%f in (%FRAMESTEMPDIR%/*.jpg) do (
  +for %%f in (%FRAMESTEMP%/*.jpg) do (
   rem For whatever reason (this has to be at the beginning of the line!)
  -  echo Processing frame %FRAMESTEMPDIR%\%%f
  +  echo Processing frame %FRAMESTEMP%\%%f
   rem TODO: There should be some error checking
  -  call gear360pano.cmd /m /o %OUTTEMPDIR% %FRAMESTEMPDIR%\%%f %PTOTMPL%
  +  call gear360pano.cmd /m /o %STITCHEDTEMP% %FRAMESTEMP%\%%f %PTOTMPL%
   )
   
   echo "Reencoding video..."
  -"%FFMPEGPATH%/ffmpeg.exe" -y -f image2 -i %OUTTEMPDIR%/%IMAGETMPLENC% -r 30 -s 3840:1920 -vcodec libx264 %OUTTEMPDIR%/%TMPVIDEO%
  +"%FFMPEGPATH%/ffmpeg.exe" -y -f image2 -i %STITCHEDTEMP%/%IMAGETMPLENC% -r %VIDFPS% -s %VIDSIZE% %FFMPEGQUALITYENC% %STITCHEDTEMP%/%TMPVIDEO%
   if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
   
  -echo "Extracting audio..."
  -"%FFMPEGPATH%/ffmpeg.exe" -y -i %1 -vn -acodec copy %OUTTEMPDIR%/%TMPAUDIO%
  -if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
  -
  -echo "Merging audio..."
  -
  -rem Check if second argument present, if not, set some default for output filename
  -rem This is here, because for whatever reason OUTNAME gets overriden by
  -rem the last iterated filename if this is at the beginning (for loop is buggy?)
  -if not [%2] == [] goto SETNAMEOK
  -set OUTNAME="%~n1_pano.mp4"
  -
  -:SETNAMEOK
  -"%FFMPEGPATH%/ffmpeg.exe" -y -i %OUTTEMPDIR%/%TMPVIDEO% -i %OUTTEMPDIR%/%TMPAUDIO% -c:v copy -c:a aac -strict experimental %OUTNAME%
  -if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
  +rem Check if there's audio
  +TMPHASAUDIO=%MYTEMPDIR%\hasaudio.tmp
  +"%FFMPEGPATH%/ffprobe.exe" -v error ^
  +                          -of default=nw=1:nk=1 ^
  +                          -select_streams a ^
  +                          -show_entries stream=codec_type ^
  +                          %INNAME% > %TMPHASAUDIO%
  +
  +HASAUDIO=<%TMPHASAUDIO%
  +del %TMPHASAUDIO%
  +
  +if %HASAUDIO% neq "" (
  +  echo "Extracting audio..."
  +  "%FFMPEGPATH%/ffmpeg.exe" -y -i %1 -vn -acodec copy %STITCHEDTEMP%/%TMPAUDIO%
  +  if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
  +
  +  echo "Merging audio..."
  +  "%FFMPEGPATH%/ffmpeg.exe" -y -i %STITCHEDTEMP%/%TMPVIDEO% -i %STITCHEDTEMP%/%TMPAUDIO% -c:v copy -c:a aac -strict experimental %OUTNAME%
  +  if %ERRORLEVEL% EQU 1 GOTO FFMPEGERROR
  +)
   
   rem Clean-up (f - force, read-only & dirs, q - quiet)
   del /f /q %FRAMESTEMPDIR%
  
positive.code: @echo off
  
  rem Script to stitch panoramic videos produced by Samsung Gear360 (and others?).
  
  rem For help (hopefully) see:
  rem https://github.com/ultramango/gear360pano
  
  rem Names:
  rem dec, DEC, decoding - means video -> images
  rem enc, ENC, encoding - means stitched images -> video
  
  set FFMPEGPATH=c:\Program Files\ffmpeg\bin
  set FRAMESTEMPDIR=frames
  set OUTTEMPDIR=frames_stitched
  set PTOTMPL=gear360video3840.pto
  rem %% is an escape character (note: this will fail on wine's cmd.exe)
  set IMAGETMPLDEC=image%%05d.jpg
  set IMAGETMPLENC=image%%05d_pano.jpg
  set TMPAUDIO=tmpaudio.aac
  set TMPVIDEO=tmpvideo.mp4
  set DEBUG=""
  
  rem Check arguments
  IF [%1] == [] GOTO NOARGS
  
  :SETNAMEOK
  rem Check ffmpeg...
  
negative.code: goto eof
  
  :NOARGS
  echo Script to stitch raw panoramic videos.
  echo Raw meaning two fisheye images side by side.
  echo.
  echo Script originally writen for Samsung Gear 360.
  echo.
  echo Usage:
  echo %0 [options] infile [outfile]
  echo.
  echo Where inputfile is a panoramic video, outfile
  echo is optional. Video file will be written
  echo to a file with appended _pano, ex.: dummy.mp4 will
  echo be stitched to dummy_pano.mp4.
  echo.
  echo /o will set the output directory of panoramas
  echo    default: html/data
  echo /s optimise for speed (lower quality)
  echo /t set temporary directory (default: system's
  echo    temporary directory)
  echo /h prints this help
  goto eof
  
  :NOFFMPEG
  echo ffmpeg was not found in %FFMPEGPATH%, download from: https://ffmpeg.zeranoe.com/builds/
  echo and unpack to program files directory (name it ffmpeg)
  goto eof
  
  :FFMPEGERROR
  