From 41d3dec536bd7c96bfd2012fffd6a83cc31017e4 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Fri, 22 Nov 2024 17:27:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4FFmpeg=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yutou/bilibili/services/LiveService.java | 3 ++ .../services/LiveVideoDownloadService.java | 30 ++++++++++++------- .../com/yutou/common/utils/FFmpegUtils.java | 3 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/yutou/bilibili/services/LiveService.java b/src/main/java/com/yutou/bilibili/services/LiveService.java index d945ee2..89f3b29 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveService.java @@ -120,6 +120,9 @@ public class LiveService { long videoTime = FFmpegUtils.getVideoTime(videoFile); long startTime = Long.parseLong(videoId); long endTime = Long.parseLong(videoId) + videoTime; + if(endTime==0){ + endTime=System.currentTimeMillis(); + } return database.getGiftInfo(startTime, endTime); } diff --git a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java index 14df37c..a8ffc0f 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java @@ -177,13 +177,13 @@ public class LiveVideoDownloadService { database = new BiliLiveDatabase(config); try { var keyframe = roomInfo.getKeyframe(); - if(!StringUtils.hasText(keyframe)){ - keyframe= roomInfo.getUserCover(); + if (!StringUtils.hasText(keyframe)) { + keyframe = roomInfo.getUserCover(); } HttpDownloadUtils.download(new HttpDownloadUtils.Builder().setUrl(keyframe) .setPath(rootPath.getAbsolutePath()) .setFileName("poster.jpg")); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } saveLiveInfo(roomInfo); @@ -257,8 +257,16 @@ public class LiveVideoDownloadService { // .withNotSymbolParam("-progress", "-") .withNotSymbolParam("-threads", "8") .withNotSymbolParam("-c:v", "copy") - .withNotSymbolParam("-fflags","+genpts") - .withNotSymbolParam("-y", ""); + .withNotSymbolParam("-fflags", "+genpts") + .withNotSymbolParam("-y", "") + //-reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 + .withNotSymbolParam("-reconnect", "1") + .withNotSymbolParam("-reconnect_at_eof", "1") + .withNotSymbolParam("-reconnect_streamed", "1") + .withNotSymbolParam("-reconnect_delay_max", "2") + .withNotSymbolParam("-loglevel", "error") +// .withNotSymbolParam("-progress",new File("cache",config.getRoomId()+".txt").getAbsolutePath()); //输出进度日志,暂时没啥用 + ; if (ck != null) { builder = builder.withParam("-cookies", cookie); } @@ -300,7 +308,7 @@ public class LiveVideoDownloadService { videoDatabaseBean.setRoomInfoJson(JSONObject.toJSONString(roomInfo)); videoDatabaseBean.setStartTime(new Date()); database.addLiveInfo(videoDatabaseBean); - // LiveInfoNfoTools.saveLiveInfoNfo(roomInfo, rootPath.getAbsolutePath(), new File(savePath).getName().replace(".flv", ".nfo")); + // LiveInfoNfoTools.saveLiveInfoNfo(roomInfo, rootPath.getAbsolutePath(), new File(savePath).getName().replace(".flv", ".nfo")); } } @@ -376,12 +384,12 @@ public class LiveVideoDownloadService { } } liveDatabase.close(); - if(videoInfo != null) { + if (videoInfo != null) { File videoFile = new File(videoInfo.getPath().replace(".flv", ".mp4")); - if(!videoFile.exists()){ - videoFile=new File(videoInfo.getPath()); + if (!videoFile.exists()) { + videoFile = new File(videoInfo.getPath()); } - FFmpegUtils ffmpeg = FFmpegUtils.segment(videoId, ffmpegPath,videoFile , ConfigTools.load(ConfigTools.CONFIG, "outVideoPath", String.class)); + FFmpegUtils ffmpeg = FFmpegUtils.segment(videoId, ffmpegPath, videoFile, ConfigTools.load(ConfigTools.CONFIG, "outVideoPath", String.class)); System.out.println(ffmpeg.getCommand()); ffmpeg.start(new DownloadInterface() { @Override @@ -396,7 +404,7 @@ public class LiveVideoDownloadService { } public static void main(String[] args) { - LiveVideoDownloadService service=new LiveVideoDownloadService(); + LiveVideoDownloadService service = new LiveVideoDownloadService(); String play = service.getVideoPlay("17961", "1730363029293"); System.out.println(play); } diff --git a/src/main/java/com/yutou/common/utils/FFmpegUtils.java b/src/main/java/com/yutou/common/utils/FFmpegUtils.java index 6bac42d..74c1f6a 100644 --- a/src/main/java/com/yutou/common/utils/FFmpegUtils.java +++ b/src/main/java/com/yutou/common/utils/FFmpegUtils.java @@ -155,6 +155,7 @@ public class FFmpegUtils extends AbsVideoRecord { * @param video 视频文件 * @return 毫秒 */ + @Deprecated public static long getVideoTime(File video) { String ffprobe = ConfigTools.load(ConfigTools.CONFIG, "ffprobe", String.class); String exec = ffprobe + " -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"" + video.getAbsolutePath() + "\""; @@ -172,7 +173,7 @@ public class FFmpegUtils extends AbsVideoRecord { return (long) (Double.parseDouble(data) * 1000); } catch (Exception e) { Log.e(e); - throw new RuntimeException(e); + return 0; } }