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 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;
22 public class FfmpegCommand extends AbstractCommand {
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;
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;
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);
48 public boolean execute() throws InterruptedException, IOException {
52 if (commentMiddleFile.exists()) {
53 commentMiddleFile.delete();
55 if (tcommMiddleFile.exists()) {
56 tcommMiddleFile.delete();
58 if (TMP_CWS.exists()) {
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.
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");
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());
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");
85 sendText("
\93®
\89æ
\82Ì
\95Ï
\8a·
\82ð
\8aJ
\8en");
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");
92 sendText("
\95Ï
\8a·
\83G
\83\89\81[:" + convertedVideoFile.getPath());
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);
101 StringBuffer sb = new StringBuffer();
103 sb.append(getFfmpeg().getFfmpeg().getPath().replace("\\", "\\\\"));
106 sb.append(ov.getMainOption());
108 sb.append(ov.getInOption());
110 if (fwsFile == null) {
112 sb.append(videoFile.getPath().replace("\\", "\\\\"));
115 sb.append(fwsFile.getPath().replace("\\", "\\\\"));
118 sb.append(ov.getOutOption());
120 sb.append(convertedVideoFile.getPath().replace("\\", "\\\\"));
122 if (!getFfmpeg().isVhookDisabled()) {
123 if (!addVhookSetting(sb, addComment, vhook_path, addTcomment, tcommPath)) {
127 String cmd = sb.substring(0);
128 System.out.println("arg:" + cmd);
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()));
135 while ((e = ebr.readLine()) != null) {
137 if (state.startsWith("frame=")) {
139 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
140 System.out.println(e);
145 } catch (InterruptedException ex) {
152 return process.exitValue();
154 if (fwsFile != null) {
160 private boolean addVhookSetting(StringBuffer sb, boolean addComment, String vhook_path, boolean addTcomment,
163 sb.append(" -vfilters \"vhext=");
164 sb.append(getFfmpeg().getVhook().getPath().replace("\\", "/"));
167 sb.append("--data-user:");
168 sb.append(URLEncoder.encode(vhook_path.replace("\\", "/"), "Shift_JIS"));
172 sb.append("--data-owner:");
173 sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
176 sb.append("--font:");
177 sb.append(URLEncoder.encode(
178 getFfmpeg().getFont().getPath().replace("\\", "/"), "Shift_JIS"));
180 sb.append("--font-index:");
181 sb.append(getFfmpeg().getFontIndex());
183 sb.append("--show-user:");
184 sb.append(getFfmpeg().getMaxNumOfComment());
186 sb.append("--shadow:");
187 sb.append(getFfmpeg().getShadowIndex());
189 if (getFfmpeg().isShowConverting()) {
190 sb.append("--enable-show-video");
193 if (getFfmpeg().isSelfAdjustFontSize()) {
194 sb.append("--enable-fix-font-size");
197 if (getFfmpeg().isCommentOpaque()) {
198 sb.append("--enable-opaque-comment");
202 } catch (UnsupportedEncodingException e) {
208 private Ffmpeg getFfmpeg() {