1
1
package logic
2
2
3
+ import androidx.compose.runtime.MutableState
4
+ import models.AppState
3
5
import org.bytedeco.javacv.FFmpegFrameRecorder
4
6
import wrappers.IterableFrameGrabber
5
7
import wrappers.Resettable2DFrameConverter
6
8
import java.awt.image.BufferedImage
7
9
import java.io.File
8
10
9
- fun createThumbnailVideo (
11
+ /* *
12
+ * Creates a scaled video from the input video as a temp file.
13
+ *
14
+ * @param inputPath [String] containing the path to the input video.
15
+ * @param scale [Float] containing the scaling factor, must be greater than 0f.
16
+ * @return [String] containing the path to the scaled video.
17
+ */
18
+ fun createScaledVideo (
10
19
inputPath : String ,
11
- downScaling : Float = 0.5f,
20
+ scale : Float = 0.5f,
12
21
): String {
13
- assert (downScaling > 0.0f && downScaling <= 1.0f ) { " Downscaling factor must be between 0 and 1 " }
22
+ assert (scale > 0.0f ) { " Scaling factor must be positive! " }
14
23
val outputPath = kotlin.io.path.createTempFile(prefix = " gui_thumbnail_video" , suffix = " .mkv" ).toString()
15
24
16
25
val grabber = IterableFrameGrabber (File (inputPath))
17
26
18
27
val inWidth = grabber.imageWidth
19
28
val inHeight = grabber.imageHeight
20
29
21
- val outWidth = (inWidth * downScaling ).toInt()
22
- val outHeight = (inHeight * downScaling ).toInt()
30
+ val outWidth = (inWidth * scale ).toInt()
31
+ val outHeight = (inHeight * scale ).toInt()
23
32
24
33
val recorder = FFmpegFrameRecorder (outputPath, outWidth, outHeight)
25
-
26
34
val converter = Resettable2DFrameConverter ()
27
35
36
+ // copy some metadata over
28
37
recorder.frameRate = grabber.frameRate
29
38
recorder.videoCodec = grabber.videoCodec
30
39
31
40
recorder.start()
32
41
33
42
for (image in grabber) {
34
- // do something with the frame
43
+ // scale down the image and record it
35
44
val scaledImage = BufferedImage (outWidth, outHeight, BufferedImage .TYPE_3BYTE_BGR )
36
45
scaledImage.createGraphics().drawImage(image.getScaledInstance(outWidth, outHeight, 0 ), 0 , 0 , null )
37
46
recorder.record(converter.convert(scaledImage))
@@ -42,3 +51,16 @@ fun createThumbnailVideo(
42
51
43
52
return outputPath
44
53
}
54
+
55
+ /* *
56
+ * Creates the thumbnail videos for the reference and current videos and updates their paths in the global state.
57
+ *
58
+ * @param state [MutableState]<[AppState]> containing the global state.
59
+ */
60
+ fun createThumbnailVideos (state : MutableState <AppState >) {
61
+ // create the thumbnail videos
62
+ val tempReference = createScaledVideo(state.value.videoReferencePath!! , 0.25f )
63
+ val tempCurrent = createScaledVideo(state.value.videoCurrentPath!! , 0.25f )
64
+
65
+ state.value = state.value.copy(thumbnailVideoPathReference = tempReference, thumbnailVideoPathCurrent = tempCurrent)
66
+ }
0 commit comments