Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed Feb 9, 2020
1 parent 11bcc83 commit df24a47
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>top.itning.yunshu</groupId>
<artifactId>yunshu-nas</artifactId>
<version>1.1.2.RELEASE</version>
<version>1.1.3.RELEASE</version>
<name>yunshu-nas</name>
<description>Yun Shu Nas Project</description>
<url>https://github.com/itning/yunshu-nas</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public Video2M3u8Helper(String ffmpegBinDir) {
* 进度条
*/
public interface Progress {
/**
* 视频准备开始转码时回调
*
* @param fromFile 源文件
* @param toPath 目标路径
* @param fileName 文件名(不要扩展名)
*/
default void onStart(String fromFile, String toPath, String fileName) {

}

/**
* 日志
*
Expand Down Expand Up @@ -120,7 +131,7 @@ default void onProgress(long frame, long totalFrames, String percentage, String
*/
private String videoStandardization(String fromFile, String toPath, boolean copyVideo, boolean copyAudio) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("start copy {} {} {} {}", fromFile, toPath, copyAudio, copyAudio);
logger.debug("3/4. start copy {} {} {} {}", fromFile, toPath, copyAudio, copyAudio);
}
final long videoFrames = getVideoFrames(fromFile);
String randomFileName = DigestUtils.md5DigestAsHex(fromFile.getBytes()) + ".mp4";
Expand Down Expand Up @@ -149,7 +160,7 @@ private String videoStandardization(String fromFile, String toPath, boolean copy
}
});
if (logger.isDebugEnabled()) {
logger.debug("end copy {} {} {} {}", fromFile, toPath, copyAudio, copyAudio);
logger.debug("4/4. end copy {} {} {} {}", fromFile, toPath, copyAudio, copyAudio);
}
return command.get(command.size() - 1);
}
Expand All @@ -168,6 +179,9 @@ public void videoConvert(final String fromFile, final String toPath, final Strin
}
this.progress = progress;
try {
if (progress != null) {
progress.onStart(fromFile, toPath, fileName);
}
Tuple2<Boolean, Boolean> compliance = checkComplianceWithSpecificationsForHls(fromFile);
if (logger.isDebugEnabled()) {
logger.debug("video: {} audio: {}", compliance.getT1(), compliance.getT2());
Expand Down Expand Up @@ -237,7 +251,7 @@ public void videoConvert(String fromFile, String toPath, String fileName) throws
*/
private Tuple2<Boolean, Boolean> checkComplianceWithSpecificationsForHls(String wantCheckVideoFile) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("start checkComplianceWithSpecificationsForHls {}", wantCheckVideoFile);
logger.debug("1/4. start checkComplianceWithSpecificationsForHls {}", wantCheckVideoFile);
}
List<String> command = new ArrayList<>(3);
command.add(ffmpegLocation);
Expand All @@ -249,7 +263,7 @@ private Tuple2<Boolean, Boolean> checkComplianceWithSpecificationsForHls(String
boolean video = s.contains(VIDEO_H_264);
boolean audio = s.contains(AUDIO_AAC);
if (logger.isDebugEnabled()) {
logger.debug("end checkComplianceWithSpecificationsForHls {}", wantCheckVideoFile);
logger.debug("2/4. end checkComplianceWithSpecificationsForHls {}", wantCheckVideoFile);
}
//音视频都是HLS规范
if (video && audio) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -22,13 +23,16 @@
public class VideoTransformHandler {
private static final Logger logger = LoggerFactory.getLogger(VideoTransformHandler.class);

private static final Object NULL_VALUE = new Object();

private final LinkedBlockingQueue<String> linkedBlockingQueue;

private final Video2M3u8Helper video2M3u8Helper;
private final ThreadPoolExecutor transformExecutorService;
private final ThreadPoolExecutor synchronousBlockingSingleService;
private final IVideoRepository iVideoRepository;
private final Video2M3u8Helper.Progress progress;
private final Map<String, Object> VIDEO_CURRENTLY_BEING_TRANSCODED = new ConcurrentHashMap<>();

public VideoTransformHandler(Video2M3u8Helper video2M3u8Helper, IVideoRepository iVideoRepository) {
this.video2M3u8Helper = video2M3u8Helper;
Expand All @@ -50,21 +54,33 @@ public VideoTransformHandler(Video2M3u8Helper video2M3u8Helper, IVideoRepository
new ThreadFactoryBuilder().setNameFormat("single-pool-%d").build());
this.linkedBlockingQueue = new LinkedBlockingQueue<>();
progress = new Video2M3u8Helper.Progress() {
@Override
public void onStart(String fromFile, String toPath, String fileName) {
VIDEO_CURRENTLY_BEING_TRANSCODED.put(fromFile, NULL_VALUE);
}

@Override
public void onLine(String line) {
ProgressWebSocket.sendMessage(line);
}

@Override
public void onFinish(String fromFile, String toPath, String fileName) {
VIDEO_CURRENTLY_BEING_TRANSCODED.remove(fromFile);
ProgressWebSocket.sendMessage(String.format("完成转换 文件:%s 目标路径:%s 文件名:%s", fromFile, toPath, fileName));
}

@Override
public void onError(Exception e, String fromFile, String toPath, String fileName) {
VIDEO_CURRENTLY_BEING_TRANSCODED.remove(fromFile);
ProgressWebSocket.sendMessage(String.format("Exception In Video Convert: %s %s %s", fromFile, toPath, fileName));
ProgressWebSocket.sendMessage(e.getMessage());
}

@Override
public void onProgress(long frame, long totalFrames, String percentage, String line) {
ProgressWebSocket.sendMessage(String.format("%d/%d %s", frame, totalFrames, percentage));
}
};
start();
}
Expand Down Expand Up @@ -93,12 +109,23 @@ private void start() {

public boolean put(String location) {
try {
if (VIDEO_CURRENTLY_BEING_TRANSCODED.containsKey(location)) {
if (logger.isDebugEnabled()) {
logger.debug("currently being transcoded {}", location);
}
return true;
}
if (linkedBlockingQueue.contains(location)) {
return false;
if (logger.isDebugEnabled()) {
logger.debug("already in queue {}", location);
}
return true;
}

File m3u8File = new File(iVideoRepository.getWriteDir(location) + File.separator + iVideoRepository.getLocationMd5(location) + ".m3u8");
if (m3u8File.exists()) {
if (logger.isDebugEnabled()) {
logger.debug("already exist m3u8 file {}", location);
}
return false;
}
linkedBlockingQueue.put(location);
Expand Down
47 changes: 33 additions & 14 deletions src/main/resources/templates/video.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
<source th:src="${'/hls/'+name+'.m3u8'}" type="application/x-mpegURL">
</video-js>
<button class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent mdui-btn-dense mdui-m-t-1 mdui-m-b-1"
onclick="del()">删除
onclick="del()">删除转码及视频
</button>
<button class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent mdui-btn-dense mdui-m-t-1 mdui-m-b-1"
onclick="delTranscoding()">删除转码
</button>
<button class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent mdui-btn-dense mdui-m-t-1 mdui-m-b-1"
onclick="copyVideoPath()">复制视频地址
</button>
<label class="mdui-switch">MSE资源(解决某些浏览器倍速失效)
<input type="checkbox" id="mse_checkbox"/>
<i class="mdui-switch-icon"></i>
</label>
<div class="mdui-table-fluid">
<table class="mdui-table mdui-table-hoverable">
<thead>
Expand Down Expand Up @@ -66,30 +70,45 @@
<script th:inline="javascript">
'use strict';
window.HELP_IMPROVE_VIDEOJS = false;
const IS_MSE_RESOURCE = "IS_MSE_RESOURCE";
const $$ = mdui.JQ;
const links = [[${links}]];
const name = [[${name}]];
const path = links[links.length - 1].link;

$$(function () {
if (!localStorage.getItem(IS_MSE_RESOURCE)) {
localStorage.setItem(IS_MSE_RESOURCE, "true");
}
const checkBox = document.getElementById("mse_checkbox");
localStorage.getItem(IS_MSE_RESOURCE) === "true" ? checkBox.checked = true : checkBox.checked = false;
checkBox.addEventListener("change", function (e) {
if (e.target.checked) {
localStorage.setItem(IS_MSE_RESOURCE, "true");
} else {
localStorage.setItem(IS_MSE_RESOURCE, "false");
}
window.location.reload();
});
setTimeout(function () {
window.scrollTo(0, 0);
}, 1000);
});
// overrideNative=true为MSE
videojs('video-id', {
playbackRates: [0.5, 1, 1.5, 2, 2.5, 3],
html5: {hls: {overrideNative: !videojs.browser.IS_SAFARI}}
})
.ready(function () {
this.hotkeys({
volumeStep: 0.1,
seekStep: 5,
enableModifiersForNumbers: false

// overrideNative=true为MSE
videojs('video-id', {
playbackRates: [0.5, 1, 1.5, 2, 2.5, 3],
html5: {hls: {overrideNative: localStorage.getItem(IS_MSE_RESOURCE) === "true"}}
})
.ready(function () {
this.hotkeys({
volumeStep: 0.1,
seekStep: 5,
enableModifiersForNumbers: false
});
});
});

//player.playbackRate(speed);
//player.playbackRate(speed);
});

function del() {
const r = window.confirm("确认删除?");
Expand Down

0 comments on commit df24a47

Please sign in to comment.