Код: Выделить всё
@PostMapping(value = "/videos",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = "video/mp4")
public ResponseEntity getVideo(
@RequestPart MultipartFile audio,
@RequestPart MultipartFile image
) throws IOException {
return ResponseEntity.status(HttpStatus.OK)
.contentType(MediaType.valueOf("video/mp4"))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"news.mp4\"")
.body(newsService.generateVideo(audio.getBytes(),image.getBytes()));
}
Код: Выделить всё
public static byte[] processVideo(byte[] audioByte, byte[] imageByte) {
try {
File tempImage = File.createTempFile("news_image", ".png");
File tempAudio = File.createTempFile("news_audio", ".mp3");
File tempVideo = File.createTempFile("news_video", ".mp4");
Files.write(tempImage.toPath(), imageByte);
Files.write(tempAudio.toPath(), audioByte);
ProcessBuilder pb = new ProcessBuilder(
"ffmpeg",
"-y",
"-loop", "1",
"-i", tempImage.getAbsolutePath(),
"-i", tempAudio.getAbsolutePath(),
"-c:v", "libx264",
"-tune", "stillimage",
"-c:a", "aac",
"-b:a", "192k",
"-pix_fmt", "yuv420p",
"-shortest",
tempVideo.getAbsolutePath()
);
pb.redirectErrorStream(true); // stderr + stdout getting together
Process process = pb.start();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("[FFMPEG] " + line);
}
}
int exitCode = process.waitFor();
System.out.println("FFmpeg finished with code: " + exitCode);
byte[] videoBytes = Files.readAllBytes(tempVideo.toPath());
tempImage.delete();
tempAudio.delete();
tempVideo.delete();
return videoBytes;
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Video creation failed", ex);
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... ication-is