-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExploreSynopsis.java
359 lines (342 loc) · 12.3 KB
/
ExploreSynopsis.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Math.*;
import javax.sound.sampled.*;
import java.util.stream.IntStream;
public class ExploreSynopsis {
static int iw = 352;
static int ih = 288;
static int synopsisFrameNum = 20;
static int hFrameNum = 2;
static int wFrameNum = synopsisFrameNum / hFrameNum;
static int wSynopsis = (iw/2) * wFrameNum;
static int hSynopsis = (ih/2) * hFrameNum;
static int wPlayer = wSynopsis;
static int hPlayer = ih * 2 + hSynopsis;
static int fps = 30;
String synopsisPath;
BufferedImageWithMetaData synopsisImg;
ArrayList<BufferedImage[]> playVideoFrame; // store all video frames
ArrayList<HashMap<String, Integer>> playFrameStringToIdx;
ArrayList<BufferedImage> playImg; // only store ./image/image-xxx in synopsisImg
HashMap<String, Integer> playImgStringToIdx;
ArrayList<String> folderNameList;
JFrame jf;
JLabel jlVideoImg;
JLabel jlVideoImgText;
int currentFrame = 0; // from idx 0, not 1. If click synopsisImg or stop, it will be update
int currentVideoFolder = -1; // initially -1 means no setup
String currentAudioPath;
boolean currentIsVideo = true;
static int ST_STOP = 0;
static int ST_PLAY = 1;
int status = ST_STOP;
long audioStartTime; // real time in nano
long audioStarTimeStamp; // relative time (from start) in micro
/*
* Constructor
*/
public ExploreSynopsis(String synopsisPath)
{
this.synopsisPath = synopsisPath;
this.synopsisImg = new BufferedImageWithMetaData(synopsisFrameNum, wSynopsis, hSynopsis);
this.playVideoFrame = new ArrayList<BufferedImage[]>();
this.playFrameStringToIdx = new ArrayList<HashMap<String, Integer>>();
this.playImg = new ArrayList<BufferedImage>();
this.playImgStringToIdx = new HashMap<String, Integer>();
this.folderNameList = new ArrayList<String>();
this.jf = new JFrame();
this.jlVideoImg = new JLabel();
this.jlVideoImg.setHorizontalAlignment(JLabel.CENTER);
this.jlVideoImg.setVerticalAlignment(JLabel.CENTER);
this.jlVideoImgText = new JLabel();
this.jlVideoImgText.setHorizontalAlignment(JLabel.CENTER);
this.jlVideoImgText.setVerticalAlignment(JLabel.CENTER);
this.currentAudioPath = new String();
}
public void deserializeSynopsisImg()
{
try {
File file = new File(synopsisPath);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
synopsisImg.readObject(ois);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadImgInFolder()
{
// init playVideoFrame for multiple folder
int playImgIdx = 0;
for (int i = 0; i < synopsisFrameNum; i++) {
if (synopsisImg.metaData[i].isVideo) {
String parentPath = new File(synopsisImg.metaData[i].filePath).getParent();
if (!folderNameList.contains(parentPath)) {
folderNameList.add(parentPath);
}
} else {
BufferedImage img = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
readImageRGB(iw, ih, synopsisImg.metaData[i].filePath, img);
playImg.add(img);
playImgStringToIdx.put(synopsisImg.metaData[i].filePath, playImgIdx);
playImgIdx++;
}
}
// read all frames in video folders, use parallel stream to optimize reading
for (int i = 0; i < folderNameList.size(); i++) {
File[] framesList = new File(folderNameList.get(i)).listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
String fileName = file.getName();
return !fileName.substring(fileName.lastIndexOf(".")).equals(".wav");
}
});
Arrays.sort(framesList);
BufferedImage[] frames = new BufferedImage[framesList.length];
HashMap<String, Integer> frameStringToIdx = new HashMap<String, Integer>();
// parallel reading
IntStream.range(0, framesList.length).parallel().forEach(
j -> {
frameStringToIdx.put(framesList[j].getPath(), j);
BufferedImage frame = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
readImageRGB(iw, ih, framesList[j].getPath(), frame);
frames[j] = frame;
}
);
playFrameStringToIdx.add(frameStringToIdx);
playVideoFrame.add(frames);
}
}
public void showJFrame()
{
jf.setSize(wPlayer, hPlayer);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
JButton play = new JButton("Play");
JButton pause = new JButton("Pause");
JButton stop = new JButton("Stop");
JButton back1s = new JButton("Back 1s");
JButton forward1s = new JButton("Forward 1s");
JPanel jpPlayer = new JPanel(new BorderLayout());
jpPlayer.add(jlVideoImgText, BorderLayout.NORTH);
jpPlayer.add(jlVideoImg, BorderLayout.CENTER);
JPanel jpButton = new JPanel();
jpButton.add(play);
jpButton.add(pause);
jpButton.add(stop);
jpButton.add(back1s);
jpButton.add(forward1s);
jpPlayer.add(jpButton, BorderLayout.SOUTH);
jf.add(jpPlayer);
JLabel jlSynopsisImg = new JLabel(new ImageIcon(synopsisImg.img));
jf.add(jlSynopsisImg, BorderLayout.SOUTH);
jf.addMouseListener(
new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
int relativeX = e.getX() - (jf.getWidth() - wSynopsis) / 2;
int relativeY = e.getY() - jf.getHeight() + hSynopsis;
for (int i = 0; i < synopsisFrameNum; i++) {
MetaData md = synopsisImg.metaData[i];
if (relativeX >= md.wLower && relativeX < md.wUpper &&
relativeY >= md.hLower && relativeY < md.hUpper) {
if (md.isVideo) {
status = ST_STOP;
currentIsVideo = true;
currentVideoFolder = folderNameList.indexOf(new File(md.filePath).getParent());
currentFrame = playFrameStringToIdx.get(currentVideoFolder).get(md.filePath);
jlVideoImg.setIcon(new ImageIcon(playVideoFrame.get(currentVideoFolder)[currentFrame]));
jlVideoImgText.setText(new File(md.filePath).getParent());
currentAudioPath = md.audioPath;
} else {
status = ST_STOP;
currentIsVideo = false;
jlVideoImg.setIcon(new ImageIcon(playImg.get(playImgStringToIdx.get(md.filePath))));
jlVideoImgText.setText(md.filePath);
}
break;
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
}
);
play.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (status != ST_PLAY && currentIsVideo && currentVideoFolder > -1) {
if (currentFrame == playVideoFrame.get(currentVideoFolder).length) {
currentFrame = 0;
}
status = ST_PLAY;
new Thread(
new Runnable() {
@Override
public void run() {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(currentAudioPath));
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
audioStarTimeStamp = currentFrame * 1000000 / fps;
clip.setMicrosecondPosition(audioStarTimeStamp);
audioStartTime = System.nanoTime();
clip.start();
showIms();
clip.stop();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}
).start();
}
}
}
);
pause.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (status != ST_STOP && currentIsVideo) {
status = ST_STOP;
}
}
}
);
stop.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentIsVideo) {
currentFrame = 0;
status = ST_STOP;
jlVideoImg.setIcon(new ImageIcon(playVideoFrame.get(currentVideoFolder)[currentFrame]));
}
}
}
);
back1s.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentIsVideo) {
currentFrame = Math.max(0, currentFrame - fps);
status = ST_STOP;
jlVideoImg.setIcon(new ImageIcon(playVideoFrame.get(currentVideoFolder)[currentFrame]));
}
}
}
);
forward1s.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentIsVideo) {
currentFrame = Math.min(playVideoFrame.get(currentVideoFolder).length-1, currentFrame + fps);
status = ST_STOP;
jlVideoImg.setIcon(new ImageIcon(playVideoFrame.get(currentVideoFolder)[currentFrame]));
}
}
}
);
jf.setVisible(true);
}
public void showIms()
{
while (currentFrame < playVideoFrame.get(currentVideoFolder).length && status == ST_PLAY) {
jlVideoImg.setIcon(new ImageIcon(playVideoFrame.get(currentVideoFolder)[currentFrame]));
currentFrame++;
long nextTimeStamp = (long)(currentFrame * 1000000 / fps);
long deltaTime = + nextTimeStamp*1000 - audioStarTimeStamp*1000 - System.nanoTime() + audioStartTime;
try {
Thread.sleep((long)(deltaTime/1000000), (int)(deltaTime - (long)(deltaTime/1000000)*1000000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
status = ST_STOP; // for reaching video end
}
/*
* Show img, for debug
*/
public void showIm(BufferedImage img)
{
JFrame frame = new JFrame();
GridBagLayout gLayout = new GridBagLayout();
frame.getContentPane().setLayout(gLayout);
JLabel lbIm1 = new JLabel(new ImageIcon(img));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
frame.getContentPane().add(lbIm1, c);
frame.pack();
frame.setVisible(true);
}
/*
* Read Image RGB
*/
public void readImageRGB(int width, int height, String imgPath, BufferedImage img)
{
try {
int frameLength = width*height*3;
File file = new File(imgPath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(0);
long len = frameLength;
byte[] bytes = new byte[(int) len];
raf.read(bytes);
raf.close();
int ind = 0;
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
// byte a = 0;
byte r = bytes[ind];
byte g = bytes[ind+height*width];
byte b = bytes[ind+height*width*2];
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
//int pix = ((a << 24) + (r << 16) + (g << 8) + b);
img.setRGB(x,y,pix);
ind++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
if (args.length != 1) {
System.err.println("Arguments format error. It should be: synopsisPath.");
System.exit(1);
}
String synopsisPath = args[0];
ExploreSynopsis es = new ExploreSynopsis(synopsisPath);
es.deserializeSynopsisImg();
es.loadImgInFolder();
es.showJFrame();
}
}