Process does not terminating in Spring Environment and Spring boot application is not responding

1 week ago 5
ARTICLE AD BOX

I have created an endpoint which gets audio and images bytes as request parts

@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())); }

There is a processVideo method call inside generateVideo method. In processVideo, ı have created Processbuilder object and pass a command that will run FFMPEG CLI application. But the postman doesnt even response. It just says sending request.

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); } }

What do I do wrong ?

Read Entire Article