2 package saccubus.converter;
4 import java.io.BufferedReader;
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.swf.Cws2Fws;
26 public class FfmpegCommand extends AbstractCommand {
28 private final File commentMiddleFile;
29 private final File tcommMiddleFile;
30 private final File TMP_CWS;
31 private final File commentFile;
32 private final File tcommFile;
33 private final File videoFile;
34 private final File convertedVideoFile;
35 private final Ffmpeg ffmpeg;
37 FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile, File tcommFile,
38 File videoFile, File convertedVideoFile, Ffmpeg ffmpeg, GeneralSetting general) throws IOException {
39 super(listener, flag);
40 this.commentFile = commentFile;
41 this.tcommFile = tcommFile;
42 this.videoFile = videoFile;
43 this.convertedVideoFile = convertedVideoFile;
46 File tmpDir = general.getTempDir();
47 commentMiddleFile = File.createTempFile("vhk", ".tmp", tmpDir);
48 tcommMiddleFile = File.createTempFile("tcom", ".tmp", tmpDir);
49 TMP_CWS = File.createTempFile("cws", ".swf", tmpDir);
52 public boolean execute() throws InterruptedException, IOException {
56 if (commentMiddleFile.exists()) {
57 commentMiddleFile.delete();
59 if (tcommMiddleFile.exists()) {
60 tcommMiddleFile.delete();
62 if (TMP_CWS.exists()) {
68 private boolean exec() throws InterruptedException, IOException {
69 final NgSetting ngSetting = getFfmpeg().getNgSetting();
70 if (commentFile != null) {
71 sendText("コメントの中間ファイルへの変換中");
72 boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, ngSetting.getId(), ngSetting.
75 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
80 if (tcommFile != null) {
81 sendText("投稿者コメントの中間ファイルへの変換中");
82 boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
84 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
91 if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
93 != null), tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
94 sendText("変換が正常に終了しました。");
97 sendText("変換エラー:" + convertedVideoFile.getPath());
102 private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String vhook_path,
103 boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
104 File fwsFile = Cws2Fws.createFws(videoFile, TMP_CWS);
106 List<String> cmdList = new ArrayList<String>();
107 cmdList.add(getFfmpeg().getFfmpeg().getPath());
109 String[] mainOptions = ov.getMainOption().split(" +");
110 for (String opt : mainOptions) {
111 if (StringUtils.isNotBlank(opt)) {
115 String[] inOptions = ov.getInOption().split(" +");
116 for (String opt : inOptions) {
117 if (StringUtils.isNotBlank(opt)) {
123 if (fwsFile == null) {
124 cmdList.add(videoFile.getPath());
126 cmdList.add(fwsFile.getPath());
128 String[] outOptions = ov.getOutOption().split(" +");
129 for (String opt : outOptions) {
130 if (StringUtils.isNotBlank(opt)) {
135 List<String> avfilterArgs = getAvfilterOptions(ov, addComment, vhook_path, addTcomment, tcommPath);
137 if (!avfilterArgs.isEmpty()) {
138 cmdList.add("-vfilters");
139 final String args = "\"" + StringUtils.join(avfilterArgs, ", ") + "\"";
143 cmdList.add(convertedVideoFile.getPath());
145 System.out.print("arg:");
146 for (String s : cmdList) {
147 System.out.print(" " + s);
149 System.out.println();
152 System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
153 Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
154 BufferedReader ebr = new BufferedReader(new InputStreamReader(
155 process.getErrorStream()));
157 while ((e = ebr.readLine()) != null) {
159 if (state.startsWith("frame=")) {
161 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
162 System.out.println(e);
167 } catch (InterruptedException ex) {
174 return process.exitValue();
176 if (fwsFile != null) {
182 private List<String> getAvfilterOptions(FfmpegOption ov, boolean addComment, String vhook_path, boolean addTcomment,
183 String tcommPath) throws UnsupportedEncodingException {
184 final List<String> avfilterArgs = new ArrayList<String>();
185 final String avfilterOption = ov.getAvfilterOption();
186 if (StringUtils.isNotBlank(avfilterOption)) {
187 avfilterArgs.add(avfilterOption);
189 final String vhookArg = (getFfmpeg().isVhookDisabled())
190 ? null : getVhookArg(addComment, vhook_path, addTcomment, tcommPath);
191 if (vhookArg != null) {
192 avfilterArgs.add(vhookArg);
197 private String getVhookArg(boolean addComment, String vhook_path, boolean addTcomment,
198 String tcommPath) throws UnsupportedEncodingException {
199 StringBuilder sb = new StringBuilder();
201 sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
204 sb.append("--data-user:");
205 sb.append(URLEncoder.encode(vhook_path.replace("\\", "/"), "Shift_JIS"));
209 sb.append("--data-owner:");
210 sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
213 sb.append("--font:");
214 sb.append(URLEncoder.encode(
215 getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
217 sb.append("--font-index:");
218 sb.append(getFfmpeg().getFontIndex());
220 sb.append("--show-user:");
221 sb.append(getFfmpeg().getMaxNumOfComment());
223 sb.append("--shadow:");
224 sb.append(getFfmpeg().getShadowIndex());
226 if (getFfmpeg().isShowConverting()) {
227 sb.append("--enable-show-video");
230 if (getFfmpeg().isSelfAdjustFontSize()) {
231 sb.append("--enable-fix-font-size");
234 if (getFfmpeg().isCommentOpaque()) {
235 sb.append("--enable-opaque-comment");
237 return sb.toString();
240 private Ffmpeg getFfmpeg() {