@Component public class MediaProcessTask { private static final Logger LOGGER = LoggerFactory.getLogger ( MediaProcessTask. class ) ; / / ffmpeg绝对路径 @Value ( "${xc‐service‐manage‐media.ffmpeg‐path}" ) String ffmpeg_path; / / 上传文件根目录 @Value ( "${xc‐service‐manage‐media.upload‐location}" ) String serverPath; @Autowired MediaFileRepository mediaFileRepository; @RabbitListener ( queues = "${xc‐service‐manage‐media.mq.queue‐media‐processtask}" ) public void receiveMediaProcessTask ( String msg ) throws IOException { Map msgMap = JSON.parseObject ( msg , Map. class ) ; LOGGER.info ( "receive media process task msg :{} " , msgMap ) ; / / 解析消息 / / 媒资文件 id String mediaId = ( String ) msgMap. get ( "mediaId" ) ; / / 获取媒资文件信息 Optional < MediaFile > optional = mediaFileRepository.findById ( fileMd 5 ) ; if ( !optional.isPresent ( ) ) { return ; } MediaFile mediaFile = optional. get ( ) ; / / 媒资文件类型 String fileType = mediaFile.getFileType ( ) ; if ( fileType = = null || !fileType. equals ( "avi" ) ) { / / 目前只处理avi文件 mediaFile.setProcessStatus ( "303004" ) ; / / 处理状态为无需处理 mediaFileRepository. save ( mediaFile ) ; return ; } else { mediaFile.setProcessStatus ( "303001" ) ; / / 处理状态为未处理 mediaFileRepository. save ( mediaFile ) ; } / / 生成mp 4 String video_path = serverPath + mediaFile.getFilePath ( ) + mediaFile.getFileName ( ) ; String mp 4 _name = mediaFile.getFileId ( ) + ".mp4" ; String mp 4 folder_path = serverPath + mediaFile.getFilePath ( ) ; Mp 4 VideoUtil videoUtil = new Mp 4 VideoUtil ( ffmpeg_path , video_path , mp 4 _name , mp 4 folder_path ) ; String result = videoUtil.generateMp 4 ( ) ; if ( result = = null || ! result . equals ( "success" ) ) { / / 操作失败写入处理日志 mediaFile.setProcessStatus ( "303003" ) ; / / 处理状态为处理失败 MediaFileProcess_m 3 u 8 mediaFileProcess_m 3 u 8 = new MediaFileProcess_m 3 u 8 ( ) ; mediaFileProcess_m 3 u 8. setErrormsg ( result ) ; mediaFile.setMediaFileProcess_m 3 u 8 ( mediaFileProcess_m 3 u 8 ) ; mediaFileRepository. save ( mediaFile ) ; return ; } / / 生成m 3 u 8 video_path = serverPath + mediaFile.getFilePath ( ) + mp 4 _name; / / 此地址为mp 4 的地址 String m 3 u 8 _name = mediaFile.getFileId ( ) + ".m3u8" ; String m 3 u 8 folder_path = serverPath + mediaFile.getFilePath ( ) + "hls/" ; HlsVideoUtil hlsVideoUtil = new HlsVideoUtil ( ffmpeg_path , video_path , m 3 u 8 _name , m 3 u 8 folder_path ) ; result = hlsVideoUtil.generateM 3 u 8 ( ) ; if ( result = = null || ! result . equals ( "success" ) ) { / / 操作失败写入处理日志 mediaFile.setProcessStatus ( "303003" ) ; / / 处理状态为处理失败 MediaFileProcess_m 3 u 8 mediaFileProcess_m 3 u 8 = new MediaFileProcess_m 3 u 8 ( ) ; mediaFileProcess_m 3 u 8. setErrormsg ( result ) ; mediaFile.setMediaFileProcess_m 3 u 8 ( mediaFileProcess_m 3 u 8 ) ; mediaFileRepository. save ( mediaFile ) ; return ; } / / 获取m 3 u 8 列表 List < String > ts_list = hlsVideoUtil.get_ts_list ( ) ; / / 更新处理状态为成功 mediaFile.setProcessStatus ( "303002" ) ; / / 处理状态为处理成功 MediaFileProcess_m 3 u 8 mediaFileProcess_m 3 u 8 = new MediaFileProcess_m 3 u 8 ( ) ; mediaFileProcess_m 3 u 8. setTslist ( ts_list ) ; mediaFile.setMediaFileProcess_m 3 u 8 ( mediaFileProcess_m 3 u 8 ) ; / / m 3 u 8 文件 url mediaFile.setFileUrl ( mediaFile.getFilePath ( ) + "hls/" + m 3 u 8 _name ) ; mediaFileRepository. save ( mediaFile ) ; } } |