Skip to content

Instantly share code, notes, and snippets.

@bcheck555
Forked from Krzysiu/extractAndGeotagFrames.bat
Last active August 17, 2023 18:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bcheck555/a656bcecc351e110f5cb19faac812a38 to your computer and use it in GitHub Desktop.
Save bcheck555/a656bcecc351e110f5cb19faac812a38 to your computer and use it in GitHub Desktop.
Extract frames from video every X seconds and geotag them using GPS data file. Requires exiftool and ffmpeg. Modified to prep Insta360 video for Streetview.
@echo off
rem Updates to prep Insta360 One X2 video for Streetview upload.
rem Brian Check
rem 13 Mar 2021
rem Ksheesh Geotag Video Frames 0.0.1
rem
rem Extract frames from video every X seconds and geotag them using GPS data file.
rem Requires exiftool and ffmpeg.
rem Copyright (c) 2017 krzysiu.net
rem
rem Permission is hereby granted, free of charge, to any person obtaining a copy
rem of this software and associated documentation files (the "Software"), to deal
rem in the Software without restriction, including without limitation the rights
rem to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rem copies of the Software, and to permit persons to whom the Software is
rem furnished to do so, subject to the following conditions:
rem
rem The above copyright notice and this permission notice shall be included in all
rem copies or substantial portions of the Software.
rem
rem THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rem IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rem FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
rem AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rem LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rem OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
rem SOFTWARE.
setlocal EnableDelayedExpansion
rem Config starts here. Remember:
rem * Paths and names: don't use quotes, even if it has spaces in the string.
rem * Binaries: if they are in shared path, you may set just the binary name,
rem i.e. "ffmpeg" (without quotes) for ffmpegBin variable.
rem Path to ffmpeg binary.
set ffmpegBin=c:\bin\ffmpeg.exe
rem Path to exiftool binary.
set exiftoolBin=c:\bin\exiftool.exe
rem Input video file
rem This is the stiched MP4 from app or Insta360 Studio. h264 and h265 both work.
set videoFile=C:\bin\videos\_VID_20210312_164911_00_021.mp4
rem The source of GPS data, i.e. GPX or other supported file. Wildcards are also
rem possible, e.g. gpsDataFile=c:\gps_data\*.gpx
rem Create .gpx using following command:
rem exiftool.exe -ee -api largefilesupport -p .\gpx.fmt .\VID_20210312_063451_00_019.insv > .\VID_20210312_063451_00_019.gpx
rem use Command Prompt, Powershell will cause issues, exiftool was unable to read file format.
rem use the larger of the insv files, should be the file with 00.
set gpsDataFile=C:\bin\gpx\VID_20210312_164911_00_021.gpx
rem Output directory for frame images (must exist; without trailing slash)
set outputDirectory=C:\bin\frames
rem Prefix which will be added to image file names
set framePrefix=frame_
rem Padding digit count (1-9) in frame file names (i.e. 3 will make 001.jpg and 2 will make 01.jpg)
set paddingDigits=4
rem Get frame every X seconds.
set getFrameEvery=3
rem Get timezone offset from UTC
set offsetTime=-05:00
rem GPS Offset from video file
rem Insta360 insv does not seem to start recording GPS until 10 seconds in
set gpsOffset=+00:10
rem Video framerate
rem This does not work, search for timeShift and modify function to correct for actual framerate, currently 29.97fps
set /a frameRate=30000/1001
rem Date and time of the start of the video. You may use:
rem * DateTimeOriginal - if the file has DateTimeOriginal tag
rem * FileCreateDate - set timestamp to file creation date
rem * FileModifyDate - set timestamp to file modify date
rem * 0 - own timestamp (see videoTimestamp variable)
rem INSV files do not have the above fields. CreateDate works.
set dateSource=CreateDate
rem If dateSource is set to 0, set here date and time of the start of the video.
rem The format is yyyy:mm:dd hh:mm:ss
rem set videoTimestamp=2017:01:01 00:00:00
set videoTimestamp=2021:03:01 22:12:31
rem Config ends here.
set exifDateParam=-tagsFromFile "%videoFile%" "-datetimeoriginal<%dateSource%" "-OffsetTimeOriginal=%offsetTime%"
if %dateSource% equ 0 set exifDateParam="-datetimeoriginal=%videoTimestamp%Z"
if not exist "%outputDirectory%" (
set errMsg=Can't access output directory ^(%outputDirectory%^)
goto error
)
if not exist "%videoFile%" (
set errMsg=Can't access video file ^(%videoFile%^)
goto error
)
pushd %outputDirectory%
echo Extracting frames...
%ffmpegBin% -loglevel error -hide_banner -stats -i "%videoFile%" -q:v 2 -vf fps=fps=1/%getFrameEvery%:start_time=0:round=zero %framePrefix%%%0%paddingDigits%d.jpg
if %errorlevel% neq 0 (
set errMsg=ffmpeg returned error
goto error
)
echo Done!
echo.
echo Setting base timestamp to files.
set fileNumber=0
%exiftoolBin% %exifDateParam% -overwrite_original -globaltimeshift "+0:0:0 0:0:!timeShift!" "%outputDirectory%\%framePrefix%*.jpg"
echo Done!
echo.
echo Setting timestamp offsets.
for %%f in ("%outputDirectory%\%framePrefix%*.jpg") do (
set /a "timeShift=((!fileNumber!*%getFrameEvery%)+((%getFrameEvery%*30)-(%getFrameEvery%*(30000/1001))))"
if !fileNumber! neq 0 %exiftoolBin% -P -m -overwrite_original "-datetimeoriginal+=0:0:0 0:0:!timeShift!" "%%~nxf"
set /a "fileNumber=!fileNumber!+1"
)
echo Done!
echo.
echo Geotagging images.
%exiftoolBin% -P -geotag "%gpsDataFile%" "-geosync=%gpsOffset%" "-geotime<SubSecDateTimeOriginal" -overwrite_original "%outputDirectory%\%framePrefix%*.jpg"
rem %exiftoolBin% -P -geotag "%gpsDataFile%" -overwrite_original "%outputDirectory%\%framePrefix%*.jpg"
echo Done!
echo.
goto theend
:error
echo %errMsg%. Exitting.
:theend
pause
popd
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment