作者:米饭2502912051 | 来源:互联网 | 2023-06-22 18:35
背景后台(Spring boot)获取rtmp流地址后返回前端(Angular2+)Ckplay插件上播放,由于需要展示多个暂停的媒体窗口封面,前端抓取封面较为困难,后台处理图片返回,于此收录本文以便日后再用。
安装好的FFmpeg,rtmp测试地址:rtmp://58.200.131.2:1935/livetv/hunantv
代码如下:
import org. springframework. beans. factory. annotation. Value; import java. io. *; public class VideoTool { @Value ( "${rtmp.imge.tool}" ) private String rtmpTool = "D:/TOOL/ffmpeg/bin/ffmpeg.exe" ; @Value ( "${rtmp.imge.url}" ) private String rtmpUrl = "C:/app/source/rtmpImge" ; public void getThumb ( String videoFilename, String thumbFilename, int width, int height, int hour, int min, float sec) throws IOException, InterruptedException { File file = new File ( rtmpUrl) ; if ( ! file. exists ( ) ) { file. mkdirs ( ) ; } ProcessBuilder processBuilder = new ProcessBuilder ( rtmpTool, "-y" , "-i" , videoFilename, "-vframes" , "1" , "-ss" , hour + ":" + min+ ":" + sec, "-f" , "mjpeg" , "-s" , width + "*" + height, "-an" , rtmpUrl. concat ( File. separator) . concat ( thumbFilename) ) ; Process process = processBuilder. start ( ) ; InputStream stderr = process. getErrorStream ( ) ; InputStreamReader isr = new InputStreamReader ( stderr) ; BufferedReader br = new BufferedReader ( isr) ; String line; while ( ( line = br. readLine ( ) ) != null) ; process. waitFor ( ) ; if ( br != null) br. close ( ) ; if ( isr != null) isr. close ( ) ; if ( stderr != null) stderr. close ( ) ; } public static void main ( String[ ] args) { VideoTool videoTool = new VideoTool ( ) ; try { videoTool. getThumb ( "rtmp://58.200.131.2:1935/livetv/hunantv" , "ffmpeg3.png" , 204 , 140 , 0 , 0 , 0 ) ; System. out. println ( "over" ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } } }
执行测试结果: