OSDN Git Service

一時ファイルについて、
[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;
25     private final File tcommMiddleFile;
26     private final File TMP_CWS;
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) throws IOException {
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         File tmpDir = new File("tmp");
43         commentMiddleFile = File.createTempFile("vhk", ".tmp", tmpDir);
44         tcommMiddleFile = File.createTempFile("tcom", ".tmp", tmpDir);
45         TMP_CWS = File.createTempFile("cws", ".swf", tmpDir);
46     }
47
48     public boolean execute() throws InterruptedException, IOException {
49         try {
50             return exec();
51         } finally {
52             if (commentMiddleFile.exists()) {
53                 commentMiddleFile.delete();
54             }
55             if (tcommMiddleFile.exists()) {
56                 tcommMiddleFile.delete();
57             }
58             if (TMP_CWS.exists()) {
59                 TMP_CWS.delete();
60             }
61         }
62     }
63
64     private boolean exec() throws InterruptedException, IOException {
65         final NgSetting ngSetting = getFfmpeg().getNgSetting();
66         if (commentFile != null) {
67             sendText("\83R\83\81\83\93\83g\82Ì\92\86\8aÔ\83t\83@\83C\83\8b\82Ö\82Ì\95Ï\8a·\92\86");
68             boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, ngSetting.getId(), ngSetting.
69                     getWord());
70             if (!conv) {
71                 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");
72                 return false;
73             }
74         }
75         stopFlagReturn();
76         if (tcommFile != null) {
77             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");
78             boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
79             if (!conv) {
80                 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");
81                 return false;
82             }
83         }
84         stopFlagReturn();
85         sendText("\93®\89æ\82Ì\95Ï\8a·\82ð\8aJ\8en");
86         int code;
87         if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(), (tcommFile
88                 != null), tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
89             sendText("\95Ï\8a·\82ª\90³\8fí\82É\8fI\97¹\82µ\82Ü\82µ\82½\81B");
90             return true;
91         } else {
92             sendText("\95Ï\8a·\83G\83\89\81[:" + convertedVideoFile.getPath());
93         }
94         return false;
95     }
96
97     private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String vhook_path,
98             boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
99         File fwsFile = Cws2Fws.createFws(videoFile, TMP_CWS);
100
101         StringBuffer sb = new StringBuffer();
102         sb.append("\"");
103         sb.append(getFfmpeg().getFfmpeg().getPath().replace("\\", "\\\\"));
104         sb.append("\"");
105         sb.append(" -y ");
106         sb.append(ov.getMainOption());
107         sb.append(" ");
108         sb.append(ov.getInOption());
109         sb.append(" -i ");
110         if (fwsFile == null) {
111             sb.append("\"");
112             sb.append(videoFile.getPath().replace("\\", "\\\\"));
113             sb.append("\"");
114         } else {
115             sb.append(fwsFile.getPath().replace("\\", "\\\\"));
116         }
117         sb.append(" ");
118         sb.append(ov.getOutOption());
119         sb.append(" \"");
120         sb.append(convertedVideoFile.getPath().replace("\\", "\\\\"));
121         sb.append("\"");
122         if (!getFfmpeg().isVhookDisabled()) {
123             if (!addVhookSetting(sb, addComment, vhook_path, addTcomment, tcommPath)) {
124                 return -1;
125             }
126         }
127         String cmd = sb.substring(0);
128         System.out.println("arg:" + cmd);
129         try {
130             System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
131             Process process = Runtime.getRuntime().exec(cmd);
132             BufferedReader ebr = new BufferedReader(new InputStreamReader(
133                     process.getErrorStream()));
134             String e;
135             while ((e = ebr.readLine()) != null) {
136                 String state = e;
137                 if (state.startsWith("frame=")) {
138                     sendText(state);
139                 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
140                     System.out.println(e);
141                 }
142
143                 try {
144                     stopFlagReturn();
145                 } catch (InterruptedException ex) {
146                     process.destroy();
147                     throw ex;
148                 }
149
150             }
151             process.waitFor();
152             return process.exitValue();
153         } finally {
154             if (fwsFile != null) {
155                 fwsFile.delete();
156             }
157         }
158     }
159
160     private boolean addVhookSetting(StringBuffer sb, boolean addComment, String vhook_path, boolean addTcomment,
161             String tcommPath) {
162         try {
163             sb.append(" -vfilters \"vhext=");
164             sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
165             if (addComment) {
166                 sb.append("|");
167                 sb.append("--data-user:");
168                 sb.append(URLEncoder.encode(vhook_path.replace("\\", "/"), "Shift_JIS"));
169             }
170             if (addTcomment) {
171                 sb.append("|");
172                 sb.append("--data-owner:");
173                 sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
174             }
175             sb.append("|");
176             sb.append("--font:");
177             sb.append(URLEncoder.encode(
178                     getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
179             sb.append("|");
180             sb.append("--font-index:");
181             sb.append(getFfmpeg().getFontIndex());
182             sb.append("|");
183             sb.append("--show-user:");
184             sb.append(getFfmpeg().getMaxNumOfComment());
185             sb.append("|");
186             sb.append("--shadow:");
187             sb.append(getFfmpeg().getShadowIndex());
188             sb.append("|");
189             if (getFfmpeg().isShowConverting()) {
190                 sb.append("--enable-show-video");
191                 sb.append("|");
192             }
193             if (getFfmpeg().isSelfAdjustFontSize()) {
194                 sb.append("--enable-fix-font-size");
195                 sb.append("|");
196             }
197             if (getFfmpeg().isCommentOpaque()) {
198                 sb.append("--enable-opaque-comment");
199             }
200             sb.append("\"");
201             return true;
202         } catch (UnsupportedEncodingException e) {
203             e.printStackTrace();
204             return false;
205         }
206     }
207
208     private Ffmpeg getFfmpeg() {
209         return ffmpeg;
210     }
211 }