3 import javax.swing.JLabel;
4 import saccubus.net.NicoClient;
7 import saccubus.conv.ConvertToVideoHook;
8 import java.net.URLEncoder;
9 import java.util.Properties;
10 import saccubus.util.Cws2Fws;
13 * <p>
\83^
\83C
\83g
\83\8b:
\82³
\82«
\82ã
\82Î
\82·</p>
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>
17 * <p>
\92\98\8dì
\8c : Copyright (c) 2007 PSI</p>
19 * <p>
\89ï
\8eÐ
\96¼: </p>
21 * @author
\96¢
\93ü
\97Í
24 public class Converter extends Thread {
25 private ConvertingSetting Setting;
29 private String VideoID;
31 private String VideoTitle;
35 private JLabel Status;
37 private final ConvertStopFlag StopFlag;
39 private static final String TMP_COMMENT_MIDDLE_FILE = "./vhook.tmp";
40 private static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
42 public Converter(String url, String time, ConvertingSetting setting,
43 JLabel status, ConvertStopFlag flag) {
45 if(url.startsWith(VIDEO_URL_PARSER)){
46 int index = url.indexOf('?',VIDEO_URL_PARSER.length());
48 Tag = url.substring(VIDEO_URL_PARSER.length(),index);
50 Tag = url.substring(VIDEO_URL_PARSER.length());
55 VideoID = "[" + Tag + "]";
62 private File VideoFile = null;
64 private File CommentFile = null;
66 private File ConvertedVideoFile = null;
68 private File CommentMiddleFile = null;
71 boolean converted = false;
73 if (!Setting.isSaveConverted() && !Setting.isSaveComment()
74 && !Setting.isSaveVideo()) {
75 Status.setText("
\89½
\82à
\82·
\82é
\82±
\82Æ
\82ª
\82 \82è
\82Ü
\82¹
\82ñ");
78 if (Setting.isSaveConverted()) {
79 File a = new File(Setting.getFFmpegPath());
81 Status.setText("FFmpeg
\82ª
\8c©
\82Â
\82©
\82è
\82Ü
\82¹
\82ñ
\81B");
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");
88 a = new File(Setting.getVhookPath());
90 Status.setText("Vhook
\83\89\83C
\83u
\83\89\83\8a\82ª
\8c©
\82Â
\82©
\82è
\82Ü
\82¹
\82ñ
\81B");
93 a = new File(Setting.getFontPath());
95 Status.setText("
\83t
\83H
\83\93\83g
\82ª
\8c©
\82Â
\82©
\82è
\82Ü
\82¹
\82ñ
\81B");
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");
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");
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");
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");
121 if (Setting.useProxy()
122 && (Setting.getProxy() == null || Setting.getProxy()
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");
129 if (stopFlagReturn()) {
132 Status.setText("
\83\8d\83O
\83C
\83\93\92\86");
135 if (Setting.useProxy()) {
136 proxy = Setting.getProxy();
137 proxy_port = Setting.getProxyPort();
142 client = new NicoClient(Setting.getMailAddress(), Setting
143 .getPassword(), StopFlag, proxy, proxy_port);
145 if (!client.isLoggedIn()) {
146 Status.setText("
\83\8d\83O
\83C
\83\93\82É
\8e¸
\94s");
149 if (stopFlagReturn()) {
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");
157 if (stopFlagReturn()) {
160 VideoTitle = client.getVideoTitle();
162 if (Setting.isSaveVideo()) {
163 if (Setting.isVideoFixFileName()) {
164 Setting.getVideoFixFileNameFolder().mkdir();
165 VideoFile = new File(Setting.getVideoFixFileNameFolder(),
166 VideoID + VideoTitle + ".flv");
168 VideoFile = Setting.getVideoFile();
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()) {
175 if (VideoFile == null) {
176 Status.setText("
\93®
\89æ
\82Ì
\83_
\83E
\83\93\83\8d\81[
\83h
\82É
\8e¸
\94s");
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");
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");
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");
203 if (stopFlagReturn()) {
207 if (Setting.isSaveComment()) {
208 if (Setting.isCommentFixFileName()) {
209 Setting.getCommentFixFileNameFolder().mkdir();
210 CommentFile = new File(Setting
211 .getCommentFixFileNameFolder(), VideoID
212 + VideoTitle + ".xml");
214 CommentFile = Setting.getCommentFile();
216 String back_comment = Setting.getBackComment();
217 if (Setting.isFixCommentNum()) {
218 back_comment = client
219 .getBackCommentFromLength(back_comment);
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,
224 if (stopFlagReturn()) {
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");
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");
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");
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");
255 if (stopFlagReturn()) {
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");
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());
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");
272 if (stopFlagReturn()) {
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");
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;
287 ConvertedVideoFile = new File(Setting
288 .getConvFixFileNameFolder(), conv_name + ExtOption);
290 String filename = Setting.getConvertedVideoFile().getPath();
291 if (!filename.endsWith(ExtOption)) {
292 filename = filename.substring(0, filename.lastIndexOf('.'))
294 ConvertedVideoFile = new File(filename);
296 ConvertedVideoFile = Setting.getConvertedVideoFile();
300 if ((code = converting_video(TMP_COMMENT_MIDDLE_FILE)) == 0) {
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*/
306 Status.setText("
\95Ï
\8a·
\83G
\83\89\81[
\81F" + LastError);
311 if (CommentMiddleFile != null) {
312 CommentMiddleFile.delete();
315 if (Setting.isDeleteCommentAfterConverting()
316 && CommentFile != null) {
317 CommentFile.delete();
319 if (Setting.isDeleteVideoAfterConverting() && VideoFile != null) {
328 private static final int CODE_CONVERTING_ABORTED = 100;
330 private int converting_video(String vhook_path) {
331 File fwsFile = Cws2Fws.createFws(VideoFile);
333 StringBuffer sb = new StringBuffer();
335 sb.append(Setting.getFFmpegPath().replace("\\", "\\\\"));
338 sb.append(MainOption);
342 if (fwsFile == null) {
344 sb.append(VideoFile.getPath().replace("\\", "\\\\"));
347 sb.append(fwsFile.getPath().replace("\\", "\\\\"));
350 sb.append(OutOption);
352 sb.append(ConvertedVideoFile.getPath().replace("\\", "\\\\"));
354 if (!Setting.isVhookDisabled()) {
355 if(!addVhookSetting(sb, vhook_path)){
359 String cmd = sb.substring(0);
360 System.out.println("arg:" + cmd);
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()));
367 while ((e = ebr.readLine()) != null) {
369 if (LastError.startsWith("frame=")) { //
370 Status.setText(LastError);
371 } else if(!LastError.endsWith("No accelerated colorspace conversion found")){
372 System.out.println(e);
374 if (stopFlagReturn()) {
376 return CODE_CONVERTING_ABORTED;
380 return process.exitValue();
381 } catch (InterruptedException ex) {
382 ex.printStackTrace();
384 } catch (IOException ex) {
385 ex.printStackTrace();
388 if (fwsFile != null) {
394 private boolean addVhookSetting(StringBuffer sb, String vhook_path) {
396 sb.append(" -vfilters \"vhext=");
397 sb.append(Setting.getVhookPath().replace("\\", "/"));
399 sb.append("--data-user:");
400 sb.append(URLEncoder.encode(vhook_path.replace("\\","/"),
403 sb.append("--font:");
404 sb.append(URLEncoder.encode(
405 Setting.getFontPath().replace("\\","/"), "Shift_JIS"));
407 sb.append("--font-index:");
408 sb.append(Setting.getFontIndex());
410 sb.append("--show-user:");
411 sb.append(Setting.getVideoShowNum());
413 sb.append("--shadow:");
414 sb.append(Setting.getShadowIndex());
416 if (Setting.isVhook_ShowConvertingVideo()) {
417 sb.append("--enable-show-video");
420 if (Setting.isFixFontSize()) {
421 sb.append("--enable-fix-font-size");
424 if (Setting.isOpaqueComment()) {
425 sb.append("--enable-opaque-comment");
429 } catch (UnsupportedEncodingException e) {
435 public boolean isConverted() {
436 return StopFlag.isConverted();
439 private boolean stopFlagReturn() {
440 if (StopFlag.needStop()) {
441 this.Status.setText("
\92\86\8e~
\82µ
\82Ü
\82µ
\82½
\81B");
447 public ConvertStopFlag getStopFlag() {
448 return this.StopFlag;
451 private String ExtOption;
453 private String InOption;
455 private String OutOption;
457 private String MainOption;
459 private boolean detectOption() {
461 if (Setting.getOptionFile() != null) {
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) {
475 } catch (IOException ex) {
476 ex.printStackTrace();
480 ExtOption = Setting.getCmdLineOptionExt();
481 InOption = Setting.getCmdLineOptionIn();
482 OutOption = Setting.getCmdLineOptionOut();
483 MainOption = Setting.getCmdLineOptionMain();
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;
492 private boolean detectVideoTitle(File dir){
493 String list[] = dir.list();
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("."));