Skip to content

Instantly share code, notes, and snippets.

@Krzysiu
Created November 22, 2017 19:28
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Krzysiu/345c37b08d79d5f47971167e33bb2bd3 to your computer and use it in GitHub Desktop.
Save Krzysiu/345c37b08d79d5f47971167e33bb2bd3 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.
@echo off
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 Input video file
set videoFile=c:\video\sample
rem Prefix which will be added to image file names
set framePrefix=frame_
rem Output directory for frame images (must exist; without trailing slash)
set outputDirectory=c:\frames
rem Path to ffmpeg binary.
set ffmpegBin=c:\bin\ffmpeg.exe
rem Path to exiftool binary.
set exiftoolBin=c:\bin\exiftool.exe
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=3
rem Get frame every X seconds.
set getFrameEvery=10
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)
set dateSource=FileModifyDate
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
set videoTimestamp=2017:01:01 00:00:00
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
set gpsDataFile=c:\gps\file.gpx
rem Config ends here.
set exifDateParam=-tagsFromFile "%videoFile%" "-datetimeoriginal<%dateSource%"
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%" -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%"
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%" "-geotime<${DateTimeOriginal}Z" -overwrite_original "%outputDirectory%\%framePrefix%*.jpg"
echo Done!
echo.
goto theend
:error
echo %errMsg%. Exitting.
:theend
pause
popd
endlocal
@bcheck555
Copy link

Thank you for this. Using a modified version to create photospheres for Streetview upload.

@Vladekk
Copy link

Vladekk commented Aug 18, 2021

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment