Skip to content

Commit

Permalink
ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
TTangNingzhi committed Dec 25, 2023
1 parent d773c5a commit b1933d0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 49 deletions.
63 changes: 44 additions & 19 deletions .idea/workspace.xml

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

8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ repositories {
}

dependencies {
implementation("org.jcodec:jcodec:0.2.5")
implementation("org.jcodec:jcodec-javase:0.2.5")
// 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")

// implementation("org.bytedeco:javacv-platform:1.5.9")
implementation("org.bytedeco:javacv:1.5.9")
implementation("org.bytedeco:ffmpeg:6.0-1.5.9")
implementation("org.bytedeco:ffmpeg-platform:6.0-1.5.9")
}

// Configure Gradle IntelliJ Plugin
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/component/ConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public ConfigDialog(Project project) throws IOException, InterruptedException {
}
List<String> freqList = AvailabilityChecker.getFrequencies(getPythonInterpreter());
freqCombo.removeAllItems();
System.out.println(freqList);
for (String freq : freqList) {
freqCombo.addItem(Double.parseDouble(freq));
}
Expand Down Expand Up @@ -353,7 +352,6 @@ protected void textChanged(@NotNull DocumentEvent e) {
deviceCombo.setSelectedIndex(0);
List<String> freqList = AvailabilityChecker.getFrequencies(getPythonInterpreter());
freqCombo.removeAllItems();
System.out.println(freqList);
for (String freq : freqList) {
freqCombo.addItem(Double.parseDouble(freq));
}
Expand Down
65 changes: 41 additions & 24 deletions src/main/java/trackers/ScreenRecorder.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
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 org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.*;
import org.bytedeco.javacv.Frame;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -27,9 +21,9 @@ public class ScreenRecorder {
* 2: started, paused; only resumeAction enabled
*/
int state = 0;
int frameRate = 4;
private AWTSequenceEncoder awtEncoder;
// private FFmpegFrameRecorder recorder;
int frameRate = 12;
private FrameRecorder recorder;
private FrameGrabber grabber;
private final ArrayList<String[]> timeList = new ArrayList<>();
private CSVWriter csvWriter;
boolean isRecording = false;
Expand All @@ -47,8 +41,32 @@ public static ScreenRecorder getInstance() {


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

// avfoundation for macOS, gdigrab for Windows, x11grab for Linux
if (utils.OSDetector.isMac()) {
grabber.setFormat("avfoundation");
} else if (utils.OSDetector.isWindows()) {
grabber.setFormat("gdigrab");
} else if (utils.OSDetector.isUnix()) {
grabber.setFormat("x11grab");
} else {
throw new IOException("Unsupported OS");
}

GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
grabber.setImageWidth((int) (Toolkit.getDefaultToolkit().getScreenSize().width * config.getDefaultTransform().getScaleX()));
grabber.setImageHeight((int) (Toolkit.getDefaultToolkit().getScreenSize().height * config.getDefaultTransform().getScaleY()));
grabber.setOption("offset_x", "0");
grabber.setOption("offset_y", "0");
grabber.start();

recorder = FrameRecorder.createDefault(dataOutputPath + "/screen_recording/clip_" + clipNumber + ".mp4", grabber.getImageWidth(), grabber.getImageHeight());
recorder.setFrameRate(frameRate);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.start();
}


Expand Down Expand Up @@ -93,31 +111,30 @@ public void resumeRecording() {
private void recordScreen() throws AWTException, IOException {
createEncoder();
frameNumber = 0;
Rectangle bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!isRecording) {
try {
awtEncoder.finish();
grabber.stop();
recorder.stop();
grabber.release();
recorder.release();
} 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) {
Frame frame = grabber.grabFrame();
recorder.record(frame);
} catch (FrameGrabber.Exception | FrameRecorder.Exception e) {
throw new RuntimeException(e);
}

}
}
}, 0, 1000 / frameRate);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/utils/OSDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils;

public class OSDetector {
private static String OS = System.getProperty("os.name").toLowerCase();

public static boolean isWindows() {
return (OS.contains("win"));
}

public static boolean isMac() {
return (OS.contains("mac"));
}

public static boolean isUnix() {
return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix"));
}
}

0 comments on commit b1933d0

Please sign in to comment.