Skip to content

Commit

Permalink
修复 #21 支持没有音频的av下载
Browse files Browse the repository at this point in the history
  • Loading branch information
nICEnnnnnnnLee committed Jan 29, 2020
1 parent f6fd68b commit b4595a9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
10 changes: 9 additions & 1 deletion src/nicelee/bilibili/downloaders/impl/M4SDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ public boolean download(String url, String avId, int qn, int page) {
if(util.getStatus() == StatusEnum.STOP)
return false;
util.init();
if (util.download(links[1], audioName, header.getBiliWwwM4sHeaders(avId))) {
if(links.length == 1 || links[1].isEmpty()) {
convertingStatus = StatusEnum.PROCESSING;
boolean result = CmdUtil.convert(videoName, null, dstName);
if (result)
convertingStatus = StatusEnum.SUCCESS;
else
convertingStatus = StatusEnum.FAIL;
return result;
}else if (util.download(links[1], audioName, header.getBiliWwwM4sHeaders(avId))) {
// 如下载成功,统计数据后重置
sumSuccessDownloaded += util.getTotalFileSize();
util.reset();
Expand Down
8 changes: 5 additions & 3 deletions src/nicelee/bilibili/parsers/impl/AbstractBaseParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,13 @@ String getVideoM4sLink(String avId, String cid, int qn) {
}
}
// 获取音频链接(默认第一个)
JSONArray audios = jObj.getJSONObject("dash").getJSONArray("audio");
link.append(audios.getJSONObject(0).getString("baseUrl"));

JSONArray audios = jObj.getJSONObject("dash").optJSONArray("audio");
if(audios != null) {
link.append(audios.getJSONObject(0).getString("baseUrl"));
}
return link.toString();
} catch (Exception e) {
e.printStackTrace();
// 鉴于部分视频如 https://www.bilibili.com/video/av24145318 H5仍然是用的是Flash源,此处切为FLV
return parseUrlJArray(jObj.getJSONArray("durl"));
}
Expand Down
85 changes: 60 additions & 25 deletions src/nicelee/bilibili/util/CmdUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class CmdUtil {

public static String FFMPEG_PATH = "ffmpeg";

public static boolean run(String cmd[]) {
Process process = null;
try {
Expand All @@ -28,12 +28,12 @@ public static boolean run(String cmd[]) {
StreamManager outputStream = new StreamManager(process, process.getInputStream());
errorStream.start();
outputStream.start();
//System.out.println("此处堵塞, 直至process 执行完毕");
// System.out.println("此处堵塞, 直至process 执行完毕");
process.waitFor();
System.out.println("process 执行完毕");
return true;
} catch (Exception e) {
//e.printStackTrace();
// e.printStackTrace();
Logger.println(e.toString());
return false;
}
Expand Down Expand Up @@ -66,14 +66,39 @@ public static boolean convert(String videoName, String audioName, String dstName
}
return false;
}

/**
* 片段合并转码
* 音视频合并转码
*
* @param videoName
* @param audioName
* @param dstName
*/
public static boolean convert(String videoName, String dstName) {
String cmd[] = createConvertCmd(videoName, null, dstName);
File mp4File = new File(Global.savePath + dstName);
File video = new File(Global.savePath + videoName);
if (!mp4File.exists()) {
Logger.println("下载完毕, 正在运行转码程序...");
run(cmd);
if (mp4File.exists() && mp4File.length() > video.length() * 0.8) {
video.delete();
return true;
}
Logger.println("转码完毕");
} else {
Logger.println("下载完毕");
return true;
}
return false;
}

/**
* 片段合并转码
*
* @param dstName
* @param part
*/
public static boolean convert(String dstName, int part) {
String cmd[] = createConvertCmd(dstName, part);
File videoFile = new File(Global.savePath + dstName);
Expand Down Expand Up @@ -141,8 +166,8 @@ public static String[] createConvertCmd(String dstName, int part) {
} catch (Exception e) {
e.printStackTrace();
}
String cmd[] = { FFMPEG_PATH, "-f", "concat", "-safe", "0", "-i", Global.savePath + dstName + ".txt", "-c", "copy",
Global.savePath + dstName };
String cmd[] = { FFMPEG_PATH, "-f", "concat", "-safe", "0", "-i", Global.savePath + dstName + ".txt", "-c",
"copy", Global.savePath + dstName };
return cmd;
}

Expand Down Expand Up @@ -205,7 +230,7 @@ public boolean accept(File dir, String name) {
return false;
}
};
if(folderDown.exists()) {
if (folderDown.exists()) {
// 删除下载文件
for (File file : folderDown.listFiles(filter)) {
System.out.println("尝试删除" + file.getName());
Expand All @@ -223,12 +248,20 @@ public boolean accept(File dir, String name) {
* @return
*/
public static String[] createConvertCmd(String videoName, String audioName, String dstName) {
String cmd[] = { FFMPEG_PATH, "-i", Global.savePath + videoName, "-i", Global.savePath + audioName, "-c", "copy",
Global.savePath + dstName };
String str = String.format("ffmpeg命令为: \r\n%s -i %s -i %s -c copy %s", FFMPEG_PATH, Global.savePath + videoName,
Global.savePath + audioName, Global.savePath + dstName);
Logger.println(str);
return cmd;
if (audioName == null) {
String cmd[] = { FFMPEG_PATH, "-i", Global.savePath + videoName, "-c", "copy", Global.savePath + dstName };
String str = String.format("ffmpeg命令为: \r\n%s -i %s -c copy %s", FFMPEG_PATH, Global.savePath + videoName,
Global.savePath + dstName);
Logger.println(str);
return cmd;
} else {
String cmd[] = { FFMPEG_PATH, "-i", Global.savePath + videoName, "-i", Global.savePath + audioName, "-c",
"copy", Global.savePath + dstName };
String str = String.format("ffmpeg命令为: \r\n%s -i %s -i %s -c copy %s", FFMPEG_PATH,
Global.savePath + videoName, Global.savePath + audioName, Global.savePath + dstName);
Logger.println(str);
return cmd;
}
}

/**
Expand All @@ -240,6 +273,7 @@ public static String[] createConvertCmd(String videoName, String audioName, Stri
*/
// public static boolean doRenameAfterComplete = true;
final static Pattern suffixPattern = Pattern.compile("\\.[^.]+$");

public synchronized static void convertOrAppendCmdToRenameBat(final String avid_q, final String formattedTitle,
int page) {
try {
Expand All @@ -253,7 +287,7 @@ public synchronized static void convertOrAppendCmdToRenameBat(final String avid_
if (Global.doRenameAfterComplete) {
File file = new File(Global.savePath, formattedTitle + tail);
File folder = file.getParentFile();
if(!folder.exists())
if (!folder.exists())
folder.mkdirs();
originFile.renameTo(file);
} else {
Expand Down Expand Up @@ -316,11 +350,12 @@ public static File getFileByAvQnP(String avid_q, int page) {
// ## clipTitle - 视频小标题
//
// 以下可能不存在
// 用法举例 (:listName 我在前面-listName-我在后面) ===> 我在前面-某收藏夹的名称-我在后面
// ### listName - 集合名称 e.g. 某收藏夹的名称
// 用法举例 (:listName 我在前面-listName-我在后面) ===> 我在前面-某收藏夹的名称-我在后面
// ### listName - 集合名称 e.g. 某收藏夹的名称
// ### listOwnerName - 集合的拥有者 e.g. 某某某 (假设搜索的是某人的收藏夹)
// public static String formatStr = "avTitle-pDisplay-clipTitle-qn";
static Pattern splitUnit = Pattern.compile("avId|pAv|pDisplay|qn|avTitle|clipTitle|UpName|UpId|listName|listOwnerName|\\(\\:([^ ]+) ([^\\)]*)\\)");
static Pattern splitUnit = Pattern.compile(
"avId|pAv|pDisplay|qn|avTitle|clipTitle|UpName|UpId|listName|listOwnerName|\\(\\:([^ ]+) ([^\\)]*)\\)");

public static String genFormatedName(String avId, String pAv, String pDisplay, int qn, String avTitle,
String clipTitle, String listName, String listOwnerName) {
Expand All @@ -334,13 +369,13 @@ public static String genFormatedName(String avId, String pAv, String pDisplay, i
paramMap.put("clipTitle", clipTitle.replaceAll("[/\\\\]", "_"));
paramMap.put("listName", listName);
paramMap.put("listOwnerName", listOwnerName);
//paramMap.put("clipTitle", clipTitle);
// paramMap.put("clipTitle", clipTitle);

// 匹配格式字符串
// avTitle-pDisplay-clipTitle-qn
return genFormatedName(paramMap, Global.formatStr);
}

public static String genFormatedName(VideoInfo avInfo, ClipInfo clip, int realQN) {
// 生成KV表
HashMap<String, String> paramMap = new HashMap<String, String>();
Expand All @@ -354,7 +389,7 @@ public static String genFormatedName(VideoInfo avInfo, ClipInfo clip, int realQN
paramMap.put("listOwnerName", clip.getListOwnerName()); // 已确保没有路径分隔符
paramMap.put("UpName", clip.getUpName().replaceAll("[/\\\\]", "_"));
paramMap.put("UpId", clip.getUpId());

// 匹配格式字符串
// avTitle-pDisplay-clipTitle-qn
return genFormatedName(paramMap, Global.formatStr);
Expand All @@ -372,13 +407,13 @@ private static String genFormatedName(HashMap<String, String> paramMap, String f
while (matcher.find()) {
// 加入匹配单位前的字符串
sb.append(formatStr.substring(pointer, matcher.start()));
String ifStr = matcher.group(1);//条件语句
if(ifStr != null) {
if(paramMap.get(ifStr)!= null) {
String ifStr = matcher.group(1);// 条件语句
if (ifStr != null) {
if (paramMap.get(ifStr) != null) {
sb.append(genFormatedName(paramMap, matcher.group(2)));
}
// Logger.println();
}else {
} else {
// 加入匹配单位对应的值
sb.append(paramMap.get(matcher.group()));
}
Expand Down

0 comments on commit b4595a9

Please sign in to comment.