Skip to content

Commit

Permalink
Cherry-Pick changes in pull request #7 "Refactor proposal".
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Aug 9, 2024
1 parent 328c53a commit 1d573ac
Show file tree
Hide file tree
Showing 64 changed files with 1,282 additions and 1,355 deletions.
79 changes: 44 additions & 35 deletions doc/handbook/MonteMedia-Handbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,30 @@ To read video frames from a movie file, you have to perform the following steps:
movie file.

```java
BufferedImage[]readMovie(File file)throws IOException{
ArrayList<BufferedImage> frames=new ArrayList<BufferedImage> ();
MovieReader in=Registry.getInstance().getReader(file);
Format format=new Format(DataClassKey,BufferedImage.class);
int track=in.findTrack(0,new Format(MediaTypeKey,MediaType.VIDEO));
Codec codec=Registry.getInstance().getCodec(in.getFormat(track),format);
try{
Buffer inbuf=new Buffer();Buffer codecbuf=new Buffer();
do{
in.read(track,inbuf);
codec.process(inbuf,codecbuf);
if(!codecbuf.isFlag(BufferFlag.DISCARD)){
frames.add(Images.cloneImage((BufferedImage)codecbuf.data));
class HowToReadVideoFramesFromAMovieFile {

BufferedImage[] readMovie(File file) throws IOException {
ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
MovieReader in = Registry.getInstance().getReader(file);
Format format = new Format(DataClassKey, BufferedImage.class);
int track = in.findTrack(0, new Format(MediaTypeKey, MediaType.VIDEO));
Codec codec = Registry.getInstance().getCodec(in.getFormat(track), format);
try {
Buffer inbuf = new Buffer();
Buffer codecbuf = new Buffer();
do {
in.read(track, inbuf);
codec.process(inbuf, codecbuf);
if (!codecbuf.isFlag(BufferFlag.DISCARD)) {
frames.add(Images.cloneImage((BufferedImage) codecbuf.data));
}
} while (!inbuf.isFlag(BufferFlag.END_OF_MEDIA));
} finally {
in.close();
}
}while(!inbuf.isFlag(BufferFlag.END_OF_MEDIA));
}finally{
in.close();
}
return frames.toArray(new BufferedImage[frames.size()]);}
return frames.toArray(new BufferedImage[frames.size()]);
}
}
```

### Writing video frames into a movie file
Expand All @@ -96,23 +101,27 @@ To write video frames from into movie file, you have to perform the following st
The example code below shows how to write an array of BufferedImages into a movie file.

```java
void writeMovie(File file,BufferedImage[]frames)throws IOException{
MovieWriter out=Registry.getInstance().getWriter(file);
Format format=new Format(MediaTypeKey,MediaType.VIDEO, // EncodingKey, ENCODING_AVI_MJPG,
FrameRateKey,new Rational(30,1),//
WidthKey,frames[0].getWidth(), //
HeightKey,frames[0].getHeight(),// DepthKey, 24
class HowToWriteVideoFramesIntoAMovieFile {

void writeMovie(File file, BufferedImage[] frames) throws IOException {
MovieWriter out = Registry.getInstance().getWriter(file);
Format format = new Format(MediaTypeKey, MediaType.VIDEO, // EncodingKey, ENCODING_AVI_MJPG,
FrameRateKey, new Rational(30, 1),//
WidthKey, frames[0].getWidth(), //
HeightKey, frames[0].getHeight()
);
int track=out.addTrack(format);
try{
Buffer buf=new Buffer();
buf.format=new Format(DataClassKey,BufferedImage.class);buf.sampleDuration=format.get(FrameRateKey).inverse();
for(int i=0;i<frames.length;i++){
buf.data=frames[i];
out.write(track,buf);
}
}finally{
out.close();
}
int track = out.addTrack(format);
try {
Buffer buf = new Buffer();
buf.format = new Format(DataClassKey, BufferedImage.class);
buf.sampleDuration = format.get(FrameRateKey).inverse();
for (int i = 0; i < frames.length; i++) {
buf.data = frames[i];
out.write(track, buf);
}
} finally {
out.close();
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ FrameRateKey, new Rational(30, 1),//
out.addTrack(format);

// Draw the animation
for (int i = 0, n = 60; i < n; i++) {
for (int i = 0, n = 61; i < n; i++) {
double t = (double) i / n;
drawAnimationFrame(img, g, t);

// write it to the writer
// write image to the writer
out.write(0, img, 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ FrameRateKey, new Rational(30, 1),//
out.setVideoColorTable(0, img.getColorModel());

// Draw the animation
for (int i = 0, n = 200; i < n; i++) {
for (int i = 0, n = 61; i < n; i++) {
double t = (double) i / n - 1;
drawAnimationFrame(img, g, t);

// write it to the writer
// write image to the writer
out.write(0, img, 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.monte.media.av.FormatKeys.MediaType;
import org.monte.media.math.Rational;
import org.monte.media.screenrecorder.JRecordingAreaFrame;
import org.monte.media.screenrecorder.MouseConfigs;
import org.monte.media.screenrecorder.ScreenRecorder;
import org.monte.media.screenrecorder.State;
import org.monte.media.swing.BackgroundTask;
import org.monte.media.swing.JLabelHyperlinkHandler;
import org.monte.media.swing.datatransfer.DropFileTransferHandler;
Expand Down Expand Up @@ -77,7 +79,7 @@ private class Handler implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
ScreenRecorder r = screenRecorder;
if (r != null && r.getState() == ScreenRecorder.State.FAILED) {
if (r != null && r.getState() == State.FAILED) {
recordingFailed();
}
}
Expand Down Expand Up @@ -713,10 +715,10 @@ private void start() throws IOException, AWTException {
crsr = null;
break;
case 1:
crsr = ScreenRecorder.ENCODING_BLACK_CURSOR;
crsr = MouseConfigs.ENCODING_BLACK_CURSOR;
break;
case 2:
crsr = ScreenRecorder.ENCODING_WHITE_CURSOR;
crsr = MouseConfigs.ENCODING_WHITE_CURSOR;
break;
}
GraphicsConfiguration cfg = getGraphicsConfiguration();
Expand Down Expand Up @@ -790,7 +792,6 @@ protected void construct() throws Exception {

@Override
protected void finished() {
ScreenRecorder.State state = r.getState();
setSettingsEnabled(true);
startStopButton.setEnabled(true);
startStopButton.setText("Start");
Expand Down
Loading

0 comments on commit 1d573ac

Please sign in to comment.