feat(ffmpeg): select frame by duration and float position

This commit is contained in:
Adrian Shum 2022-10-22 02:46:19 +08:00
parent 7c18dcda7a
commit 3b02ab7458
15 changed files with 14 additions and 9 deletions

View file

@ -243,9 +243,7 @@ func incrementDuration(av *AVContext, frame *C.AVFrame, i C.int) {
ptsToNano := C.int64_t(1000000000 * av.stream.time_base.num / av.stream.time_base.den) ptsToNano := C.int64_t(1000000000 * av.stream.time_base.num / av.stream.time_base.den)
newDuration := time.Duration(frame.pts * ptsToNano) newDuration := time.Duration(frame.pts * ptsToNano)
av.availableDuration = newDuration av.availableDuration = newDuration
if av.selectedDuration > 0 && i > 0 && newDuration <= av.selectedDuration {
av.selectedIndex = i
}
if !av.durationInFormat && newDuration > av.duration { if !av.durationInFormat && newDuration > av.duration {
av.duration = newDuration av.duration = newDuration
} }
@ -325,6 +323,13 @@ func populateThumbContext(av *AVContext, frames chan *C.AVFrame, n C.int, done <
incrementDuration(av, frame, i) incrementDuration(av, frame, i)
frames <- frame frames <- frame
frame = nil frame = nil
if av.selectedDuration > 0 {
if av.availableDuration <= av.selectedDuration {
av.selectedIndex = i
} else {
break
}
}
} }
if av.selectedIndex > av.availableIndex { if av.selectedIndex > av.availableIndex {
av.selectedIndex = av.availableIndex av.selectedIndex = av.availableIndex

View file

@ -72,7 +72,7 @@ func TestAVContext(t *testing.T) {
if n == 10 { if n == 10 {
require.NoError(t, av.ProcessFrames(n)) require.NoError(t, av.ProcessFrames(n))
} else if n == 99999 { } else if n == 99999 {
require.NoError(t, av.SelectDuration(time.Millisecond*500)) require.NoError(t, av.SelectDuration(time.Second))
} else if n == 9999 { } else if n == 9999 {
require.NoError(t, av.SelectPosition(0.7)) require.NoError(t, av.SelectPosition(0.7))
} else if n > -1 { } else if n > -1 {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 54 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

View file

@ -1 +1 @@
{"orientation":1,"duration":12040,"width":720,"height":576,"fps":10.9,"has_video":true,"has_audio":false} {"orientation":1,"duration":12040,"width":720,"height":576,"fps":9.8,"has_video":true,"has_audio":false}

View file

@ -1 +1 @@
{"orientation":1,"duration":7407,"width":640,"height":480,"fps":9.3,"has_video":true,"has_audio":true} {"orientation":1,"duration":7407,"width":640,"height":480,"fps":1.2,"has_video":true,"has_audio":true}

View file

@ -1 +1 @@
{"orientation":1,"duration":7407,"width":640,"height":480,"fps":26.3,"has_video":true,"has_audio":true} {"orientation":1,"duration":7407,"width":640,"height":480,"fps":16,"has_video":true,"has_audio":true}

View file

@ -1 +1 @@
{"orientation":1,"duration":2560,"width":480,"height":360,"fps":29.7,"has_video":true,"has_audio":true} {"orientation":1,"duration":2560,"width":480,"height":360,"fps":29.6,"has_video":true,"has_audio":true}

View file

@ -1 +1 @@
{"orientation":1,"duration":2560,"width":480,"height":360,"fps":29.7,"has_video":true,"has_audio":true} {"orientation":1,"duration":2560,"width":480,"height":360,"fps":29.3,"has_video":true,"has_audio":true}