Convert PTS into Frame Number
| 2551 | |
| 2552 | // Convert PTS into Frame Number |
| 2553 | int64_t FFmpegReader::ConvertVideoPTStoFrame(int64_t pts) { |
| 2554 | // Apply PTS offset |
| 2555 | int64_t previous_video_frame = current_video_frame; |
| 2556 | const double fps_value = (info.fps.num > 0 && info.fps.den > 0) ? info.fps.ToDouble() : 30.0; |
| 2557 | const double video_timebase_value = |
| 2558 | (info.video_timebase.num > 0 && info.video_timebase.den > 0) |
| 2559 | ? info.video_timebase.ToDouble() |
| 2560 | : (1.0 / 30.0); |
| 2561 | |
| 2562 | // Get the video packet start time (in seconds) |
| 2563 | double video_seconds = (double(pts) * video_timebase_value) + pts_offset_seconds; |
| 2564 | |
| 2565 | // Divide by the video timebase, to get the video frame number (frame # is decimal at this point) |
| 2566 | int64_t frame = round(video_seconds * fps_value) + 1; |
| 2567 | |
| 2568 | // Keep track of the expected video frame # |
| 2569 | if (current_video_frame == 0) |
| 2570 | current_video_frame = frame; |
| 2571 | else { |
| 2572 | |
| 2573 | // Sometimes frames are duplicated due to identical (or similar) timestamps |
| 2574 | if (frame == previous_video_frame) { |
| 2575 | // return -1 frame number |
| 2576 | frame = -1; |
| 2577 | } else { |
| 2578 | // Increment expected frame |
| 2579 | current_video_frame++; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | // Return frame # |
| 2584 | return frame; |
| 2585 | } |
| 2586 | |
| 2587 | // Convert Frame Number into Video PTS |
| 2588 | int64_t FFmpegReader::ConvertFrameToVideoPTS(int64_t frame_number) { |