package com.ruoyi.inherit.apicontroller;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.inherit.utils.FfmpegUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
@RestController
@RequestMapping("/api/ffmpeg")
public class MmFfmpegController extends BaseController {
@GetMapping(value = "/getVideoCover")
public void getVideoCover(String videoUrl, Integer second, HttpServletResponse response) {
String videoCover = FfmpegUtil.getVideoCover(videoUrl, second);
File file = new File(videoCover);
// 设置响应的 MIME 类型
response.setContentType("image/png"); // 根据图片格式设置 MIME 类型
// 获取文件的输入流
try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) {
// 将文件内容写入响应流
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.ruoyi.inherit.utils;
import com.ruoyi.common.core.utils.uuid.UUID;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class FfmpegUtil {
//win
public static String ffmpegPath = "I:\\ffmpeg-7.1-full_build\\bin\\ffmpeg.exe";
//liunx
//public static String ffmpegPath = "/opt/ffmpeg/bin/ffmpeg";
/**
* 获取视频帧作为前端的封面
* @param videoUrl
* @param second
* @return
*/
public static String getVideoCover(String videoUrl,Integer second){
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString().replace("-", "");
String tempDir = System.getProperty("java.io.tmpdir");
String out=tempDir+uuidString+".jpg";
try {
Process process = Runtime.getRuntime().exec(ffmpegPath+" -i "+videoUrl+" -ss "+second+" -vframes 1 "+out);
// 创建线程读取输出流
new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Output: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
// 创建线程读取错误流
new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Error: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
// 等待命令执行完成
int exitCode = process.waitFor(); // 阻塞当前线程,直到命令执行完成
} catch (IOException e) {
throw new RuntimeException(e);
}catch (InterruptedException e) {
throw new RuntimeException(e);
}
return out;
}
public static void main(String[] args) {
String videoCover = getVideoCover("https://mmwz.obs.cn-east-3.myhuaweicloud.com/bbb.mp4", 10);
File file = new File(videoCover);
System.out.println(file.getAbsolutePath());
System.out.println(file.length());
}
}