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 java.util.logging.Level;
13 import java.util.logging.Logger;
14 import org.apache.commons.lang.StringUtils;
15 import saccubus.ConvertStopFlag;
16 import saccubus.conv.ConvertToVideoHook;
17 import yukihane.saccubus.converter.profile.NgSetting;
18 import yukihane.saccubus.converter.profile.ConvertProfile;
19 import yukihane.saccubus.converter.profile.FfmpegOption;
20 import yukihane.saccubus.converter.profile.GeneralProfile;
21 import saccubus.net.TextProgressListener;
22 import yukihane.mediainfowrapper.Info;
23 import yukihane.mediainfowrapper.MediaInfo;
24 import yukihane.mediainfowrapper.Size;
25 import yukihane.swf.Cws2Fws;
31 public class FfmpegCommand extends AbstractCommand {
32 private static final Logger logger = Logger.getLogger(FfmpegCommand.class.getName());
33 private final File commentMiddleFile;
34 private final File tcommMiddleFile;
35 private final File TMP_CWS;
36 private final File commentFile;
37 private final File videoFile;
38 private final File convertedVideoFile;
39 private final ConvertProfile ffmpeg;
41 FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile,
42 File videoFile, File convertedVideoFile, ConvertProfile ffmpeg, GeneralProfile general) throws IOException {
43 super(listener, flag);
44 this.commentFile = commentFile;
45 this.videoFile = videoFile;
46 this.convertedVideoFile = convertedVideoFile;
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);
55 public boolean execute() throws InterruptedException, IOException {
59 if (commentMiddleFile.exists()) {
60 commentMiddleFile.delete();
62 if (tcommMiddleFile.exists()) {
63 tcommMiddleFile.delete();
65 if (TMP_CWS.exists()) {
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.
78 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
83 // if (tcommFile != null) {
84 // sendText("投稿者コメントの中間ファイルへの変換中");
85 // boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
87 // sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
94 if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
95 false, tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
96 sendText("変換が正常に終了しました。");
99 sendText("変換エラー:" + convertedVideoFile.getPath());
104 private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String commPath,
105 boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
106 File fwsFile = Cws2Fws.createFws(videoFile, TMP_CWS);
108 List<String> cmdList = new ArrayList<String>();
109 cmdList.add(getFfmpeg().getFfmpeg().getPath());
111 String[] mainOptions = ov.getMainOption().split(" +");
112 for (String opt : mainOptions) {
113 if (StringUtils.isNotBlank(opt)) {
117 String[] inOptions = ov.getInOption().split(" +");
118 for (String opt : inOptions) {
119 if (StringUtils.isNotBlank(opt)) {
125 if (fwsFile == null) {
126 cmdList.add(videoFile.getPath());
128 cmdList.add(fwsFile.getPath());
130 String[] outOptions = ov.getOutOption().split(" +");
131 for (String opt : outOptions) {
132 if (StringUtils.isNotBlank(opt)) {
137 final Info info = MediaInfo.getInfo(new File("bin", "MediaInfo"), videoFile);
138 // 4:3 なら1.33, 16:9 なら1.76
139 boolean isHD = ((double)info.getWidth()/(double)info.getHeight() > 1.5);
141 final Size scaled = (ov.isAdjustRatio()) ? MediaInfo.adjustSize(info, ov.getResizeWidth(), ov.
142 getResizeHeight()) : new Size(info.getWidth(), info.getHeight());
144 cmdList.add(scaled.getWidth() + "x" + scaled.getHeight());
147 List<String> avfilterArgs = getAvfilterOptions(ov.getAvfilterOption());
148 if (!getFfmpeg().isVhookDisabled()) {
149 String vhookArg = getVhookArg(addComment, commPath, addTcomment, tcommPath, isHD);
150 if(StringUtils.isNotBlank(vhookArg)){
151 avfilterArgs.add(vhookArg);
155 if (!avfilterArgs.isEmpty()) {
156 cmdList.add("-vfilters");
157 final String args = "\"" + StringUtils.join(avfilterArgs, ", ") + "\"";
161 cmdList.add(convertedVideoFile.getPath());
163 final StringBuilder argMsg = new StringBuilder();
164 argMsg.append("arg:");
165 for (String s : cmdList) {
166 argMsg.append(" ").append(s);
168 logger.log(Level.INFO, argMsg.toString());
171 logger.log(Level.INFO, "\n\n----\nProcessing FFmpeg...\n----\n\n");
172 Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
173 BufferedReader ebr = new BufferedReader(new InputStreamReader(
174 process.getErrorStream()));
176 while ((e = ebr.readLine()) != null) {
178 if (state.startsWith("frame=")) {
180 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
181 logger.log(Level.INFO, e);
186 } catch (InterruptedException ex) {
193 return process.exitValue();
195 if (fwsFile != null) {
201 private List<String> getAvfilterOptions(String avfilterOption) {
202 final List<String> avfilterArgs = new ArrayList<String>();
203 if (StringUtils.isNotBlank(avfilterOption)) {
204 avfilterArgs.add(avfilterOption);
209 private String getVhookArg(boolean addComment, String commPath, boolean addTcomment,
210 String tcommPath, boolean isHD) throws UnsupportedEncodingException {
211 StringBuilder sb = new StringBuilder();
213 sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
216 sb.append("--data-user:");
217 sb.append(URLEncoder.encode(commPath.replace("\\", "/"), "Shift_JIS"));
221 sb.append("--data-owner:");
222 sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
225 sb.append("--font:");
226 sb.append(URLEncoder.encode(
227 getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
229 sb.append("--font-index:");
230 sb.append(getFfmpeg().getFontIndex());
232 sb.append("--show-user:");
233 sb.append(getFfmpeg().getMaxNumOfComment());
235 sb.append("--shadow:");
236 sb.append(getFfmpeg().getShadowIndex());
238 if (getFfmpeg().isShowConverting()) {
239 sb.append("--enable-show-video");
242 if (!getFfmpeg().isDisableFontSizeArrange()) {
243 sb.append("--enable-fix-font-size");
246 if (getFfmpeg().isCommentOpaque()) {
247 sb.append("--enable-opaque-comment");
251 sb.append("--aspect-mode:1");
254 return sb.toString();
257 private ConvertProfile getFfmpeg() {