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 saccubus.converter.profile.ConvertProfile;
18 import saccubus.converter.profile.FfmpegProfile;
19 import saccubus.converter.profile.GeneralProfile;
20 import saccubus.net.TextProgressListener;
21 import yukihane.mediainfowrapper.Info;
22 import yukihane.mediainfowrapper.MediaInfo;
23 import yukihane.mediainfowrapper.Size;
24 import saccubus.converter.profile.ConvertProfile.HideCondition;
25 import yukihane.swf.Cws2Fws;
31 public class FfmpegCommand extends AbstractCommand<FfmpegCommandResult, FfmpegCommandProgress> {
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 {
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);
57 protected FfmpegCommandResult doInBackground() throws Exception {
61 if (commentMiddleFile.exists()) {
62 commentMiddleFile.delete();
64 if (tcommMiddleFile.exists()) {
65 tcommMiddleFile.delete();
67 if (TMP_CWS.exists()) {
73 private FfmpegCommandResult exec() throws InterruptedException, IOException {
74 final HideCondition ngSetting = getFfmpeg().getNgSetting();
75 if (commentFile != null) {
76 sendText("コメントの中間ファイルへの変換中");
77 boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, ngSetting.getId(), ngSetting.
80 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
81 return new FfmpegCommandResult(false);
85 // if (tcommFile != null) {
86 // sendText("投稿者コメントの中間ファイルへの変換中");
87 // boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
89 // sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
96 if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
97 false, tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
98 sendText("変換が正常に終了しました。");
99 return new FfmpegCommandResult(true);
101 sendText("変換エラー:" + convertedVideoFile.getPath());
103 return new FfmpegCommandResult(false);
106 private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String commPath,
107 boolean addTcomment, String tcommPath, FfmpegProfile ov) throws InterruptedException, IOException {
108 File fwsFile = Cws2Fws.createFws(videoFile, TMP_CWS);
110 List<String> cmdList = new ArrayList<String>();
111 cmdList.add(getFfmpeg().getFfmpeg().getPath());
113 String[] mainOptions = ov.getMainOption().split(" +");
114 for (String opt : mainOptions) {
115 if (StringUtils.isNotBlank(opt)) {
119 String[] inOptions = ov.getInOption().split(" +");
120 for (String opt : inOptions) {
121 if (StringUtils.isNotBlank(opt)) {
127 if (fwsFile == null) {
128 cmdList.add(videoFile.getPath());
130 cmdList.add(fwsFile.getPath());
132 String[] outOptions = ov.getOutOption().split(" +");
133 for (String opt : outOptions) {
134 if (StringUtils.isNotBlank(opt)) {
139 final Info info = MediaInfo.getInfo(new File("bin", "MediaInfo"), videoFile);
140 // 4:3 なら1.33, 16:9 なら1.76
141 boolean isHD = ((double)info.getWidth()/(double)info.getHeight() > 1.5);
143 final Size scaled = (ov.isAdjustRatio()) ? MediaInfo.adjustSize(info, ov.getResizeWidth(), ov.
144 getResizeHeight()) : new Size(info.getWidth(), info.getHeight());
146 cmdList.add(scaled.getWidth() + "x" + scaled.getHeight());
149 List<String> avfilterArgs = getAvfilterOptions(ov.getAvfilterOption());
150 if (!getFfmpeg().isVhookDisabled()) {
151 String vhookArg = getVhookArg(addComment, commPath, addTcomment, tcommPath, isHD);
152 if(StringUtils.isNotBlank(vhookArg)){
153 avfilterArgs.add(vhookArg);
157 if (!avfilterArgs.isEmpty()) {
158 cmdList.add("-vfilters");
159 final String args = "\"" + StringUtils.join(avfilterArgs, ", ") + "\"";
163 cmdList.add(convertedVideoFile.getPath());
165 final StringBuilder argMsg = new StringBuilder();
166 argMsg.append("arg:");
167 for (String s : cmdList) {
168 argMsg.append(" ").append(s);
170 logger.log(Level.INFO, argMsg.toString());
172 Process process = null;
174 logger.log(Level.INFO, "\n\n----\nProcessing FFmpeg...\n----\n\n");
175 process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
176 BufferedReader ebr = new BufferedReader(new InputStreamReader(
177 process.getErrorStream()));
179 while ((e = ebr.readLine()) != null) {
181 if (state.startsWith("frame=")) {
183 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
184 logger.log(Level.INFO, e);
189 } catch (InterruptedException ex) {
195 return process.exitValue();
197 // TODO 正常終了した場合もdestroyしていいのか?
198 if (process != null) {
201 if (fwsFile != null) {
207 private List<String> getAvfilterOptions(String avfilterOption) {
208 final List<String> avfilterArgs = new ArrayList<String>();
209 if (StringUtils.isNotBlank(avfilterOption)) {
210 avfilterArgs.add(avfilterOption);
215 private String getVhookArg(boolean addComment, String commPath, boolean addTcomment,
216 String tcommPath, boolean isHD) throws UnsupportedEncodingException {
217 StringBuilder sb = new StringBuilder();
219 sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
222 sb.append("--data-user:");
223 sb.append(URLEncoder.encode(commPath.replace("\\", "/"), "Shift_JIS"));
227 sb.append("--data-owner:");
228 sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
231 sb.append("--font:");
232 sb.append(URLEncoder.encode(
233 getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
235 sb.append("--font-index:");
236 sb.append(getFfmpeg().getFontIndex());
238 sb.append("--show-user:");
239 sb.append(getFfmpeg().getMaxNumOfComment());
241 sb.append("--shadow:");
242 sb.append(getFfmpeg().getShadowIndex());
244 if (getFfmpeg().isShowConverting()) {
245 sb.append("--enable-show-video");
248 if (!getFfmpeg().isDisableFontSizeArrange()) {
249 sb.append("--enable-fix-font-size");
252 if (getFfmpeg().isCommentOpaque()) {
253 sb.append("--enable-opaque-comment");
257 sb.append("--aspect-mode:1");
260 return sb.toString();
263 private ConvertProfile getFfmpeg() {