Created
November 22, 2017 19:28
Extract frames from video every X seconds and geotag them using GPS data file. Requires exiftool and ffmpeg.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks