ffmpeg 提取帧并根据 exif 或 txt 中每个提取帧的创建时间获取时间戳

标签 ffmpeg timestamp frame exif extraction

我尝试使用 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´´´







最佳答案

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

  • 将程序 exiftool、ffmpeg、Namexif、GeoSetter、批处理脚本和视频文件放在一个文件夹中。
  • 使用 Mediainfo 从 gopro 视频文件中读取时间戳。
  • 在批处理脚本中插入视频文件名和时间戳(第 34 和 63 行)
  • 运行批处理脚本每秒提取一帧(时间戳放在 exif 中)
  • 使用 GeoSetter 将帧与 .gpx 文件合并(坐标放置在 exif 中)
  • 使用 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
    

    关于ffmpeg 提取帧并根据 exif 或 txt 中每个提取帧的创建时间获取时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59865863/

    相关文章:

    c++ - 使用 libavcodec 播放媒体时出现段错误

    c++ - 使用 Ffmpeg 传输 SEI

    python - 如何用Python编写可变帧率视频

    PHP:获取月份的星期几

    MYSQL查询上次列根据时间戳有不同的值

    android - 用于简短交互的帧动画或电影文件 "movie"Android 应用程序

    html - 如果你只知道它的宽度,如何为 img 添加一个花哨的框架?

    指向数组指针的 C++ 指针

    java - Java中的JDBC时间戳转义格式和SimpleDateFormat

    java - build 迷宫