Skip to content

Commit

Permalink
Try harder with small clips with sparse keyframes
Browse files Browse the repository at this point in the history
Fixes #41
  • Loading branch information
Marginal committed Jun 13, 2017
1 parent 4ff57c1 commit 218030a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions qlgenerator/GeneratePreviewForURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
NSInteger snapshot_time = [defaults integerForKey:kSettingsSnapshotTime];
if (snapshot_time <= 0)
snapshot_time = kDefaultSnapshotTime;
NSInteger time = duration < kMinimumDuration ? 0 : (duration < 2 * snapshot_time ? duration/2 : snapshot_time);
NSInteger time = duration < kMinimumDuration ? -1 : (duration < 2 * snapshot_time ? duration/2 : snapshot_time);
thePreview = [snapshotter newSnapshotWithSize:size atTime:time];
if (!thePreview && time)
if (!thePreview && time > 0)
thePreview = [snapshotter newSnapshotWithSize:size atTime:0]; // Failed. Try again at start.
}
} // Free snapshotter and ffmpeg resources before handing back to QuickLook
Expand Down
4 changes: 2 additions & 2 deletions qlgenerator/GenerateThumbnailForURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
snapshot_time = kDefaultSnapshotTime;

NSInteger duration = [snapshotter duration];
NSInteger time = duration < kMinimumDuration ? 0 : (duration < 2 * snapshot_time ? duration/2 : snapshot_time);
NSInteger time = duration < kMinimumDuration ? -1 : (duration < 2 * snapshot_time ? duration/2 : snapshot_time);
snapshot = [snapshotter newSnapshotWithSize:scaled atTime:time];
if (!snapshot && time)
if (!snapshot && time > 0)
snapshot = [snapshotter newSnapshotWithSize:size atTime:0]; // Failed. Try again at start.
} // Free snapshotter and ffmpeg resources before handing back to QuickLook

Expand Down
11 changes: 8 additions & 3 deletions qlgenerator/Snapshotter.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ - (int) newImageWithSize:(CGSize)size atTime:(NSInteger)seconds to:(uint8_t *con
int64_t stoptime;
if (seconds > 0)
{
avcodec_flush_buffers(dec_ctx); // Discard any buffered packets left over from previous call
timestamp += av_rescale(seconds, stream->time_base.den, stream->time_base.num);
stoptime = timestamp + av_rescale(kMaxKeyframeTime, stream->time_base.den, stream->time_base.num);
if (av_seek_frame(fmt_ctx, stream_idx, timestamp, AVSEEK_FLAG_BACKWARD) < 0) // AVSEEK_FLAG_BACKWARD is more reliable for MP4 container
return -1;
}
else if (seconds == 0 && !(fmt_ctx->iformat->flags & AVFMT_NO_BYTE_SEEK)) // rewind
{
avcodec_flush_buffers(dec_ctx); // Discard any buffered packets left over from previous call
av_seek_frame(fmt_ctx, stream_idx, 0, AVSEEK_FLAG_BYTE);
stoptime = av_rescale(kMaxKeyframeTime, stream->time_base.den, stream->time_base.num);
}
Expand All @@ -217,15 +219,18 @@ - (int) newImageWithSize:(CGSize)size atTime:(NSInteger)seconds to:(uint8_t *con
return -1;

int got_frame = 0;
avcodec_flush_buffers(dec_ctx); // Discard any buffered packets left over from previous call
while (av_read_frame(fmt_ctx, &pkt) >= 0 && !got_frame)
{
if (pkt.stream_index == stream_idx)
{
avcodec_decode_video2(dec_ctx, frame, &got_frame, &pkt);

// MPEG TS demuxer doesn't necessarily seek to keyframes. So keep looking for one.
if (got_frame && !frame->key_frame && frame->pkt_pts < stoptime)
if (got_frame && seconds > 0 &&
(
// It's a small clip and we've ended up at a keyframe at start of it. Keep reading until desired time.
(seconds <= kMaxKeyframeTime && frame->pts < timestamp) ||
// MPEG TS demuxer doesn't necessarily seek to keyframes. So keep looking for one.
((seconds > kMaxKeyframeTime && !frame->key_frame && frame->pts < stoptime))))
{
got_frame = 0;
av_frame_unref(frame);
Expand Down

0 comments on commit 218030a

Please sign in to comment.