【问题标题】:ffmpeg extract frame and get timestamp based on creation time for each extracted frame in exif or txtffmpeg 提取帧并根据 exif 或 txt 中每个提取帧的创建时间获取时间戳
【发布时间】:2020-05-08 23:25:09
【问题描述】:

我尝试使用 ffmpeg 从水下调查视频中提取帧。提取的帧应在该过程稍后基于时间戳与 GPS 位置合并。我用来提取帧的代码是:

ffmpeg -i GOPR0173.MP4 -qscale:v 2 -r 1 frames/%4d.jpg.

我需要一个解决方案来根据调查日期时间获取帧的实际时间戳,以便帧的时间戳等于创建视频时的实际时间。理想情况下,时间戳保存在图像 exif 中,或者如果这不可能在单独的文本文件中保存,则时间戳用作文件的名称,例如 2020_01_01_HH_MM_SS.jpg。

我的想法是使用视频的 creation_time 将帧的时间戳添加到此并将结果存储在提取帧的 exif 中,或者如果这不可能在 txt 文件中。

我是 ffmpeg 的新手,真的迷路了,如果有任何帮助来解决这个任务,我将不胜感激! 如果有人能给我提供必要的代码,那就太棒了!

C:\Unterwasserfotos>ffmpeg -i GOPR0173.MP4 -vstats -qscale:v 2 -r 0.1 frames/%4d.jpg
ffmpeg version git-2020-01-21-fc6fde2 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.2.1 (GCC) 20200122
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls
 --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enabl
e-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-
libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwol
ame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enab
le-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable
-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable
-libaom --enable-libmfx --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enabl
e-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
  libavutil      56. 38.100 / 56. 38.100
  libavcodec     58. 66.100 / 58. 66.100
  libavformat    58. 35.104 / 58. 35.104
  libavdevice    58.  9.103 / 58.  9.103
  libavfilter     7. 71.100 /  7. 71.100
  libswscale      5.  6.100 /  5.  6.100
  libswresample   3.  6.100 /  3.  6.100
  libpostproc    55.  6.100 / 55.  6.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'GOPR0173.MP4':
  Metadata:
    major_brand     : mp41
    minor_version   : 538120216
    compatible_brands: mp41
    creation_time   : 2020-01-21T12:33:10.000000Z
    firmware        : HD4.02.05.00.00
  Duration: 00:02:21.85, start: 0.000000, bitrate: 30152 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt709), 1920x1080 [SAR
 1:1 DAR 16:9], 30000 kb/s, 50 fps, 50 tbr, 90k tbn, 100 tbc (default)
    Metadata:
      creation_time   : 2020-01-21T12:33:10.000000Z
      handler_name    :         GoPro AVC
      encoder         : GoPro AVC encoder
      timecode        : 12:33:11:16
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (defa
ult)
    Metadata:
      creation_time   : 2020-01-21T12:33:10.000000Z
      handler_name    :         GoPro AAC
      timecode        : 12:33:11:16
    Stream #0:2(eng): Data: none (tmcd / 0x64636D74) (default)
    Metadata:
      creation_time   : 2020-01-21T12:33:10.000000Z
      handler_name    :         GoPro TCD
      timecode        : 12:33:11:16
    Stream #0:3(eng): Data: none (fdsc / 0x63736466), 12 kb/s (default)
    Metadata:
      creation_time   : 2020-01-21T12:33:10.000000Z
      handler_name    :         GoPro SOS
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
Output #0, image2, to 'frames/%4d.jpg':
  Metadata:
    major_brand     : mp41
    minor_version   : 538120216
    compatible_brands: mp41
    firmware        : HD4.02.05.00.00
    encoder         : Lavf58.35.104
    Stream #0:0(eng): Video: mjpeg, yuvj420p(pc), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s,
 0.10 fps, 0.10 tbn, 0.10 tbc (default)
    Metadata:
      creation_time   : 2020-01-21T12:33:10.000000Z
      handler_name    :         GoPro AVC
      timecode        : 12:33:11:16
      encoder         : Lavc58.66.100 mjpeg
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
frame=   16 fps=0.3 q=2.0 Lsize=N/A time=00:02:40.00 bitrate=N/A dup=0 drop=7076 speed=3.26x
video:1938kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown´´´







