OSDN Git Service

c56cc485411049f0b025cb58ecae9d1ee44a5c19
[coroid/inqubus.git] / frontend / src / saccubus / converter / FfmpegCommand.java
1 /* $Id$ */
2 package saccubus.converter;
3
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.UnsupportedEncodingException;
9 import java.net.URLEncoder;
10 import saccubus.ConvertStopFlag;
11 import saccubus.conv.ConvertToVideoHook;
12 import saccubus.converter.profile.Ffmpeg;
13 import saccubus.converter.profile.FfmpegOption;
14 import saccubus.converter.profile.NgSetting;
15 import saccubus.net.TextProgressListener;
16 import yukihane.swf.Cws2Fws;
17
18 /**
19  *
20  * @author yuki
21  */
22 public class FfmpegCommand extends AbstractCommand {
23
24     private final File commentMiddleFile = new File("./vhook.tmp");
25     private final File tcommMiddleFile = new File("./tcomment.tmp");
26     private static final String TMP_CWS = "fws_tmp.swf";
27     private final File commentFile;
28     private final File tcommFile;
29     private final File videoFile;
30     private final File convertedVideoFile;
31     private final Ffmpeg ffmpeg;
32
33     FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile, File tcommFile,
34             File videoFile, File convertedVideoFile, Ffmpeg ffmpeg) {
35         super(listener, flag);
36         this.commentFile = commentFile;
37         this.tcommFile = tcommFile;
38         this.videoFile = videoFile;
39         this.convertedVideoFile = convertedVideoFile;
40         this.ffmpeg = ffmpeg;
41
42     }
43
44     public boolean execute() throws InterruptedException, IOException {
45         try {
46             return exec();
47         } finally {
48             if (commentMiddleFile.exists()) {
49                 commentMiddleFile.delete();
50             }
51             if (tcommMiddleFile.exists()) {
52                 tcommMiddleFile.delete();
53             }
54         }
55     }
56
57     private boolean exec() throws InterruptedException, IOException {
58         final NgSetting ngSetting = getFfmpeg().getNgSetting();
59         if (commentFile != null) {
60             sendText("\83R\83\81\83\93\83g\82Ì\92\86\8aÔ\83t\83@\83C\83\8b\82Ö\82Ì\95Ï\8a·\92\86");
61             boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, ngSetting.getId(), ngSetting.
62                     getWord());
63             if (!conv) {
64                 sendText("\83R\83\81\83\93\83g\95Ï\8a·\82É\8e¸\94s\81B\83t\83@\83C\83\8b\96¼\82É\8eg\97p\82Å\82«\82È\82¢\95\8e\9a\82ª\8aÜ\82Ü\82ê\82Ä\82¢\82é\82©\90³\8bK\95\\8c»\82Ì\8aÔ\88á\82¢\81H");
65                 return false;
66             }
67         }
68         stopFlagReturn();
69         if (tcommFile != null) {
70             sendText("\93\8a\8de\8eÒ\83R\83\81\83\93\83g\82Ì\92\86\8aÔ\83t\83@\83C\83\8b\82Ö\82Ì\95Ï\8a·\92\86");
71             boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
72             if (!conv) {
73                 sendText("\83R\83\81\83\93\83g\95Ï\8a·\82É\8e¸\94s\81B\83t\83@\83C\83\8b\96¼\82É\8eg\97p\82Å\82«\82È\82¢\95\8e\9a\82ª\8aÜ\82Ü\82ê\82Ä\82¢\82é\82©\90³\8bK\95\\8c»\82Ì\8aÔ\88á\82¢\81H");
74                 return false;
75             }
76         }
77         stopFlagReturn();
78         sendText("\93®\89æ\82Ì\95Ï\8a·\82ð\8aJ\8en");
79         int code;
80         if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(), (tcommFile
81                 != null), tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
82             sendText("\95Ï\8a·\82ª\90³\8fí\82É\8fI\97¹\82µ\82Ü\82µ\82½\81B");
83             return true;
84         } else {
85             sendText("\95Ï\8a·\83G\83\89\81[:" + convertedVideoFile.getPath());
86         }
87         return false;
88     }
89
90     private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String vhook_path,
91             boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
92         File fwsFile = Cws2Fws.createFws(videoFile, new File(TMP_CWS));
93
94         StringBuffer sb = new StringBuffer();
95         sb.append("\"");
96         sb.append(getFfmpeg().getFfmpeg().getPath().replace("\\", "\\\\"));
97         sb.append("\"");
98         sb.append(" -y ");
99         sb.append(ov.getMainOption());
100         sb.append(" ");
101         sb.append(ov.getInOption());
102         sb.append(" -i ");
103         if (fwsFile == null) {
104             sb.append("\"");
105             sb.append(videoFile.getPath().replace("\\", "\\\\"));
106             sb.append("\"");
107         } else {
108             sb.append(fwsFile.getPath().replace("\\", "\\\\"));
109         }
110         sb.append(" ");
111         sb.append(ov.getOutOption());
112         sb.append(" \"");
113         sb.append(convertedVideoFile.getPath().replace("\\", "\\\\"));
114         sb.append("\"");
115         if (!getFfmpeg().isVhookDisabled()) {
116             if (!addVhookSetting(sb, addComment, vhook_path, addTcomment, tcommPath)) {
117                 return -1;
118             }
119         }
120         String cmd = sb.substring(0);
121         System.out.println("arg:" + cmd);
122         try {
123             System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
124             Process process = Runtime.getRuntime().exec(cmd);
125             BufferedReader ebr = new BufferedReader(new InputStreamReader(
126                     process.getErrorStream()));
127             String e;
128             while ((e = ebr.readLine()) != null) {
129                 String state = e;
130                 if (state.startsWith("frame=")) {
131                     sendText(state);
132                 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
133                     System.out.println(e);
134                 }
135
136                 try {
137                     stopFlagReturn();
138                 } catch (InterruptedException ex) {
139                     process.destroy();
140                     throw ex;
141                 }
142
143             }
144             process.waitFor();
145             return process.exitValue();
146         } finally {
147             if (fwsFile != null) {
148                 fwsFile.delete();
149             }
150         }
151     }
152
153     private boolean addVhookSetting(StringBuffer sb, boolean addComment, String vhook_path, boolean addTcomment,
154             String tcommPath) {
155         try {
156             sb.append(" -vfilters \"vhext=");
157             sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
158             if (addComment) {
159                 sb.append("|");
160                 sb.append("--data-user:");
161                 sb.append(URLEncoder.encode(vhook_path.replace("\\", "/"), "Shift_JIS"));
162             }
163             if (addTcomment) {
164                 sb.append("|");
165                 sb.append("--data-owner:");
166                 sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
167             }
168             sb.append("|");
169             sb.append("--font:");
170             sb.append(URLEncoder.encode(
171                     getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
172             sb.append("|");
173             sb.append("--font-index:");
174             sb.append(getFfmpeg().getFontIndex());
175             sb.append("|");
176             sb.append("--show-user:");
177             sb.append(getFfmpeg().getMaxNumOfComment());
178             sb.append("|");
179             sb.append("--shadow:");
180             sb.append(getFfmpeg().getShadowIndex());
181             sb.append("|");
182             if (getFfmpeg().isShowConverting()) {
183                 sb.append("--enable-show-video");
184                 sb.append("|");
185             }
186             if (getFfmpeg().isSelfAdjustFontSize()) {
187                 sb.append("--enable-fix-font-size");
188                 sb.append("|");
189             }
190             if (getFfmpeg().isCommentOpaque()) {
191                 sb.append("--enable-opaque-comment");
192             }
193             sb.append("\"");
194             return true;
195         } catch (UnsupportedEncodingException e) {
196             e.printStackTrace();
197             return false;
198         }
199     }
200
201     private Ffmpeg getFfmpeg() {
202         return ffmpeg;
203     }
204 }