Skip to content

Commit

Permalink
try jcodec
Browse files Browse the repository at this point in the history
  • Loading branch information
TTangNingzhi committed Dec 25, 2023
1 parent d830eb8 commit d773c5a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 45 deletions.
35 changes: 30 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ dependencies {
// https://mvnrepository.com/artifact/com.opencsv/opencsv
// https://mvnrepository.com/artifact/com.opencsv/opencsv
implementation("com.opencsv:opencsv:5.7.1")
implementation("org.bytedeco:javacv-platform:1.5.9")

}

// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij {
version.set("2022.2.5")
version.set("2023.1.4")

// type.set("IC")
// plugins.set(listOf("com.intellij.java"))
type.set("IC")
plugins.set(listOf("com.intellij.java"))

// type.set("PC")
// plugins.set(listOf("PythonCore"))
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/component/AlertDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public AlertDialog(String label,Icon icon) {
JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new BoxLayout(dialogPanel, BoxLayout.Y_AXIS));

//show a warning icon

JLabel label = new JLabel(this.label);
JLabel icon = new JLabel();
label.setIcon(this.icon);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/component/ConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ConfigDialog(Project project) throws IOException, InterruptedException {
for (String freq : freqList) {
freqCombo.addItem(Double.parseDouble(freq));
}
}else{ //use mouse and default freq
} else { //use mouse and default freq
deviceCombo.removeAllItems();
deviceCombo.addItem("Mouse");
freqCombo.removeAllItems();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/trackers/EyeTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void startTracking(Project project) throws IOException {
if (virtualFiles.length > 0) {
filePath = virtualFiles[0].getPath();
}
if (deviceIndex == 0 ) {
if (deviceIndex == 0) {
setting.setAttribute("eye_tracker", "Mouse");
} else {
setting.setAttribute("eye_tracker", "Tobii Pro Fusion");
Expand Down Expand Up @@ -237,7 +237,7 @@ public void processRawData(String message) {
Element aSTStructure = getASTStructureElement(psiElement);
gaze.appendChild(aSTStructure);
lastElement = psiElement;
System.out.println(gaze.getAttribute("timestamp") + " " + System.currentTimeMillis());
// System.out.println(gaze.getAttribute("timestamp") + " " + System.currentTimeMillis());
handleElement(gaze);
}
}));
Expand Down
68 changes: 36 additions & 32 deletions src/main/java/trackers/ScreenRecorder.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package trackers;

import com.opencsv.CSVWriter;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.jcodec.api.FrameGrab;
import org.jcodec.api.SequenceEncoder;
import org.jcodec.api.awt.AWTSequenceEncoder;
import org.jcodec.common.model.ColorSpace;
import org.jcodec.common.model.Picture;
import org.jcodec.scale.AWTUtil;

import javax.imageio.ImageIO;
import java.awt.*;
Expand All @@ -13,6 +16,8 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class ScreenRecorder {

Expand All @@ -22,18 +27,17 @@ public class ScreenRecorder {
* 2: started, paused; only resumeAction enabled
*/
int state = 0;
int frameRate = 4;
private AWTSequenceEncoder awtEncoder;
// private FFmpegFrameRecorder recorder;
private final ArrayList<String[]> timeList = new ArrayList<>();
private CSVWriter csvWriter;
boolean isRecording = false;
private int clipNumber = 1;
private int frameNumber = 0;
private String dataOutputPath = "";

private static ScreenRecorder instance = null;

private ScreenRecorder() {
}

public static ScreenRecorder getInstance() {
if (instance == null) {
instance = new ScreenRecorder();
Expand All @@ -44,12 +48,13 @@ public static ScreenRecorder getInstance() {

private void createEncoder() throws IOException {
awtEncoder = AWTSequenceEncoder.createSequenceEncoder(
new File(dataOutputPath + "/screen_recording/video_clip_" + clipNumber + ".mp4"), 24);
new File(dataOutputPath + "/screen_recording/video_clip_" + clipNumber + ".mp4"), frameRate);
}


public void startRecording() throws IOException {
state = 1;
clipNumber = 1;
isRecording = true;
File file = new File(dataOutputPath + "/screen_recording/frames.csv");
file.getParentFile().mkdirs();
Expand Down Expand Up @@ -86,37 +91,36 @@ public void resumeRecording() {
}

private void recordScreen() throws AWTException, IOException {
createEncoder();
frameNumber = 0;
Rectangle bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
createEncoder();
long frameRate = 24;
Thread recordThread = new Thread(() -> {
long frameNumber = 0;
long lastFrameTime = System.nanoTime();
long frameDuration = 1000000000 / frameRate;
while (isRecording) {
long currentTime = System.nanoTime();
if (currentTime - lastFrameTime < frameDuration) {
continue;
}
lastFrameTime = currentTime;

BufferedImage screenCapture = robot.createScreenCapture(bounds);
frameNumber++;
timeList.add(new String[]{String.valueOf(System.currentTimeMillis()), String.valueOf(frameNumber), String.valueOf(clipNumber)});
try {
awtEncoder.encodeImage(screenCapture);
} catch (IOException e) {
throw new RuntimeException(e);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!isRecording) {
try {
awtEncoder.finish();
} catch (IOException e) {
throw new RuntimeException(e);
}
timer.cancel();
} else {
BufferedImage screenCapture = robot.createScreenCapture(bounds);
frameNumber++;
timeList.add(new String[]{String.valueOf(System.currentTimeMillis()), String.valueOf(frameNumber), String.valueOf(clipNumber)});
try {
if (frameNumber == 1) {
ImageIO.write(screenCapture, "png", new File(dataOutputPath + "/screen_recording/frame_" + frameNumber + ".png"));
}
awtEncoder.encodeImage(screenCapture);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
try {
awtEncoder.finish();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
recordThread.start();
}, 0, 1000 / frameRate);
}

public void setDataOutputPath(String dataOutputPath) {
Expand Down

0 comments on commit d773c5a

Please sign in to comment.