【问题讨论】:

    标签: ffmpeg timestamp frame exif extraction


    【解决方案1】:

    我也修改了上面的代码。它现在接受命令行参数,允许通过使用电子表格将命令行拼凑起来自动处理多个 mp4 文件。

    针对未压缩的 TIFF(大文件)进行了修改

    语法: batchfilename.bat path(with terminating backslash) filename(without extension, currently assumed to be MP4) number_of_seconds_between_frames(numeric) date(xxxx:xx:xx year:month:day) time(xx:xx:xx.x)

    xxxx.bat code:
        @echo off
        rem Ksheesh Geotag Video Frames 0.0.1  
        rem MODIFIED FOR UNCOMPRESSED TIFF USING COMMAND LINE PARAMETERS
        rem SYNTAX: batchfilename.bat path(with terminating backslash) filename(without extension, currently
        rem assummed to be MP4) number_of_seconds_between_frames(numeric) date(xxxx:xx:xx year:month
        rem :day) time(xx:xx:xx.x)
        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=%1%2.mp4
        rem Prefix which will be added to image file names
        set framePrefix=%2GEO_
        echo %framePrefix%
        rem Output directory for frame images (must exist; without trailing slash)
        set outputDirectory=%1GPStiff
        md "%outputDirectory%"
        
        rem Path to ffmpeg binary.
        set ffmpegBin=q:\drone\djimp3tiff\ffmpeg.exe
        
        rem Path to exiftool binary.
        set exiftoolBin=q:\drone\djimp3tiff\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=4
        
        rem Get frame every X seconds.
        set getFrameEvery=%3
        
        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=0
        
        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=2021:10:21 20:30:26.7
        set videoTimestamp=%4 %5
        
        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=%1%2.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 -vsync 2 -format rgb -pix_fmt rgba -compression_algo deflate %framePrefix%%%0%paddingDigits%d.tif
        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%*.tif"
        echo Done!
        echo.
        
        echo Setting timestamp offsets.
        for %%f in ("%outputDirectory%\%framePrefix%*.tif") 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 on
        echo Geotagging images.
        %exiftoolBin% -P -geotag "%gpsDataFile%" "-geotime<${DateTimeOriginal}Z" -overwrite_original "%outputDirectory%\%framePrefix%*.tif"
        echo Done!
        echo.
        
        goto theend
        
        :error
        echo %errMsg%. Exiting.
        
        :theend
        pause
        popd
        endlocal
        
    ---------------------------------------
    
    I often have multiple video files which are all contained in one gpx file.  I'm using the free program GPX editor to find the correct start time for each video.
    

    【讨论】:

      【解决方案2】:

      这是我的任务的解决方案。核心组件是一个很好的批处理脚本(见下文),我发现并采用了我的需求。

      1. 将程序 exiftoolffmpegNamexifGeoSetter、批处理脚本和视频文件放入一个文件夹。
      2. 使用 Mediainfo 从 gopro 视频文件中读取时间戳。
      3. 在批处理脚本中插入视频文件名和时间戳(第 34 和 63 行)
      4. 运行批处理脚本以每秒提取一帧(时间戳放在 exif 中)
      5. 使用 GeoSetter 将帧与 .gpx 文件合并(坐标放置在 exif 中)
      6. 使用 Namexif 将帧的文件名替换为基于其 exif 的日期和时间

      批处理脚本:

      @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=GOPR0173.MP4
      
      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=%cd%
      
      rem Path to ffmpeg binary.
      set ffmpegBin=ffmpeg.exe
      
      rem Path to exiftool binary.
      set exiftoolBin=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=4
      
      rem Get frame every X seconds.
      set getFrameEvery=1
      
      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=0
      
      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=2020:01:21 13:28:33
      
      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=2020-01-21_gps.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%" -qscale: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!" "%framePrefix%*.jpg"
      echo Done!
      echo.
      
      echo Setting timestamp offsets.
      for %%f in ("%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.
      goto theend
      
      :error
      echo %errMsg%. Exitting.
      
      :theend
      pause
      popd
      endlocal
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-24
        • 2019-12-06
        • 2019-04-08
        • 1970-01-01
        • 2022-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多