springboot 定时任务下载video
定时任务配置
@Component
@EnableScheduling
public class DownloadYoutubeSchedule {
@Autowired
DownloadVideo downloadVideo;
/**
* 定时下载
*/
@Scheduled(fixedDelay = 1000 * 60 * 30)
public void ytdl() {
try {
downloadVideo.exec();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 定时更新
*/
@Scheduled(fixedDelay = 1000 * 60 * 60 * 24)
public void ytUpdate() {
try {
downloadVideo.dlUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
下载脚本
private final static String checkCmdpre = "youtube-dl --get-id --playlist-end 1 ";
private final static String downloadMp3Cmdpre = "youtube-dl -o /opt/download/%(playlist)s/%(title)s.%(ext)s -x --audio-format mp3 --audio-quality 0 --playlist-end 1 ";
private final static String downloadMp4Cmdpre = "youtube-dl -o /opt/download/%(playlist)s/%(title)s.%(ext)s --playlist-end 1 ";
java执行shell命令
package com.ytdl.util.shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class ShellUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ShellUtils.class);
private static final long THREAD_SLEEP_TIME = 10;
private static final int DEFAULT_WAIT_TIME = 20 * 60 * 1000;
public static ShellResult runShell(String cmd) {
String[] command = new String[]{"/bin/sh", "-c", cmd};
ShellResult result = null;
try {
Process process = Runtime.getRuntime().exec(command);
result = getProcessResult(process, DEFAULT_WAIT_TIME);
LOGGER.info("Command [{}] executed successfully.", cmd);
LOGGER.info(result.toString());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 获取命令执行结果
* @param process 子进程
* @param waitTime 指定超时时间
* @return 命令执行输出结果
*/
public static ShellResult getProcessResult(Process process, long waitTime) {
ShellResult cmdResult = new ShellResult();
boolean isTimeout = false;
long loopNumber = waitTime / THREAD_SLEEP_TIME;
long realLoopNumber = 0;
int exitValue = -1;
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
errorGobbler.start();
outputGobbler.start();
try {
while (true) {
try {
Thread.sleep(THREAD_SLEEP_TIME);
exitValue = process.exitValue();
break;
} catch (InterruptedException e) {
realLoopNumber++;
if (realLoopNumber >= loopNumber) {
isTimeout = true;
break;
}
}
}
errorGobbler.join();
outputGobbler.join();
if (isTimeout) {
cmdResult.setErrorCode(ShellResult.TIMEOUT);
return cmdResult;
}
cmdResult.setErrorCode(exitValue);
if (exitValue != ShellResult.SUCCESS) {
cmdResult.setDescription(errorGobbler.getOutput());
} else {
cmdResult.setDescription(outputGobbler.getOutput());
}
} catch (InterruptedException e) {
LOGGER.error("Get shell result error.");
cmdResult.setErrorCode(ShellResult.ERROR);
} finally {
CommonUtils.closeStream(process.getErrorStream());
CommonUtils.closeStream(process.getInputStream());
CommonUtils.closeStream(process.getOutputStream());
}
return cmdResult;
}
}