From ee812fc4c53369562830c5d6df5b1c44bcde68f0 Mon Sep 17 00:00:00 2001 From: wanggeng888 <450292408@qq.com> Date: Mon, 14 Jun 2021 18:45:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A7=86=E9=A2=91=E8=BD=AC?= =?UTF-8?q?=E7=A0=81=E3=80=81=E8=A7=86=E9=A2=91=E9=A2=84=E8=A7=88=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/api/video/VideoController.java | 11 +- .../route/video/VideoRouteController.java | 40 ++++- .../media/manager/process/IMediaStream.java | 12 ++ .../media/service/impl/MediaServiceImpl.java | 60 +++++++- .../media/service/video/IVideoService.java | 12 ++ .../service/video/impl/VideoServiceImpl.java | 105 +++++++++++-- .../task/transcoding/VideoConvertManager.java | 31 ++-- .../runnable/VideoConvertRunnable.java | 21 +-- .../mybatis/mapper/media-video-mapper.xml | 5 +- .../templates/file/media/video/list.html | 139 +++++++++--------- .../templates/file/media/video/preview.html | 29 ++++ .../templates/file/media/video/upload.html | 2 +- .../src/test/java/MediaTest.java | 22 +-- 13 files changed, 364 insertions(+), 125 deletions(-) create mode 100644 module-file-media/src/main/resources/templates/file/media/video/preview.html diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/controller/api/video/VideoController.java b/module-file-media/src/main/java/ink/wgink/module/file/media/controller/api/video/VideoController.java index 087e703b..23c66a55 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/controller/api/video/VideoController.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/controller/api/video/VideoController.java @@ -61,14 +61,9 @@ public class VideoController extends DefaultBaseController { @ApiImplicitParam(name = "ids", value = "ID列表,用下划线分隔", paramType = "path", example = "1_2_3") }) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) - @DeleteMapping("remove/{isRemoveSource}/{ids}") - public synchronized SuccessResult remove(@PathVariable() Integer isRemoveSource, @PathVariable("ids") String ids) { - if (isRemoveSource == 0) { - videoService.remove(Arrays.asList(ids.split("\\_"))); - } - if (isRemoveSource == 1) { - videoService.delete(Arrays.asList(ids.split("\\_"))); - } + @DeleteMapping("delete/{ids}") + public synchronized SuccessResult delete(@PathVariable("ids") String ids) { + videoService.delete(Arrays.asList(ids.split("\\_"))); return new SuccessResult(); } diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/controller/route/video/VideoRouteController.java b/module-file-media/src/main/java/ink/wgink/module/file/media/controller/route/video/VideoRouteController.java index eb4ddc3c..1a3d3531 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/controller/route/video/VideoRouteController.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/controller/route/video/VideoRouteController.java @@ -1,14 +1,23 @@ package ink.wgink.module.file.media.controller.route.video; +import ink.wgink.exceptions.ParamsException; import ink.wgink.interfaces.consts.ISystemConstant; +import ink.wgink.module.file.media.service.video.IVideoService; +import ink.wgink.pojo.result.ErrorResult; import ink.wgink.properties.media.video.VideoProperties; -import io.swagger.annotations.Api; +import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; +import javax.servlet.AsyncContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.concurrent.CompletableFuture; + /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -19,13 +28,15 @@ import org.springframework.web.servlet.ModelAndView; * @Date: 2021/6/12 11:16 下午 * @Version: 1.0 */ -@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文件管理接口") +@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "视频管理接口") @Controller @RequestMapping(ISystemConstant.ROUTE_PREFIX + "/file/media/video") public class VideoRouteController { @Autowired private VideoProperties videoProperties; + @Autowired + private IVideoService videoService; @GetMapping("list") public ModelAndView list() { @@ -40,4 +51,29 @@ public class VideoRouteController { return modelAndView; } + @GetMapping("preview") + public ModelAndView preview() { + return new ModelAndView("file/media/video/preview"); + } + + @ApiOperation(value = "下载视频", notes = "下载视频接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "isOpen", value = "是否打开,true和false", paramType = "path"), + @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path") + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping(value = "download/{isOpen}/{fileId}", produces = "video/mp4") + public void downLoad(HttpServletRequest request, + HttpServletResponse response, + @PathVariable("isOpen") Boolean isOpen, + @PathVariable("fileId") String fileId) { + if (isOpen == null) { + throw new ParamsException("参数错误"); + } + if (fileId.indexOf(".") > 0) { + fileId = fileId.split("\\.")[0]; + } + videoService.downLoad(request, response, isOpen, fileId); + } + } diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/manager/process/IMediaStream.java b/module-file-media/src/main/java/ink/wgink/module/file/media/manager/process/IMediaStream.java index 301f2eb6..917c21ac 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/manager/process/IMediaStream.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/manager/process/IMediaStream.java @@ -17,8 +17,20 @@ import java.io.InputStream; */ public interface IMediaStream { + /** + * 输入 + * + * @param inputStream + * @throws Exception + */ void input(InputStream inputStream) throws Exception; + /** + * 错误 + * + * @param errorStream + * @throws Exception + */ void error(InputStream errorStream) throws Exception; } diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/service/impl/MediaServiceImpl.java b/module-file-media/src/main/java/ink/wgink/module/file/media/service/impl/MediaServiceImpl.java index 95a28f5f..433e4280 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/service/impl/MediaServiceImpl.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/service/impl/MediaServiceImpl.java @@ -59,7 +59,65 @@ public class MediaServiceImpl extends DefaultBaseService implements IMediaServic @Override public String getContentType(String fileType) { - return null; + String contentType; + switch (fileType) { + case "png": + contentType = "image/png"; + break; + case "blob": + case "jpg": + case "jpeg": + contentType = "image/jpeg"; + break; + case "gif": + contentType = "image/gif"; + break; + case "mp4": + contentType = "video/mp4"; + break; + case "rmvb": + contentType = "application/vnd.rn-realmedia-vbr"; + break; + case "mp3": + contentType = "audio/mp3"; + break; + case "wmv": + contentType = "video/x-ms-wmv"; + break; + case "wav": + contentType = "audio/wav"; + break; + case "doc": + contentType = "application/msword"; + break; + case "docx": + contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; + break; + case "xls": + contentType = "application/vnd.ms-excel"; + break; + case "xlsx": + contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + break; + case "ppt": + contentType = "application/vnd.ms-powerpoint"; + break; + case "pptx": + contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; + break; + case "txt": + contentType = "text/plain"; + break; + case "apk": + contentType = "application/vnd.android.package-archive"; + break; + case "pdf": + contentType = "application/pdf"; + break; + default: + contentType = "application/octet-stream"; + } + return contentType; } @Override diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/IVideoService.java b/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/IVideoService.java index ca4ba583..7eff26b1 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/IVideoService.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/IVideoService.java @@ -9,7 +9,10 @@ import ink.wgink.pojo.result.SuccessResultList; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -137,4 +140,13 @@ public interface IVideoService { */ SuccessResultList> listPage(ListPage page); + /** + * 文件下载 + * + * @param request + * @param response + * @param isOpen + * @param fileId + */ + void downLoad(HttpServletRequest request, HttpServletResponse response, Boolean isOpen, String fileId); } diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/impl/VideoServiceImpl.java b/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/impl/VideoServiceImpl.java index 87a455cc..0d3cec38 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/impl/VideoServiceImpl.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/service/video/impl/VideoServiceImpl.java @@ -18,18 +18,32 @@ import ink.wgink.module.file.media.service.video.IVideoService; import ink.wgink.module.file.media.task.transcoding.VideoConvertManager; import ink.wgink.module.file.media.task.transcoding.runnable.VideoConvertRunnable; import ink.wgink.pojo.ListPage; +import ink.wgink.pojo.pos.FilePO; import ink.wgink.pojo.result.SuccessResultList; import ink.wgink.properties.media.MediaProperties; import ink.wgink.util.UUIDUtil; import ink.wgink.util.date.DateUtil; import ink.wgink.util.map.HashMapUtil; +import org.apache.catalina.connector.ClientAbortException; import org.apache.commons.lang3.StringUtils; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.net.URLEncoder; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.WritableByteChannel; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -126,6 +140,7 @@ public class VideoServiceImpl extends DefaultBaseService implements IVideoServic params.put("fileFullPath", convertFile.getAbsolutePath()); VideoMetaInfo videoMetaInfo = MediaManager.getInstance().getVideoMetaInfo(convertFile); + System.out.println(videoMetaInfo); params.put("videoDuration", videoMetaInfo.getDuration()); params.put("videoWidth", videoMetaInfo.getWidth()); params.put("videoHeight", videoMetaInfo.getHeight()); @@ -173,7 +188,7 @@ public class VideoServiceImpl extends DefaultBaseService implements IVideoServic // 构建视频内容 VideoVO videoVO = new VideoVO(); - videoVO.setFileName(fileName); + videoVO.setFileName(fileName.split("\\.")[0]); videoVO.setFileFullPath(fileFullPath); videoVO.setFilePath(filePath); videoVO.setFileSize(fileSize); @@ -204,18 +219,17 @@ public class VideoServiceImpl extends DefaultBaseService implements IVideoServic @Override public SseEmitter getConvertProgress() { - String threadId = String.valueOf(Thread.currentThread().getId()); - // 30分钟超时 - SseEmitter sseEmitter = new SseEmitter(1800000L); + final String sseEmitterId = UUIDUtil.getUUID(); + SseEmitter sseEmitter = new SseEmitter(0L); sseEmitter.onTimeout(() -> { - VideoConvertManager.getInstance().removeConvertProgressSseEmitter(threadId); + VideoConvertManager.getInstance().removeConvertProgressSseEmitter(sseEmitterId); + LOG.debug("连接超时,移出队列"); }); sseEmitter.onCompletion(() -> { + VideoConvertManager.getInstance().removeConvertProgressSseEmitter(sseEmitterId); + LOG.debug("连接结束,移出队列"); }); - sseEmitter.onError(throwable -> { - throwable.printStackTrace(); - }); - VideoConvertManager.getInstance().addConvertProgressSseEmitter(threadId, sseEmitter); + VideoConvertManager.getInstance().addConvertProgressSseEmitter(sseEmitterId, sseEmitter); return sseEmitter; } @@ -251,6 +265,79 @@ public class VideoServiceImpl extends DefaultBaseService implements IVideoServic return new SuccessResultList<>(videoDTOs, pageInfo.getPageNum(), pageInfo.getTotal()); } + @Override + public void downLoad(HttpServletRequest request, HttpServletResponse response, Boolean isOpen, String fileId) { + VideoDTO videoDTO = get(fileId); + if (videoDTO == null) { + response.setStatus(HttpStatus.MOVED_PERMANENTLY.value()); + try { + response.sendRedirect("404.html"); + } catch (IOException e) { + throw new FileException("文件输出异常", e); + } + return; + } + try ( + RandomAccessFile randomAccessFile = new RandomAccessFile(videoDTO.getFileFullPath(), "r"); + FileChannel fileChannel = randomAccessFile.getChannel(); + OutputStream outputStream = response.getOutputStream(); + WritableByteChannel writableByteChannel = Channels.newChannel(outputStream); + ) { + response.setHeader("Content-Length", String.valueOf(videoDTO.getFileSize())); + response.setContentType(mediaService.getContentType(videoDTO.getFileType())); + if (!isOpen) { + response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(videoDTO.getFileName(), "UTF-8")); + } else { + response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(videoDTO.getFileName(), "UTF-8")); + } + String rangeString = request.getHeader("Range"); + LOG.debug("range: {}", rangeString); + long contentLength = Long.valueOf(videoDTO.getFileSize()); + long startRange = 0; + long endRange = contentLength - 1; + if (!StringUtils.isBlank(rangeString)) { + if (!isOpen) { + response.setContentType("multipart/byteranges"); + } + String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-"); + startRange = Long.valueOf(rangeArray[0]); + if (rangeArray.length > 1) { + endRange = Long.valueOf(rangeArray[1]); + } + setRangeHeader(startRange, endRange, response, videoDTO.getFileId(), contentLength); + randomAccessFile.seek(startRange); + } + LOG.debug("startRange: {}, endRange: {}", startRange, endRange); + long totalOutputLength = endRange - startRange + 1; + fileChannel.transferTo(startRange, totalOutputLength, writableByteChannel); + outputStream.flush(); + } catch (Exception e) { + if (e instanceof ClientAbortException) { + LOG.debug("客户端断开连接"); + } else { + throw new FileException("文件输出异常", e); + } + } + } + + /** + * 处理视频流问题 + * + * @param startRange + * @param endRange + * @param response + * @param fileId + * @param contentLength + */ + private void setRangeHeader(long startRange, long endRange, HttpServletResponse response, String fileId, long contentLength) { + // 这里不设置,会出现第一次加载很慢的情况 + response.setHeader("Content-Length", String.valueOf(endRange - startRange + 1)); + response.setHeader("Content-Range", String.format("bytes %d-%d/%d", startRange, endRange, contentLength)); + response.setHeader("Accept-Ranges", "bytes"); + response.setHeader("Etag", fileId); + response.setStatus(206); + } + /** * 检查转换中状态 */ diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/VideoConvertManager.java b/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/VideoConvertManager.java index fe2e2676..66878318 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/VideoConvertManager.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/VideoConvertManager.java @@ -2,8 +2,11 @@ package ink.wgink.module.file.media.task.transcoding; import ink.wgink.module.file.media.pojo.dtos.ConvertProgressDTO; import ink.wgink.module.file.media.task.transcoding.runnable.VideoConvertRunnable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import java.io.IOException; import java.util.Map; import java.util.concurrent.*; @@ -18,7 +21,7 @@ import java.util.concurrent.*; * @Version: 1.0 */ public class VideoConvertManager { - + private static final Logger LOG = LoggerFactory.getLogger(VideoConvertManager.class); private static VideoConvertManager VIDEO_TRANS_CODING_MANAGER = VideoTransCodingManagerBuilder.videoConvertManager; private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10, 30L, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); private Map convertProgressSseEmitterMap = new ConcurrentHashMap<>(); @@ -34,24 +37,34 @@ public class VideoConvertManager { threadPoolExecutor.execute(videoConvertRunnable); } - public void addConvertProgressSseEmitter(String threadId, SseEmitter sseEmitter) { - convertProgressSseEmitterMap.put(threadId, sseEmitter); + public void addConvertProgressSseEmitter(String sseEmitterId, SseEmitter sseEmitter) { + convertProgressSseEmitterMap.put(sseEmitterId, sseEmitter); + // 发送空对象 + ConvertProgressDTO convertProgressDTO = new ConvertProgressDTO(); + convertProgressDTO.setFileId(""); + convertProgressDTO.setPercent(0D); + try { + sseEmitter.send(convertProgressDTO); + } catch (Throwable e) { + e.printStackTrace(); + } } - public void removeConvertProgressSseEmitter(String threadId) { - convertProgressSseEmitterMap.remove(threadId); + public void removeConvertProgressSseEmitter(String sseEmitterId) { + convertProgressSseEmitterMap.remove(sseEmitterId); } public void convertProgressSseEmitterNotice(String fileId, Double percent) { ConvertProgressDTO convertProgressDTO = new ConvertProgressDTO(); convertProgressDTO.setFileId(fileId); convertProgressDTO.setPercent(percent); - try { - for (Map.Entry kv : convertProgressSseEmitterMap.entrySet()) { + LOG.debug("视频ID {} 转码进度:{}%", fileId, percent); + for (Map.Entry kv : convertProgressSseEmitterMap.entrySet()) { + try { kv.getValue().send(convertProgressDTO); + } catch (Throwable e) { + e.printStackTrace(); } - } catch (Exception e) { - e.printStackTrace(); } } diff --git a/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/runnable/VideoConvertRunnable.java b/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/runnable/VideoConvertRunnable.java index 5b86524c..7999b686 100644 --- a/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/runnable/VideoConvertRunnable.java +++ b/module-file-media/src/main/java/ink/wgink/module/file/media/task/transcoding/runnable/VideoConvertRunnable.java @@ -85,30 +85,17 @@ public class VideoConvertRunnable implements Runnable { if (fullTime > 0L) { convertPercent = new BigDecimal((currentTime / (fullTime * 1D) * 100D)).setScale(2, RoundingMode.HALF_UP).doubleValue(); } - if (convertPercent > 0D && convertPercent < 100D && (int) convertPercent % 5 == 0) { + if (convertPercent > 0D && convertPercent < 100D) { VideoConvertManager.getInstance().convertProgressSseEmitterNotice(fileId, convertPercent); } - if (convertPercent >= 100D) { - updateConvertStatus(); - } } + VideoConvertManager.getInstance().convertProgressSseEmitterNotice(fileId, convertPercent); + LOG.debug("转码结束,更新状态"); + videoService.updateConvert(fileId, outFile); } }); } - /** - * 更新转换状态 - */ - private void updateConvertStatus() { - if (isUpdateConvertStatus) { - return; - } - VideoConvertManager.getInstance().convertProgressSseEmitterNotice(fileId, convertPercent); - isUpdateConvertStatus = true; - LOG.debug("转码结束,更新状态"); - videoService.updateConvert(fileId, outFile); - } - /** * 视频时长 * diff --git a/module-file-media/src/main/resources/mybatis/mapper/media-video-mapper.xml b/module-file-media/src/main/resources/mybatis/mapper/media-video-mapper.xml index eb12ea82..593fa439 100644 --- a/module-file-media/src/main/resources/mybatis/mapper/media-video-mapper.xml +++ b/module-file-media/src/main/resources/mybatis/mapper/media-video-mapper.xml @@ -236,11 +236,14 @@ FROM media_video + is_delete = 0 + AND file_id = #{fileId} - AND file_md5 = #{fileMd5} + AND + file_md5 = #{fileMd5} diff --git a/module-file-media/src/main/resources/templates/file/media/video/list.html b/module-file-media/src/main/resources/templates/file/media/video/list.html index 1bcc4557..81ef3069 100644 --- a/module-file-media/src/main/resources/templates/file/media/video/list.html +++ b/module-file-media/src/main/resources/templates/file/media/video/list.html @@ -9,6 +9,10 @@ +
@@ -41,45 +45,29 @@ 删除
- 视频在线播放只支持MP4格式,格式不符点击"转码状态"列的"转码"按钮进行转码 + 视频在线播放只支持MP4格式,格式不符合或不能播放,请点击"转码状态"列的"开始转码"按钮进行转码 - - diff --git a/module-file-media/src/main/resources/templates/file/media/video/preview.html b/module-file-media/src/main/resources/templates/file/media/video/preview.html new file mode 100644 index 00000000..b40e9656 --- /dev/null +++ b/module-file-media/src/main/resources/templates/file/media/video/preview.html @@ -0,0 +1,29 @@ + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/module-file-media/src/main/resources/templates/file/media/video/upload.html b/module-file-media/src/main/resources/templates/file/media/video/upload.html index 898db1a3..6dab762f 100644 --- a/module-file-media/src/main/resources/templates/file/media/video/upload.html +++ b/module-file-media/src/main/resources/templates/file/media/video/upload.html @@ -57,7 +57,7 @@ var id = data.response; uploadFileArray.push(id); top.dialog.dialogData.uploadFileArray = uploadFileArray; - if(uploadFileArray.length >= maxFileCount) { + if(uploadFileArray.length >= $('#maxUploadCount').val()) { $('.btn-file').hide(); } }).on('filesuccessremove', function(event, previewId, index) { diff --git a/module-file-media/src/test/java/MediaTest.java b/module-file-media/src/test/java/MediaTest.java index b652fbdf..09841e17 100644 --- a/module-file-media/src/test/java/MediaTest.java +++ b/module-file-media/src/test/java/MediaTest.java @@ -54,9 +54,9 @@ public class MediaTest { @Test public void t3() { - MediaManager.getInstance().setFFmpegPath("D:\\ffmpeg-4.4-full_build\\ffmpeg-4.4-full_build\\bin\\ffmpeg.exe"); - String sourcePath = "C:\\Users\\wenc0\\Desktop\\UploadFiles"; - String sourceName = "超体.rmvb"; + MediaManager.getInstance().setFFmpegPath("/Users/wanggeng/ffmpeg/ffmpeg"); + String sourcePath = "/Users/wanggeng/Desktop/UploadFiles"; + String sourceName = "720P_4000K_302139672.mp4"; File sourceFile = new File(sourcePath + File.separator + sourceName); File outFile = new File(sourcePath + File.separator + sourceName + ".mp4"); @@ -76,27 +76,29 @@ public class MediaTest { public void error(InputStream errorStream) throws Exception { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(errorStream)); for (String line; (line = bufferedReader.readLine()) != null; ) { + System.out.println(line); Matcher durationMatcher = durationPattern.matcher(line); Matcher timeMatcher = timePattern.matcher(line); if (durationMatcher.find()) { String duration = durationMatcher.group(); - System.out.println(duration); +// System.out.println(duration); String durationTime = duration.replace("Duration: ", ""); fullTime = durationToLongTime(durationTime); } if (timeMatcher.find()) { String time = timeMatcher.group(); - System.out.println(time); +// System.out.println(time); String timeTime = time.replace("time=", ""); currentTime = durationToLongTime(timeTime); } - System.out.println(fullTime + "-" + currentTime); - if (fullTime > 0L) { - - System.out.println((currentTime / (fullTime * 1D) * 100D) + "%"); - } +// System.out.println(fullTime + "-" + currentTime); +// if (fullTime > 0L) { +// System.out.println((currentTime / (fullTime * 1D) * 100D) + "%"); +// } } + System.out.println("123123"); } + }); }