OSDN Git Service

6b47594a2effd2ec4d8c57654eb2fffa46684efa
[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 java.util.ArrayList;
11 import java.util.List;
12 import org.apache.commons.lang.StringUtils;
13 import saccubus.ConvertStopFlag;
14 import saccubus.conv.ConvertToVideoHook;
15 import saccubus.converter.profile.Ffmpeg;
16 import saccubus.converter.profile.FfmpegOption;
17 import saccubus.converter.profile.GeneralSetting;
18 import saccubus.converter.profile.NgSetting;
19 import saccubus.net.TextProgressListener;
20 import yukihane.mediainfowrapper.Info;
21 import yukihane.mediainfowrapper.MediaInfo;
22 import yukihane.mediainfowrapper.Size;
23 import yukihane.swf.Cws2Fws;
24
25 /**
26  *
27  * @author yuki
28  */
29 public class FfmpegCommand extends AbstractCommand {
30
31     private final File commentMiddleFile;
32     private final File tcommMiddleFile;
33     private final File TMP_CWS;
34     private final File commentFile;
35     private final File tcommFile;
36     private final File videoFile;
37     private final File convertedVideoFile;
38     private final Ffmpeg ffmpeg;
39
40     FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile, File tcommFile,
41             File videoFile, File convertedVideoFile, Ffmpeg ffmpeg, GeneralSetting general) throws IOException {
42         super(listener, flag);
43         this.commentFile = commentFile;
44         this.tcommFile = tcommFile;
45         this.videoFile = videoFile;
46         this.convertedVideoFile = convertedVideoFile;
47         this.ffmpeg = ffmpeg;
48
49         File tmpDir = general.getTempDir();
50         commentMiddleFile = File.createTempFile("vhk", ".tmp", tmpDir);
51         tcommMiddleFile = File.createTempFile("tcom", ".tmp", tmpDir);
52         TMP_CWS = File.createTempFile("cws", ".swf", tmpDir);
53     }
54
55     public boolean execute() throws InterruptedException, IOException {
56         try {
57             return exec();
58         } finally {
59             if (commentMiddleFile.exists()) {
60                 commentMiddleFile.delete();
61             }
62             if (tcommMiddleFile.exists()) {
63                 tcommMiddleFile.delete();
64             }
65             if (TMP_CWS.exists()) {
66                 TMP_CWS.delete();
67             }
68         }
69     }
70
71     private boolean exec() throws InterruptedException, IOException {
72         final NgSetting ngSetting = getFfmpeg().getNgSetting();
73         if (commentFile != null) {
74             sendText("コメントの中間ファイルへの変換中");
75             boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, ngSetting.getId(), ngSetting.
76                     getWord());
77             if (!conv) {
78                 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
79                 return false;
80             }
81         }
82         stopFlagReturn();
83         if (tcommFile != null) {
84             sendText("投稿者コメントの中間ファイルへの変換中");
85             boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
86             if (!conv) {
87                 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
88                 return false;
89             }
90         }
91         stopFlagReturn();
92         sendText("動画の変換を開始");
93         int code;
94         if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
95                 (tcommFile
96                 != null), tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
97             sendText("変換が正常に終了しました。");
98             return true;
99         } else {
100             sendText("変換エラー:" + convertedVideoFile.getPath());
101         }
102         return false;
103     }
104
105     private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String commPath,
106             boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
107         File fwsFile = Cws2Fws.createFws(videoFile, TMP_CWS);
108
109         List<String> cmdList = new ArrayList<String>();
110         cmdList.add(getFfmpeg().getFfmpeg().getPath());
111         cmdList.add("-y");
112         String[] mainOptions = ov.getMainOption().split(" +");
113         for (String opt : mainOptions) {
114             if (StringUtils.isNotBlank(opt)) {
115                 cmdList.add(opt);
116             }
117         }
118         String[] inOptions = ov.getInOption().split(" +");
119         for (String opt : inOptions) {
120             if (StringUtils.isNotBlank(opt)) {
121                 cmdList.add(opt);
122             }
123         }
124         cmdList.add("-i");
125
126         if (fwsFile == null) {
127             cmdList.add(videoFile.getPath());
128         } else {
129             cmdList.add(fwsFile.getPath());
130         }
131         String[] outOptions = ov.getOutOption().split(" +");
132         for (String opt : outOptions) {
133             if (StringUtils.isNotBlank(opt)) {
134                 cmdList.add(opt);
135             }
136         }
137
138         if (ov.isResize()) {
139             final Info info = MediaInfo.getInfo(new File("bin", "MediaInfo"), videoFile);
140             final Size scaled = (ov.isAdjustRatio()) ? MediaInfo.adjustSize(info, ov.getResizeWidth(), ov.
141                     getResizeHeight()) : new Size(info.getWidth(), info.getHeight());
142             cmdList.add("-s");
143             cmdList.add(scaled.getWidth() + "x" + scaled.getHeight());
144         }
145
146         List<String> avfilterArgs = getAvfilterOptions(ov, addComment, commPath, addTcomment, tcommPath);
147
148         if (!avfilterArgs.isEmpty()) {
149             cmdList.add("-vfilters");
150             final String args = "\"" + StringUtils.join(avfilterArgs, ", ") + "\"";
151             cmdList.add(args);
152         }
153
154         cmdList.add(convertedVideoFile.getPath());
155
156         System.out.print("arg:");
157         for (String s : cmdList) {
158             System.out.print(" " + s);
159         }
160         System.out.println();
161
162         try {
163             System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
164             Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
165             BufferedReader ebr = new BufferedReader(new InputStreamReader(
166                     process.getErrorStream()));
167             String e;
168             while ((e = ebr.readLine()) != null) {
169                 String state = e;
170                 if (state.startsWith("frame=")) {
171                     sendText(state);
172                 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
173                     System.out.println(e);
174                 }
175
176                 try {
177                     stopFlagReturn();
178                 } catch (InterruptedException ex) {
179                     process.destroy();
180                     throw ex;
181                 }
182
183             }
184             process.waitFor();
185             return process.exitValue();
186         } finally {
187             if (fwsFile != null) {
188                 fwsFile.delete();
189             }
190         }
191     }
192
193     private List<String> getAvfilterOptions(FfmpegOption ov, boolean addComment, String commPath, boolean addTcomment,
194             String tcommPath) throws UnsupportedEncodingException {
195         final List<String> avfilterArgs = new ArrayList<String>();
196         final String avfilterOption = ov.getAvfilterOption();
197         if (StringUtils.isNotBlank(avfilterOption)) {
198             avfilterArgs.add(avfilterOption);
199         }
200         final String vhookArg = (getFfmpeg().isVhookDisabled())
201                 ? null : getVhookArg(addComment, commPath, addTcomment, tcommPath);
202         if (vhookArg != null) {
203             avfilterArgs.add(vhookArg);
204         }
205         return avfilterArgs;
206     }
207
208     private String getVhookArg(boolean addComment, String commPath, boolean addTcomment,
209             String tcommPath) throws UnsupportedEncodingException {
210         StringBuilder sb = new StringBuilder();
211         sb.append("vhext=");
212         sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
213         if (addComment) {
214             sb.append("|");
215             sb.append("--data-user:");
216             sb.append(URLEncoder.encode(commPath.replace("\\", "/"), "Shift_JIS"));
217         }
218         if (addTcomment) {
219             sb.append("|");
220             sb.append("--data-owner:");
221             sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
222         }
223         sb.append("|");
224         sb.append("--font:");
225         sb.append(URLEncoder.encode(
226                 getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
227         sb.append("|");
228         sb.append("--font-index:");
229         sb.append(getFfmpeg().getFontIndex());
230         sb.append("|");
231         sb.append("--show-user:");
232         sb.append(getFfmpeg().getMaxNumOfComment());
233         sb.append("|");
234         sb.append("--shadow:");
235         sb.append(getFfmpeg().getShadowIndex());
236         sb.append("|");
237         if (getFfmpeg().isShowConverting()) {
238             sb.append("--enable-show-video");
239             sb.append("|");
240         }
241         if (getFfmpeg().isSelfAdjustFontSize()) {
242             sb.append("--enable-fix-font-size");
243             sb.append("|");
244         }
245         if (getFfmpeg().isCommentOpaque()) {
246             sb.append("--enable-opaque-comment");
247         }
248         return sb.toString();
249     }
250
251     private Ffmpeg getFfmpeg() {
252         return ffmpeg;
253     }
254 }