diff --git a/src/main/java/com/dsideal/QingLong/Render/ImageRender.java b/src/main/java/com/dsideal/QingLong/Render/ImageRender.java new file mode 100644 index 00000000..398cc781 --- /dev/null +++ b/src/main/java/com/dsideal/QingLong/Render/ImageRender.java @@ -0,0 +1,47 @@ +package com.dsideal.QingLong.Render; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; + +import com.jfinal.render.Render; + +/** + * 用来向客户端渲染图片 + * + * @author CC11001100 + */ +public class ImageRender extends Render { + + /** + * 要渲染到客户端去的图像文件 + */ + private File imgFile; + + public ImageRender(File imgFile) { + super(); + this.imgFile = imgFile; + } + + public File getImgFile() { + return imgFile; + } + + public void setImgFile(File imgFile) { + this.imgFile = imgFile; + } + + /** + * 将特定的图片写回到客户端 + */ + public void render() { + response.setContentType("image/jpeg"); + try { + FileUtils.copyFile(imgFile, response.getOutputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/dsideal/QingLong/YunXiao/Controller/YunXiaoController.java b/src/main/java/com/dsideal/QingLong/YunXiao/Controller/YunXiaoController.java index 1d7fade6..e5ecbafc 100644 --- a/src/main/java/com/dsideal/QingLong/YunXiao/Controller/YunXiaoController.java +++ b/src/main/java/com/dsideal/QingLong/YunXiao/Controller/YunXiaoController.java @@ -1,9 +1,11 @@ package com.dsideal.QingLong.YunXiao.Controller; import com.dsideal.QingLong.Interceptor.IsLoginInterface; +import com.dsideal.QingLong.Render.ImageRender; import com.dsideal.QingLong.Util.CommonUtil; import com.dsideal.QingLong.YunXiao.Model.YunXiaoModel; import com.dsideal.QingLong.YunXiao.Util.YunXiaoExportExcelUtil; +import com.dsideal.QingLong.YunXiao.Util.YunXiaoVideoUtil; import com.jfinal.aop.Before; import com.jfinal.core.Controller; import com.jfinal.ext.interceptor.GET; @@ -112,7 +114,7 @@ public class YunXiaoController extends Controller { //获取系统临时目录 String tmpDir = System.getProperty("java.io.tmpdir"); //使用guid生成一个临时文件名,然后拼接到tmpDir后面,生成完整的临时文件路径 - String tmpFile = tmpDir + File.separator + UUID.randomUUID() + ".xlsx"; + String tmpFile = tmpDir + UUID.randomUUID() + ".xlsx"; exporter.LessonConstructionExportExcel(tmpFile, list1, list2, list3); //提供下载 renderFile(new File(tmpFile), "课程建设情况报表.xlsx"); @@ -133,9 +135,25 @@ public class YunXiaoController extends Controller { //获取系统临时目录 String tmpDir = System.getProperty("java.io.tmpdir"); //使用guid生成一个临时文件名,然后拼接到tmpDir后面,生成完整的临时文件路径 - String tmpFile = tmpDir + File.separator + UUID.randomUUID() + ".xlsx"; + String tmpFile = tmpDir + UUID.randomUUID() + ".xlsx"; exporter.LessonConstructionInfoByXzqhSchool(tmpFile, list1, list2, list3, list4); //提供下载 renderFile(new File(tmpFile), "【区域+学校】课程建设情况报表.xlsx"); } + + /** + * 生成视频截图 + * @param url 视频地址 + */ + // http://10.10.21.20:9000/QingLong/yx/getLessonCover?url= + @Before(GET.class) + public void getLessonCover(String url) { + //获取系统临时目录 + String tmpDir = System.getProperty("java.io.tmpdir"); + //使用guid生成一个临时文件名,然后拼接到tmpDir后面,生成完整的临时文件路径 + String tmpFile = tmpDir + UUID.randomUUID() + ".jpg"; + YunXiaoVideoUtil.getCover(url, tmpFile); + File imgFile = new File(tmpFile); + render(new ImageRender(imgFile)); + } } diff --git a/src/main/java/com/dsideal/QingLong/YunXiao/Util/YunXiaoVideoUtil.java b/src/main/java/com/dsideal/QingLong/YunXiao/Util/YunXiaoVideoUtil.java index 39a4a7c3..6701f939 100644 --- a/src/main/java/com/dsideal/QingLong/YunXiao/Util/YunXiaoVideoUtil.java +++ b/src/main/java/com/dsideal/QingLong/YunXiao/Util/YunXiaoVideoUtil.java @@ -2,11 +2,15 @@ package com.dsideal.QingLong.YunXiao.Util; import cn.hutool.core.util.RuntimeUtil; +import java.io.File; + public class YunXiaoVideoUtil { //MP4的片头长10秒 public static final int MP4 = 10; //MU38的片头长7秒 - public static final int M3U8 = 16; + public static final int M3U8 = 5; + //ffmpeg路径 + public static final String FFMPEG_PATH = "D:\\ffmpeg\\ffmpeg.exe"; /** * 获取视频封面 @@ -16,23 +20,48 @@ public class YunXiaoVideoUtil { * @param videoOutputPath * @return */ - private static String getCmd(String ffmpegPath, int seconds, String videoInputPath, String videoOutputPath) { + private static String getCmdMp4(String ffmpegPath, int seconds, String videoInputPath, String videoOutputPath) { return ffmpegPath + " -ss " + seconds + " -i " + videoInputPath + " -frames:v 1 -q:v 2 -s 1280x720 " + videoOutputPath; } + /** + * 截取m3u8视频封面 + * + * @param ffmpegPath + * @param seconds + * @param videoInputPath + * @param videoOutputPath + * @return + */ + private static String getCmdM3u8(String ffmpegPath, int seconds, String videoInputPath, String videoOutputPath) { + return ffmpegPath + " -i " + videoInputPath + " -ss " + seconds + " -vframes 1 -s 1280x720 " + videoOutputPath; + } + /** * 获取视频封面 * - * @param ffmpegPath ffmpeg路径 * @param videoInputPath 输入的视频路径 * @param videoOutputPath 输出的图片路径 * @return */ - public static String getCmd(String ffmpegPath, String videoInputPath, String videoOutputPath) { - if (videoInputPath.toLowerCase().equals(".m3u8")) { - return getCmd(ffmpegPath, M3U8, videoInputPath, videoOutputPath); + public static String getCmd(String videoInputPath, String videoOutputPath) { + if (videoInputPath.toLowerCase().endsWith(".m3u8")) { + return getCmdM3u8(FFMPEG_PATH, M3U8, videoInputPath, videoOutputPath); } - return getCmd(ffmpegPath, MP4, videoInputPath, videoOutputPath); + return getCmdMp4(FFMPEG_PATH, MP4, videoInputPath, videoOutputPath); + } + + /** + * 获取视频封面 + * @param url + * @param JpegPath + */ + public static void getCover(String url, String JpegPath) { + String cmd = getCmd(url, JpegPath); + //使用java调用ffmpeg命令行工具生成图片 + RuntimeUtil.exec(cmd); + + System.out.println(JpegPath); } public static void main(String[] args) { @@ -43,9 +72,9 @@ public class YunXiaoVideoUtil { "https://ccmsyk-video.edusoa.com/down/M3u8/9A/9A69A401-BF92-433D-AB96-7B9965BDDD87.m3u8" }; for (int i = 0; i < a.length; i++) { - String cmd = getCmd("D:\\ffmpeg\\ffmpeg.exe", a[i], "c:\\" + (i + 1) + ".jpg"); - //使用java调用ffmpeg命令行工具生成图片 - RuntimeUtil.exec(cmd); + File f = new File("c:\\" + (i + 1) + ".jpg"); + if (f.exists()) f.delete(); + getCover(a[i], f.getAbsolutePath()); } } }