Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
2b0ae935
Commit
2b0ae935
authored
Nov 15, 2021
by
yanxia54
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
异常信息优化
parent
fe741404
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
28 deletions
+45
-28
MediaCacheLocal.java
.../src/main/java/com/yanzuoguang/media/MediaCacheLocal.java
+2
-1
MediaFirst.java
...image/src/main/java/com/yanzuoguang/media/MediaFirst.java
+34
-14
MediaFirstTest.java
yzg-util-image/src/test/java/helper/MediaFirstTest.java
+9
-13
No files found.
yzg-util-image/src/main/java/com/yanzuoguang/media/MediaCacheLocal.java
View file @
2b0ae935
package
com
.
yanzuoguang
.
media
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
org.springframework.stereotype.Component
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -10,7 +11,7 @@ import java.util.Map;
*
* @author 颜佐光
*/
public
class
MediaCacheLocal
implements
MediaCache
Base
{
public
class
MediaCacheLocal
implements
MediaCache
{
private
Map
<
String
,
MapLock
>
mapLock
=
new
HashMap
<
String
,
MapLock
>();
...
...
yzg-util-image/src/main/java/com/yanzuoguang/media/MediaFirst.java
View file @
2b0ae935
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.helper.StringHelper
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
...
...
@@ -45,7 +47,7 @@ public class MediaFirst {
throw
new
CodeException
(
"请通过程序编写视频缓存处理对象"
);
}
// 将临时文件移动到正式文件
cache
.
lockTempFile
(
req
,
new
Runnable
()
{
this
.
getCache
()
.
lockTempFile
(
req
,
new
Runnable
()
{
@Override
public
void
run
()
{
File
file
=
getFile
(
req
);
...
...
@@ -90,27 +92,28 @@ public class MediaFirst {
private
void
handle
(
MediaReqVo
req
)
{
do
{
MediaResVo
res
=
cache
.
get
(
req
);
MediaResVo
res
=
this
.
getCache
()
.
get
(
req
);
if
(
res
==
null
)
{
res
=
JsonHelper
.
to
(
req
,
MediaResVo
.
class
);
}
// 下载m3mu并转换成mp4
String
name
=
StringHelper
.
getNewID
();
downM3mu
(
req
);
// 转换为图片
catImage
(
req
);
// 将下载临时文件移动到临时文件
cache
.
lockTempFile
(
req
,
new
Runnable
()
{
@Override
public
void
run
()
{
File
catTemp
=
getCatTempFile
(
req
);
File
fileTemp
=
getFileTemp
(
req
);
if
(
catTemp
.
exists
())
{
catTemp
.
renameTo
(
fileTemp
);
}
}
});
// this.getCache()
.lockTempFile(req, new Runnable() {
//
@Override
//
public void run() {
//
File catTemp = getCatTempFile(req);
//
File fileTemp = getFileTemp(req);
//
if (catTemp.exists()) {
//
catTemp.renameTo(fileTemp);
//
}
//
}
//
});
// 写入缓存
cache
.
sub
(
res
);
this
.
getCache
()
.
sub
(
res
);
// 判断是否还有执行次数
if
(
res
.
getCount
()
<
1
)
{
break
;
...
...
@@ -122,10 +125,27 @@ public class MediaFirst {
}
private
void
downM3mu
(
MediaReqVo
req
)
{
try
{
String
targetFile
=
getTargetFile
(
req
.
getImageUrl
());
HlsDownloader
downloader
=
new
HlsDownloader
(
req
.
getUrl
(),
targetFile
+
".mp4"
,
1
,
req
.
getCount
()
);
downloader
.
download
(
false
,
true
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
void
catImage
(
MediaReqVo
req
)
{
String
targetFile
=
getTargetFile
(
req
.
getImageUrl
());
MediaHelper
.
getVideoFirstImage
(
targetFile
+
".mp4"
,
targetFile
+
".jpg"
);
}
private
String
getTargetFile
(
String
file
)
{
// 注意,路径应为文件在工程中的相对路径
File
f
=
new
File
(
file
);
return
f
.
getAbsolutePath
();
}
}
yzg-util-image/src/test/java/helper/MediaFirstTest.java
View file @
2b0ae935
package
helper
;
import
com.yanzuoguang.media.HlsDownloader
;
import
com.yanzuoguang.media.MediaFirst
;
import
com.yanzuoguang.media.MediaReqVo
;
import
com.yanzuoguang.media.MediaResVo
;
import
com.yanzuoguang.media.*
;
import
com.yanzuoguang.util.MediaHelper
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
...
...
@@ -33,15 +30,14 @@ public class MediaFirstTest {
@Test
public
void
test
()
{
MediaFirst
first
=
new
MediaFirst
();
for
(
int
i
=
0
;
i
<
1000
;
i
++)
{
MediaResVo
start
=
first
.
start
(
new
MediaReqVo
());
File
file
=
new
File
(
start
.
getImageUrl
());
if
(
file
.
exists
())
{
System
.
out
.
println
(
"文件"
+
file
.
getAbsolutePath
()
+
"已经存在"
);
}
else
{
System
.
out
.
println
(
"文件"
+
file
.
getAbsolutePath
()
+
"不存在"
);
}
MediaFirst
first
=
new
MediaFirst
(
new
MediaCacheLocal
());
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
MediaReqVo
req
=
new
MediaReqVo
();
req
.
setUrl
(
"http://rtmp.tourbida.com/hls/f4c1444c-1b31-4de8-9bd9-7ff6a231e262.m3u8"
);
req
.
setCount
(
1
);
req
.
setSplit
(
3000
);
req
.
setImageUrl
(
"target/videodir/f4c1444c-1b31-4de8-9bd9-7ff6a231e262_"
+
i
);
MediaResVo
start
=
first
.
start
(
req
);
ThreadHelper
.
sleep
(
5000
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment