1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.yanzuoguang.media;
import com.yanzuoguang.util.MediaHelper;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.thread.ThreadHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
/**
* 直播首页操作
*
* @author 颜佐光
*/
@Component
public class MediaFirst {
/**
* 判断该视频是否已经实现处理
*/
@Autowired(required = false)
private MediaCache cache;
private final MediaCacheBase cacheLocal = new MediaCacheLocal();
public MediaFirst() {
}
private MediaCacheBase getCache() {
if (cache != null) {
return cache;
}
return cacheLocal;
}
public MediaFirst(MediaCache cache) {
this.cache = cache;
}
public MediaResVo start(MediaReqVo req) {
// 将临时文件移动到正式文件
this.getCache().lockTempFile(req, new Runnable() {
@Override
public void run() {
File file = getFile(req);
File fileTemp = getFileTemp(req);
FileHelper.createDirectory(file.getParentFile());
if (fileTemp.exists()) {
fileTemp.renameTo(file);
}
}
});
// 通过判断,当没有开启线程运行时,则开启线程运行
return this.getCache().start(req, new Runnable() {
@Override
public void run() {
// 开始开启线程运行
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
handle(req);
}
});
thread.start();
}
});
}
private File getFile(MediaReqVo req) {
return new File(req.getImageUrl());
}
private File getFileTemp(MediaReqVo req) {
return new File(req.getImageUrl() + ".tmp");
}
private File getDownM3muFile(MediaReqVo req) {
return new File(req.getImageUrl() + ".tmp.mp4");
}
private File getCatTempFile(MediaReqVo req) {
return new File(req.getImageUrl() + ".tmp.jpg");
}
private void handle(MediaReqVo req) {
do {
MediaResVo res = this.getCache().get(req);
if (res == null) {
res = JsonHelper.to(req, MediaResVo.class);
}
try {
// 下载m3mu并转换成mp4
downM3mu(req);
// 转换为图片
catImage(req);
// 将下载临时文件移动到临时文件
this.getCache().lockTempFile(req, new Runnable() {
@Override
public void run() {
File mp4 = getDownM3muFile(req);
File catTemp = getCatTempFile(req);
File fileTemp = getFileTemp(req);
if (catTemp.exists()) {
catTemp.renameTo(fileTemp);
}
mp4.delete();
}
});
// 写入缓存
this.getCache().sub(res);
// 判断是否还有执行次数
if (isFinish(req)) {
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
ThreadHelper.sleep(res.getSplit());
} while (!isFinish(req));
}
private void downM3mu(MediaReqVo req) throws Exception {
String localUrl = getDownM3muFile(req).getAbsolutePath();
HlsDownloader downloader = new HlsDownloader(req.getUrl(),
localUrl,
1,
1
);
downloader.download(false, true);
}
private void catImage(MediaReqVo req) {
String localTempUrl = this.getDownM3muFile(req).getAbsolutePath();
String localCatUrl = this.getCatTempFile(req).getAbsolutePath();
MediaHelper.getVideoFirstImage(localTempUrl, localCatUrl);
}
public boolean isFinish(MediaReqVo req) {
MediaResVo res = this.getCache().get(req);
System.out.println("剩余次数:" + res.getCount());
// 判断是否还有执行次数
return res.getCount() < 1;
}
}