1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.concurrent.CompletableFuture;
import javax.imageio.ImageIO;
import org.apache.commons.exec.CommandLine; import org.apache.pdfbox.io.IOUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
@Component public class ConvertHelper { static final String EXT_PDF = ".pdf"; static final String EXT_MP4 = ".mp4"; static final String EXT_AVI = ".avi"; static final String EXT_DOC_STR = ".doc,.docx,.ppt,.pptx,.xls,.xlsx,"; static final String EXT_VIDEO_FFMPEG_STR = ".avi,.mpg,.wmv,.3gp,.mov,.asf,.asx,.flv,.f4v,.vob,.mkv,.ts,"; static final String EXT_VIDEO_MENCODER_STR = ".wmv9,.rm,.rmvb,.mpeg,";
@Autowired ConvertConfig convertConfig; public static String getExt(String filename) { if (StringUtils.isNullOrEmpty(filename)) return ""; int pos = filename.lastIndexOf("."); if (pos < 0) return ""; return filename.substring(pos).toLowerCase(); } public static boolean isDoc(String ext) { return EXT_DOC_STR.contains(ext + ','); }
public static boolean isVideo(String ext) { return EXT_VIDEO_FFMPEG_STR.contains(ext + ',') || needMEncoder(ext); }
public static boolean needMEncoder(String ext) { return EXT_VIDEO_MENCODER_STR.contains(ext + ','); }
public CompletableFuture<ConvertTask> process(ConvertTask task) { CompletableFuture<ConvertTask> cf = new CompletableFuture<>(); new Thread(() -> { String srcFile = task.getSrc(); String dotExt = getExt(srcFile); String outFile = null; CmdHandler cmdHandler = null; FileOutputStream logStream = null; try { File logFile = new File(convertConfig.getLog() + srcFile + ".log"); logStream = new FileOutputStream(logFile); if (isDoc(dotExt)) { outFile = srcFile + EXT_PDF; cmdHandler = doc2pdf(convertConfig.getSrc() + srcFile, convertConfig.getOut() + outFile, logStream); if (cmdHandler.getException() == null) { if (pdf2png(convertConfig.getOut() + outFile, convertConfig.getOut() + task.getSrc() + ".png")) { task.setImg(task.getSrc() + ".png"); } } } else if (isVideo(dotExt)) { if (needMEncoder(dotExt)) { outFile = srcFile + EXT_AVI; cmdHandler = video2avi(convertConfig.getSrc() + srcFile, convertConfig.getOut() + outFile, logStream); task.setRet(cmdHandler.resultString()); if (cmdHandler.getException() != null) { cf.complete(task); return; } srcFile = outFile; } cmdHandler = video2jpg(convertConfig.getSrc() + srcFile, convertConfig.getOut() + task.getSrc() + ".jpg", logStream); if (cmdHandler.getException() == null) { task.setImg(task.getSrc() + ".jpg"); } outFile = task.getSrc() + EXT_MP4; cmdHandler = video2mp4(convertConfig.getSrc() + srcFile, convertConfig.getOut() + outFile, logStream); } else { task.setRet("-1:unsupport"); cf.complete(task); return; } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(logStream); }
task.setOut(outFile); task.setRet(cmdHandler.resultString()); if (cmdHandler.getException() != null) { task.setOut(null); } cf.complete(task); }).start(); return cf; }
CmdHandler doc2pdf(String srcFile, String outFile, OutputStream cmdOut) { CommandLine cmd = new CommandLine(convertConfig.getUnoconv()); cmd.addArgument("-f"); cmd.addArgument("pdf"); cmd.addArgument("-o"); cmd.addArgument(outFile); cmd.addArgument(srcFile); return CmdHelper.run(cmd, cmdOut, 0); }
boolean pdf2png(String srcFile, String outFile) { try { File file = new File(srcFile); PDDocument doc; doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); BufferedImage image = renderer.renderImageWithDPI(1, 144); BufferedImage tag = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(image, 0, 0, 64, 64, null); ImageIO.write(tag, "PNG", new File(outFile)); return true; } catch (IOException e) { e.printStackTrace(); return false; } }
CmdHandler video2mp4(String srcFile, String outFile, OutputStream cmdOut) { CommandLine cmd = new CommandLine(convertConfig.getFfmpeg()); cmd.addArgument("-i"); cmd.addArgument(srcFile); cmd.addArgument("-ar"); cmd.addArgument("22050"); cmd.addArgument("-vcodec"); cmd.addArgument("libx264"); cmd.addArgument("-q:v"); cmd.addArgument("6"); cmd.addArgument("-r"); cmd.addArgument("25"); cmd.addArgument("-flags"); cmd.addArgument("+loop"); cmd.addArgument("-crf"); cmd.addArgument("24"); cmd.addArgument("-bt"); cmd.addArgument("256k"); cmd.addArgument("-af"); cmd.addArgument("volume=2"); cmd.addArgument("-y"); cmd.addArgument(outFile); return CmdHelper.run(cmd, cmdOut, 0); }
CmdHandler video2jpg(String srcFile, String outFile, OutputStream cmdOut) { CommandLine cmd = new CommandLine(convertConfig.getFfmpeg()); cmd.addArgument("-i"); cmd.addArgument(srcFile); cmd.addArgument("-f"); cmd.addArgument("image2"); cmd.addArgument("-ss"); cmd.addArgument("15"); cmd.addArgument("-t"); cmd.addArgument("0.001"); cmd.addArgument("-s"); cmd.addArgument("64x64"); cmd.addArgument("-y"); cmd.addArgument(outFile); return CmdHelper.run(cmd, cmdOut, 0); }
CmdHandler video2avi(String srcFile, String outFile, OutputStream cmdOut) { CommandLine cmd = new CommandLine(convertConfig.getMencoder()); cmd.addArgument(srcFile); cmd.addArgument("-oac"); cmd.addArgument("mp3lame"); cmd.addArgument("-lameopts"); cmd.addArgument("preset=64"); cmd.addArgument("-ovc"); cmd.addArgument("xvid"); cmd.addArgument("-xvidencopts"); cmd.addArgument("bitrate=600"); cmd.addArgument("-of"); cmd.addArgument("avi"); cmd.addArgument("-o"); cmd.addArgument(outFile); return CmdHelper.run(cmd, cmdOut, 0); } }
|