-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathEx1ReadVideoSequence.scala
52 lines (41 loc) · 1.42 KB
/
Ex1ReadVideoSequence.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Copyright (c) 2011-2019 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/
package opencv_cookbook.chapter11
import org.bytedeco.javacv.{CanvasFrame, FFmpegFrameGrabber}
import java.io.File
import javax.swing.WindowConstants
import scala.collection.Iterator.continually
/** The example for section "Reading video sequences" in Chapter 10, page 248.
*
* This version of the example is implemented using JavaCV `FFmpegFrameGrabber`class.
*
*/
object Ex1ReadVideoSequence extends App {
// Use command line path, if provided
val inputFile = args
.headOption
.map(new File(_))
.getOrElse(new File("data/bike.avi"))
val grabber = new FFmpegFrameGrabber(inputFile)
// Open video video file
grabber.start()
// Prepare window to display frames
val canvasFrame = new CanvasFrame("Extracted Frame", 1)
canvasFrame.setCanvasSize(grabber.getImageWidth, grabber.getImageHeight)
// Exit the example when the canvas frame is closed
canvasFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
val delay = math.round(1000d / grabber.getFrameRate)
// Read frame by frame, stop early if the display window is closed
for (frame <- continually(grabber.grab()).takeWhile(_ != null)
if canvasFrame.isVisible) {
// Capture and show the frame
canvasFrame.showImage(frame)
// Delay
Thread.sleep(delay)
}
// Close the video file
grabber.release()
}