OSDN Git Service

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