From 725b392279157d28b28f34307f58907a0fcf1c9e Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 16 Nov 2023 20:45:38 +0900 Subject: [PATCH] =?UTF-8?q?2023-11-16=E3=81=AE=E8=A8=98=E4=BA=8B=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/posts/2023-11-16/index.md | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 content/posts/2023-11-16/index.md diff --git a/content/posts/2023-11-16/index.md b/content/posts/2023-11-16/index.md new file mode 100644 index 0000000..c8ac3fb --- /dev/null +++ b/content/posts/2023-11-16/index.md @@ -0,0 +1,44 @@ +--- +author: usbharu +draft: true +categories: + - 技術 +date: 2023-11-16T20:36:52+09:00 +tags: + - Kotlin + - JavaCV + - FFmpeg +keywords: + - Kotlin + - JavaCV + - FFmpeg +title: JavaCVを利用して動画を特定のFPSにする +relpermalink: posts/2023-11-16/ +url: posts/2023-11-16/ +decription: JavaCVを利用して動画を特定のFPSで再エンコードし、保存します。 +--- + +取りあえず動くリポジトリ + +[https://git.usbharu.dev/usbharu/javacv-demo](https://git.usbharu.dev/usbharu/javacv-demo) + +## JavaCV で FPS を変更する + +GPU を利用したフレーム補間などではなく、単にフレームを複製、もしくは削減してフレーム数を調整します。 +このコードの肝は`while`がネストしている部分で、フレームが増えたり減ったりして音声と同期が取れなくなった分を多めに取得することで途中で無音になる問題を解決しています。 + +```kotlin + +while (true) { + val grab = grabber.grab() ?: break + println("GRAB ${grab.timestamp} : ${grab.image} ${grab.samples}") + if (grab.image != null || grab.samples != null) { + filter.push(grab) + } + while (true) { + val frame = filter.pull() ?: break + it.record(frame) + } + } + +```