OSDN Git Service

f630148867db917550e6ace7ff58066d8e4d22f5
[coroid/inqubus.git] / frontend / src / saccubus / Converter.java
1 package saccubus;
2
3 import javax.swing.JLabel;
4 import saccubus.net.NicoClient;
5 import java.io.*;
6
7 import saccubus.conv.ConvertToVideoHook;
8 import java.net.URLEncoder;
9 import java.util.Properties;
10 import yukihane.swf.Cws2Fws;
11
12 /**
13  * <p>\83^\83C\83g\83\8b\82³\82«\82ã\82Î\82·</p>
14  *
15  * <p>\90à\96¾: \83j\83R\83j\83R\93®\89æ\82Ì\93®\89æ\82ð\83R\83\81\83\93\83g\82Â\82«\82Å\95Û\91¶</p>
16  *
17  * <p>\92\98\8dì\8c : Copyright (c) 2007 PSI</p>
18  *
19  * <p>\89ï\8eÐ\96¼: </p>
20  *
21  * @author \96¢\93ü\97Í
22  * @version 1.0
23  */
24 public class Converter extends Thread {
25         private final ConvertingSetting Setting;
26
27         private final String Tag;
28
29         private final String VideoID;
30
31         private String VideoTitle;
32
33         private final String Time;
34
35         private JLabel Status;
36
37         private final ConvertStopFlag StopFlag;
38
39         private static final String TMP_COMMENT_MIDDLE_FILE = "./vhook.tmp";
40         private static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
41
42         public Converter(String url, String time, ConvertingSetting setting,
43                         JLabel status, ConvertStopFlag flag) {
44                 url = url.trim();
45                 if(url.startsWith(VIDEO_URL_PARSER)){
46                         int index = url.indexOf('?',VIDEO_URL_PARSER.length());
47                         if(index >= 0){
48                                 Tag = url.substring(VIDEO_URL_PARSER.length(),index);
49                         }else{
50                                 Tag = url.substring(VIDEO_URL_PARSER.length());
51                         }
52                 }else{
53                         Tag = url;
54                 }
55                 VideoID = "[" + Tag + "]";
56                 Time = time;
57                 Setting = setting;
58                 Status = status;
59                 StopFlag = flag;
60         }
61
62         private File VideoFile = null;
63
64         private File CommentFile = null;
65
66         private File ConvertedVideoFile = null;
67
68         private File CommentMiddleFile = null;
69
70     @Override
71         public void run() {
72                 boolean converted = false;
73                 try {
74                         if (!Setting.isSaveConverted() && !Setting.isSaveComment()
75                     && (Setting.getVideoSaveKind() != VideoSaveKind.SAVE)) {
76                                 Status.setText("\89½\82à\82·\82é\82±\82Æ\82ª\82 \82è\82Ü\82¹\82ñ");
77                                 return;
78                         }
79                         if (Setting.isSaveConverted()) {
80                                 File a = new File(Setting.getFFmpegPath());
81                                 if (!a.canRead()) {
82                                         Status.setText("FFmpeg\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
83                                         return;
84                                 }
85                                 if (Setting.getVhookPath().indexOf(' ') >= 0) {
86                                         Status.setText("\82·\82¢\82Ü\82¹\82ñ\81B\8c»\8dÝvhook\83\89\83C\83u\83\89\83\8a\82É\82Í\94¼\8ap\8bó\94\92\82Í\8eg\82¦\82Ü\82¹\82ñ\81B");
87                                         return;
88                                 }
89                                 a = new File(Setting.getVhookPath());
90                                 if (!a.canRead()) {
91                                         Status.setText("Vhook\83\89\83C\83u\83\89\83\8a\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
92                                         return;
93                                 }
94                                 a = new File(Setting.getFontPath());
95                                 if (!a.canRead()) {
96                                         Status.setText("\83t\83H\83\93\83g\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
97                                         return;
98                                 }
99                                 if (!detectOption()) {
100                                         Status.setText("\95Ï\8a·\83I\83v\83V\83\87\83\93\83t\83@\83C\83\8b\82Ì\93Ç\82Ý\8d\9e\82Ý\82É\8e¸\94s\82µ\82Ü\82µ\82½\81B");
101                                         return;
102                                 }
103                         } else {
104                                 if (Setting.isDeleteVideoAfterConverting()) {
105                                         Status.setText("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93®\89æ\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
106                                         return;
107                                 }
108                                 if (Setting.isDeleteCommentAfterConverting()) {
109                                         Status.setText("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
110                                         return;
111                                 }
112                         }
113                         NicoClient client = null;
114             if ((Setting.getVideoSaveKind() == VideoSaveKind.SAVE) || Setting.isSaveComment()) {
115                                 if (Setting.getMailAddress() == null
116                                                 || Setting.getPassword() == null
117                                                 || Setting.getMailAddress().equals("")
118                                                 || Setting.getPassword().equals("")) {
119                                         Status.setText("\83\81\81[\83\8b\83A\83h\83\8c\83X\82©\83p\83X\83\8f\81[\83h\82ª\8bó\94\92\82Å\82·\81B");
120                                         return;
121                                 }
122                                 if (Setting.useProxy()
123                                                 && (Setting.getProxy() == null || Setting.getProxy()
124                                                                 .length() <= 0)
125                                                 && (Setting.getProxyPort() < 0 || Setting
126                                                                 .getProxyPort() > 65535)) {
127                                         Status.setText("\83v\83\8d\83L\83V\82Ì\90Ý\92è\82ª\95s\90³\82Å\82·\81B");
128                                         return;
129                                 }
130                                 if (stopFlagReturn()) {
131                                         return;
132                                 }
133                                 Status.setText("\83\8d\83O\83C\83\93\92\86");
134                                 String proxy;
135                                 int proxy_port;
136                                 if (Setting.useProxy()) {
137                                         proxy = Setting.getProxy();
138                                         proxy_port = Setting.getProxyPort();
139                                 } else {
140                                         proxy = null;
141                                         proxy_port = -1;
142                                 }
143                                 client = new NicoClient(Setting.getMailAddress(), Setting
144                                                 .getPassword(), StopFlag, proxy, proxy_port);
145
146                                 if (!client.isLoggedIn()) {
147                                         Status.setText("\83\8d\83O\83C\83\93\82É\8e¸\94s");
148                                         return;
149                                 }
150                                 if (stopFlagReturn()) {
151                                         return;
152                                 }
153                                 /*\93®\89æ\82Ì\95Û\91¶*/
154                                 if (!client.getVideoInfo(Tag, Time)) {
155                                         Status.setText(Tag + "\82Ì\8fî\95ñ\82Ì\8eæ\93¾\82É\8e¸\94s");
156                                         return;
157                                 }
158                                 if (stopFlagReturn()) {
159                                         return;
160                                 }
161                                 VideoTitle = client.getVideoTitle();
162                         }
163             if (Setting.getVideoSaveKind() == VideoSaveKind.SAVE) {
164                                 if (Setting.isVideoFixFileName()) {
165                                         Setting.getVideoFixFileNameFolder().mkdir();
166                                         VideoFile = new File(Setting.getVideoFixFileNameFolder(),
167                                                         VideoID + VideoTitle + ".flv");
168                                 } else {
169                                         VideoFile = Setting.getVideoFile();
170                                 }
171                                 Status.setText("\93®\89æ\82Ì\83_\83E\83\93\83\8d\81[\83h\8aJ\8en\92\86");
172                                 VideoFile = client.getVideo(VideoFile, Status);
173                                 if (stopFlagReturn()) {
174                                         return;
175                                 }
176                                 if (VideoFile == null) {
177                                         Status.setText("\93®\89æ\82Ì\83_\83E\83\93\83\8d\81[\83h\82É\8e¸\94s");
178                                         return;
179                                 }
180                         } else {
181                                 if (Setting.isSaveConverted()) {
182                     if (Setting.getVideoSaveKind() == VideoSaveKind.NO_SAVE) {
183                         if (Setting.isVideoFixFileName()) {
184                             if (!detectVideoTitle(Setting.getVideoFixFileNameFolder())) {
185                                 Status.setText("\93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
186                                 return;
187                             }
188                             VideoFile = new File(Setting.getVideoFixFileNameFolder(), VideoID
189                                     + VideoTitle + ".flv");
190                             if (!VideoFile.canRead()) {
191                                 Status.setText("\93®\89æ\83t\83@\83C\83\8b\82ª\93Ç\82Ý\8d\9e\82ß\82Ü\82¹\82ñ\81B");
192                                 return;
193                             }
194                         } else {
195                             VideoFile = Setting.getVideoFile();
196                             if (!VideoFile.exists()) {
197                                 Status.setText("\93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
198                                 return;
199                             }
200                         }
201                     } else if (Setting.getVideoSaveKind() == VideoSaveKind.NICOBROWSER) {
202                         VideoFile = Setting.getVideoFile();
203                         if (!VideoFile.isFile()) {
204                             Status.setText("\93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
205                             return;
206                         }
207                     }
208                 }
209                         }
210
211                         if (stopFlagReturn()) {
212                                 return;
213                         }
214
215                         if (Setting.isSaveComment()) {
216                                 if (Setting.isCommentFixFileName()) {
217                                         Setting.getCommentFixFileNameFolder().mkdir();
218                                         CommentFile = new File(Setting
219                                                         .getCommentFixFileNameFolder(), VideoID
220                                                         + VideoTitle + ".xml");
221                                 } else {
222                                         CommentFile = Setting.getCommentFile();
223                                 }
224                                 String back_comment = Setting.getBackComment();
225                                 if (Setting.isFixCommentNum()) {
226                                         back_comment = client
227                                                         .getBackCommentFromLength(back_comment);
228                                 }
229                                 Status.setText("\83R\83\81\83\93\83g\82Ì\83_\83E\83\93\83\8d\81[\83h\8aJ\8en\92\86");
230                                 CommentFile = client.getComment(CommentFile, Status,
231                                                 back_comment);
232                                 if (stopFlagReturn()) {
233                                         return;
234                                 }
235                                 if (CommentFile == null) {
236                                         Status.setText("\83R\83\81\83\93\83g\82Ì\83_\83E\83\93\83\8d\81[\83h\82É\8e¸\94s");
237                                         return;
238                                 }
239                         } else {
240                                 if (Setting.isSaveConverted()) {
241                                         if (Setting.isCommentFixFileName()) {
242                                                 if(!detectVideoTitle(Setting.getCommentFixFileNameFolder())){
243                                                         Status.setText("\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
244                                                         return;
245                                                 }
246                                                 CommentFile = new File(Setting
247                                                                 .getCommentFixFileNameFolder(), VideoID
248                                                                 + VideoTitle + ".xml");
249                                                 if (!CommentFile.canRead()) {
250                                                         Status.setText("\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\93Ç\82Ý\8d\9e\82ß\82Ü\82¹\82ñ\81B");
251                                                         return;
252                                                 }
253                                         } else {
254                                                 CommentFile = Setting.getCommentFile();
255                                                 if (!CommentFile.exists()) {
256                                                         Status.setText("\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
257                                                         return;
258                                                 }
259                                         }
260                                 }
261                         }
262
263                         if (stopFlagReturn()) {
264                                 return;
265                         }
266
267                         if (!Setting.isSaveConverted()) {
268                                 Status.setText("\93®\89æ\81E\83R\83\81\83\93\83g\82ð\95Û\91\82µ\81A\95Ï\8a·\82Í\8ds\82¢\82Ü\82¹\82ñ\82Å\82µ\82½\81B");
269                                 return;
270                         }
271                         CommentMiddleFile = new File(TMP_COMMENT_MIDDLE_FILE);
272                         Status.setText("\83R\83\81\83\93\83g\82Ì\92\86\8aÔ\83t\83@\83C\83\8b\82Ö\82Ì\95Ï\8a·\92\86");
273                         boolean conv = ConvertToVideoHook
274                                         .convert(CommentFile, CommentMiddleFile,
275                                                         Setting.getNG_ID(), Setting.getNG_Word());
276                         if (!conv) {
277                                 Status.setText("\83R\83\81\83\93\83g\95Ï\8a·\82É\8e¸\94s\81B\82¨\82»\82ç\82­\90³\8bK\95\\8c»\82Ì\8aÔ\88á\82¢\81H");
278                                 return;
279                         }
280                         if (stopFlagReturn()) {
281                                 return;
282                         }
283                         Status.setText("\93®\89æ\82Ì\95Ï\8a·\82ð\8aJ\8en");
284                         /*\83r\83f\83I\96¼\82Ì\8am\92è*/
285                         if (Setting.isConvFixFileName()) {
286                                 if (VideoTitle == null) {
287                                         Status.setText("\95Ï\8a·\8cã\82Ì\83r\83f\83I\83t\83@\83C\83\8b\96¼\82ª\8am\92è\82Å\82«\82Ü\82¹\82ñ\81B");
288                                         return;
289                                 }
290                                 Setting.getConvFixFileNameFolder().mkdir();
291                                 String conv_name = VideoTitle;
292                                 if (!Setting.isNotAddVideoID_Conv()) {//\95t\89Á\82µ\82È\82¢\82È\82ç
293                                         conv_name = VideoID + conv_name;
294                                 }
295                                 ConvertedVideoFile = new File(Setting
296                                                 .getConvFixFileNameFolder(), conv_name + ExtOption);
297                         } else {
298                                 String filename = Setting.getConvertedVideoFile().getPath();
299                                 if (!filename.endsWith(ExtOption)) {
300                                         filename = filename.substring(0, filename.lastIndexOf('.'))
301                                                         + ExtOption;
302                                         ConvertedVideoFile = new File(filename);
303                                 } else {
304                                         ConvertedVideoFile = Setting.getConvertedVideoFile();
305                                 }
306                         }
307                         int code;
308                         if ((code = converting_video(TMP_COMMENT_MIDDLE_FILE)) == 0) {
309                                 converted = true;
310                                 Status.setText("\95Ï\8a·\82ª\90³\8fí\82É\8fI\97¹\82µ\82Ü\82µ\82½\81B");
311                         } else if (code == CODE_CONVERTING_ABORTED) { /*\92\86\92f*/
312
313                         } else {
314                                 Status.setText("\95Ï\8a·\83G\83\89\81[\81F" + LastError);
315                         }
316
317                 } finally {
318                         StopFlag.finished();
319                         if (CommentMiddleFile != null) {
320                                 CommentMiddleFile.delete();
321                         }
322                         if (converted) {
323                                 if (Setting.isDeleteCommentAfterConverting()
324                                                 && CommentFile != null) {
325                                         CommentFile.delete();
326                                 }
327                                 if (Setting.isDeleteVideoAfterConverting() && VideoFile != null) {
328                                         VideoFile.delete();
329                                 }
330                         }
331                 }
332         }
333
334         String LastError;
335
336         private static final int CODE_CONVERTING_ABORTED = 100;
337
338         private int converting_video(String vhook_path) {
339         File fwsFile = Cws2Fws.createFws(VideoFile, new File("fws_tmp.swf"));
340
341                 StringBuffer sb = new StringBuffer();
342                 sb.append("\"");
343                 sb.append(Setting.getFFmpegPath().replace("\\", "\\\\"));
344                 sb.append("\"");
345                 sb.append(" -y ");
346                 sb.append(MainOption);
347                 sb.append(" ");
348                 sb.append(InOption);
349                 sb.append(" -i ");
350                 if (fwsFile == null) {
351                         sb.append("\"");
352                         sb.append(VideoFile.getPath().replace("\\", "\\\\"));
353                         sb.append("\"");
354                 } else {
355                         sb.append(fwsFile.getPath().replace("\\", "\\\\"));
356                 }
357                 sb.append(" ");
358                 sb.append(OutOption);
359                 sb.append(" \"");
360                 sb.append(ConvertedVideoFile.getPath().replace("\\", "\\\\"));
361                 sb.append("\"");
362                 if (!Setting.isVhookDisabled()) {
363                         if(!addVhookSetting(sb, vhook_path)){
364                                 return -1;
365                         }
366                 }
367                 String cmd = sb.substring(0);
368                 System.out.println("arg:" + cmd);
369                 try {
370                         System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
371                         Process process = Runtime.getRuntime().exec(cmd);
372                         BufferedReader ebr = new BufferedReader(new InputStreamReader(
373                                         process.getErrorStream()));
374                         String e;
375                         while ((e = ebr.readLine()) != null) {
376                                 LastError = e;
377                                 if (LastError.startsWith("frame=")) { //
378                                         Status.setText(LastError);
379                                 } else if(!LastError.endsWith("No accelerated colorspace conversion found")){
380                                         System.out.println(e);
381                                 }
382                                 if (stopFlagReturn()) {
383                                         process.destroy();
384                                         return CODE_CONVERTING_ABORTED;
385                                 }
386                         }
387                         process.waitFor();
388                         return process.exitValue();
389                 } catch (InterruptedException ex) {
390                         ex.printStackTrace();
391                         return -1;
392                 } catch (IOException ex) {
393                         ex.printStackTrace();
394                         return -1;
395                 } finally {
396                         if (fwsFile != null) {
397                                 fwsFile.delete();
398                         }
399                 }
400         }
401
402         private boolean addVhookSetting(StringBuffer sb, String vhook_path) {
403                 try {
404                         sb.append(" -vfilters \"vhext=");
405                         sb.append(Setting.getVhookPath().replace("\\", "/"));
406                         sb.append("|");
407                         sb.append("--data-user:");
408                         sb.append(URLEncoder.encode(vhook_path.replace("\\","/"),
409                                         "Shift_JIS"));
410                         sb.append("|");
411                         sb.append("--font:");
412                         sb.append(URLEncoder.encode(
413                                         Setting.getFontPath().replace("\\","/"), "Shift_JIS"));
414                         sb.append("|");
415                         sb.append("--font-index:");
416                         sb.append(Setting.getFontIndex());
417                         sb.append("|");
418                         sb.append("--show-user:");
419                         sb.append(Setting.getVideoShowNum());
420                         sb.append("|");
421                         sb.append("--shadow:");
422                         sb.append(Setting.getShadowIndex());
423                         sb.append("|");
424                         if (Setting.isVhook_ShowConvertingVideo()) {
425                                 sb.append("--enable-show-video");
426                                 sb.append("|");
427                         }
428                         if (Setting.isFixFontSize()) {
429                                 sb.append("--enable-fix-font-size");
430                                 sb.append("|");
431                         }
432                         if (Setting.isOpaqueComment()) {
433                                 sb.append("--enable-opaque-comment");
434                         }
435                         sb.append("\"");
436                         return true;
437                 } catch (UnsupportedEncodingException e) {
438                         e.printStackTrace();
439                         return false;
440                 }
441         }
442
443         public boolean isConverted() {
444                 return StopFlag.isConverted();
445         }
446
447         private boolean stopFlagReturn() {
448                 if (StopFlag.needStop()) {
449                         this.Status.setText("\92\86\8e~\82µ\82Ü\82µ\82½\81B");
450                         return true;
451                 }
452                 return false;
453         }
454
455         public ConvertStopFlag getStopFlag() {
456                 return this.StopFlag;
457         }
458
459         private String ExtOption;
460
461         private String InOption;
462
463         private String OutOption;
464
465         private String MainOption;
466
467         private boolean detectOption() {
468                 boolean ret;
469                 if (Setting.getOptionFile() != null) {
470                         try {
471                                 Properties prop = new Properties();
472                                 prop.loadFromXML(new FileInputStream(Setting.getOptionFile()));
473                                 ExtOption = prop.getProperty("EXT", null);
474                                 InOption = prop.getProperty("IN", null);
475                                 OutOption = prop.getProperty("OUT", null);
476                                 MainOption = prop.getProperty("MAIN", null);
477                                 if (ExtOption != null && InOption != null && OutOption != null
478                                                 && MainOption != null) {
479                                         ret = true;
480                                 } else {
481                                         ret = false;
482                                 }
483                         } catch (IOException ex) {
484                                 ex.printStackTrace();
485                                 ret = false;
486                         }
487                 } else {
488                         ExtOption = Setting.getCmdLineOptionExt();
489                         InOption = Setting.getCmdLineOptionIn();
490                         OutOption = Setting.getCmdLineOptionOut();
491                         MainOption = Setting.getCmdLineOptionMain();
492                         ret = true;
493                 }
494                 //\83I\83v\83V\83\87\83\93\82É\8ag\92£\8eq\82ð\8aÜ\82ñ\82Å\82µ\82Ü\82Á\82½\8fê\8d\87\82É\82à\91Î\89\9e\81\99
495                 if(!ExtOption.startsWith(".")){
496                         ExtOption = "."+ExtOption;
497                 }
498                 return ret;
499         }
500         private boolean detectVideoTitle(File dir){
501                 String list[] = dir.list();
502                 if(list == null){
503                         return false;
504                 }
505                 for (int i = 0; i < list.length; i++) {
506                         if (list[i].startsWith(VideoID)) {
507                                 VideoTitle = list[i].substring(VideoID.length(),
508                                                 list[i].lastIndexOf("."));
509                                 return true;
510                         }
511                 }
512                 return false;
513         }
514 }