Opened 10 years ago

Last modified 7 years ago

#4554 reopened defect

wrong seek with -ss

Reported by: Josephine Dussourd Owned by:
Priority: normal Component: undetermined
Version: git-master Keywords: mov h264 seek
Cc: nicolas@noirbent.fr Blocked By:
Blocking: Reproduced by developer: yes
Analyzed by developer: no

Description

I’m trying to extract a particular frame from my video but I don’t get the frame I expect:
The video encoded in h.264, and have 25 fps. The timecode is burn in my video

00:00:00.88 correspond to 14:53:07:12 brunt in my video, but when I seek the next frame I don’t have the actual next frame :

ffmpeg -ss 00:00:00.88 -i myVideo.mp4 -frames:v 1 sec088.jpg 
	gives me the frame with the timecode 14:53:07:12
ffmpeg -ss 00:00:00.92 -i myVideo.mp4 -frames:v 1 sec092.jpg
	gives me the frame with the timecode 14:53:07:15
ffmpeg -ss 00:00:00.96 -i myVideo.mp4 -frames:v 1 sec096.jpg
	gives me the frame with the timecode 14:53:07:15
ffmpeg -ss 00:00:01 -i myVideo.mp4 -frames:v 1 sec100.jpg
	gives me the frame with the timecode 14:53:07:15
ffmpeg -ss 00:00:01.04 -i myVideo.mp4 -frames:v 1 sec104.jpg
	gives me the frame with the timecode 14:53:07:16

I decided to extract the images from the video with this command line: ffmpeg -i myVideo.mp4 c:/images/image%06d.jpg
When I checked the images, the frames with the time code 14:53:07:13 and 14:53:07:14 exist.

ffmpeg -v 9 -loglevel 99 -i myVideo.mp4

ffmpeg version N-71209-gd759844 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-l
ibilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enab
le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --en
able-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --ena
ble-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc
 --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enabl
e-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-lzma --ena
ble-decklink --enable-zlib
  libavutil      54. 22.100 / 54. 22.100
  libavcodec     56. 32.100 / 56. 32.100
  libavformat    56. 27.100 / 56. 27.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 13.101 /  5. 13.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Splitting the commandline.
Reading option '-v' ... matched as option 'v' (set logging level) with argument
'9'.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level)
with argument '99'.
Reading option '-i' ... matched as input file with argument 'myVideo.mp4'.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option v (set logging level) with argument 9.
Successfully parsed a group of options.
Parsing a group of options: input file myVideo.mp4.
Successfully parsed a group of options.
Opening an input file: myVideo.mp4.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000000316440] Format mov,mp4,m4a,3gp,3g2,mj2 prob
ed with size=2048 and score=100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000000316440] ISO: File Type Major Brand: isom
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000000316440] Before avformat_find_stream_info()
pos: 7557884 bytes read:50882 seeks:1
[h264 @ 0000000000327f20] no picture
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000000316440] All info found
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000000316440] After avformat_find_stream_info() p
os: 20381 bytes read:83650 seeks:2 frames:1
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'myVideo.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.27.100
  Duration: 00:01:00.08, start: 0.000000, bitrate: 1006 kb/s
    Stream #0:0(eng), 1, 1/25000: Video: h264 (Main), 4 reference frames (avc1 /
 0x31637661), yuv420p(tv, left), 640x360 (640x368) [SAR 1:1 DAR 16:9], 1/50, 100
3 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Successfully opened the file.
At least one output file must be specified
[AVIOContext @ 0000000000316cc0] Statistics: 83650 bytes read, 2 seeks

Attachments (1)

frames.txt (676.7 KB ) - added by Josephine Dussourd 10 years ago.
description of the frames of my video by "ffprobe -show_frames"

Download all attachments as: .zip

Change History (9)

by Josephine Dussourd, 10 years ago

Attachment: frames.txt added

description of the frames of my video by "ffprobe -show_frames"

comment:1 by Carl Eugen Hoyos, 10 years ago

Keywords: seek added
Version: unspecifiedgit-master

Please provide the input file.

comment:2 by Josephine Dussourd, 10 years ago

comment:3 by Carl Eugen Hoyos, 10 years ago

Keywords: mov h264 added
Status: newopen

comment:4 by cbsrobot, 10 years ago

Resolution: invalid
Status: openclosed

If you need accurate seeking, please use -ss as an output option. See https://ffmpeg.org/ffmpeg.html#Main-options

This command gives me the correct images:

for i in 0.88 0.92 0.96 1.00 1.04 1.08; do ffmpeg -i myVideo-bugReport-4554.mp4 -ss 00:00:0${i} -frames:v 1 sec${i}.jpg; done;

sec0.88.jpg -> 14:53:07:12
sec0.92.jpg -> 14:53:07:13
sec0.96.jpg -> 14:53:07:14
sec1.00.jpg -> 14:53:07:15
sec1.04.jpg -> 14:53:07:16
sec1.08.jpg -> 14:53:07:17

comment:5 by Michael Niedermayer, 10 years ago

Reproduced by developer: set
Resolution: invalidfixed

Fixed accurate seeking with -ss as input option for this file in b0322e4a9e326f41e0214abb0083fe9eb7407460

comment:6 by Josephine Dussourd, 10 years ago

Thank you for the solving my problem. Actually I had exactly the same "freeze" problem with my HTML5 player. Do you have any idea of what can cause that ?

comment:7 by Nicolas Noirbent, 9 years ago

Cc: nicolas@noirbent.fr added
Resolution: fixed
Status: closedreopened

Unfortunately the heuristic fix actually breaks seeking for other files, i.e. it seeks too far back.

For example with http://kimsufi.noirbee.net/pub/video.mp4 , when seeking and using stream copy with the following command:

ffmpeg -y -ss 2 -i video.mp4 -c copy video_seek_heuristic.mp4

Despite the fact that there is a keyframe 2 seconds / 50 frames into the video, ffmpeg will seek back to the very first one.

Reverting the abovementioned commit, i.e. not using the heuristic introduced by Michael, correctly seeks to the keyframe at 2 seconds.

Original video: http://kimsufi.noirbee.net/pub/video.mp4
ffprobe -select_streams v -show_packets video.mp4 : http://kimsufi.noirbee.net/pub/video_ffprobe.txt
Output video when using the heuristic (git HEAD): http://kimsufi.noirbee.net/pub/video_seek_heuristic.mp4
Output video without the heuristic: http://kimsufi.noirbee.net/pub/video_seek.mp4

Not sure how this could be fixed both for the OP's and my files, considering seeking uses DTS anyway in MOV/MP4 (well actually for everything else than nut if I trust git grep AVFMT_SEEK_TO_PTS).

comment:8 by oxd, 7 years ago

Also seeing same issue with seeking as noirbee when using -ss as an input option and doing stream copy.

In my case when I try to trim a video from a specific keyframe (using either same timestamp as keyframe or just afterwards) ffmpeg will seek to the previous keyframe. Michael's heuristic subtracts some amount from the timestamp that I entered resulting in the seek timestamp being earlier than my target keyframe.

Example:
Target keyframe has DTS time of 50.005 and any of the following commands are issued:

ffmpeg -y -noaccurate_seek -ss 50.005 -c copy -i test.mp4 test_out.mp4
ffmpeg -y -noaccurate_seek -ss 50.055 -c copy -i test.mp4 test_out.mp4
ffmpeg -y -noaccurate_seek -ss 50.105 -c copy -i test.mp4 test_out.mp4

The resulting output will start from previous keyframe before 50.005, e.g. 40.005

Last edited 7 years ago by oxd (previous) (diff)
Note: See TracTickets for help on using tickets.