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
f1f7d5ee
Commit
f1f7d5ee
authored
Nov 15, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载视频
parent
2b0ae935
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
52 deletions
+60
-52
MediaFirst.java
...image/src/main/java/com/yanzuoguang/media/MediaFirst.java
+47
-45
MediaFirstTest.java
yzg-util-image/src/test/java/helper/MediaFirstTest.java
+13
-7
No files found.
yzg-util-image/src/main/java/com/yanzuoguang/media/MediaFirst.java
View file @
f1f7d5ee
...
...
@@ -4,7 +4,6 @@ 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
;
...
...
@@ -64,12 +63,13 @@ public class MediaFirst {
@Override
public
void
run
()
{
// 开始开启线程运行
Thread
Helper
.
run
Thread
(
new
Runnable
()
{
Thread
thread
=
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
handle
(
req
);
}
});
thread
.
start
();
}
});
}
...
...
@@ -83,11 +83,11 @@ public class MediaFirst {
}
private
File
getDownM3muFile
(
MediaReqVo
req
)
{
return
n
ull
;
return
n
ew
File
(
req
.
getImageUrl
()
+
".tmp.mp4"
)
;
}
private
File
getCatTempFile
(
MediaReqVo
req
)
{
return
n
ull
;
return
n
ew
File
(
req
.
getImageUrl
()
+
".tmp.jpg"
)
;
}
private
void
handle
(
MediaReqVo
req
)
{
...
...
@@ -96,56 +96,58 @@ public class MediaFirst {
if
(
res
==
null
)
{
res
=
JsonHelper
.
to
(
req
,
MediaResVo
.
class
);
}
try
{
// 下载m3mu并转换成mp4
String
name
=
StringHelper
.
getNewID
();
downM3mu
(
req
);
// 转换为图片
catImage
(
req
);
// 将下载临时文件移动到临时文件
// this.getCache().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
mp4
=
getDownM3muFile
(
req
);
File
catTemp
=
getCatTempFile
(
req
);
File
fileTemp
=
getFileTemp
(
req
);
if
(
catTemp
.
exists
())
{
catTemp
.
renameTo
(
fileTemp
);
}
mp4
.
delete
();
}
});
// 写入缓存
this
.
getCache
().
sub
(
res
);
// 判断是否还有执行次数
if
(
res
.
getCount
()
<
1
)
{
if
(
isFinish
(
req
)
)
{
break
;
}
// 等待下一次截图
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
ThreadHelper
.
sleep
(
res
.
getSplit
());
}
while
(
true
);
}
while
(
!
isFinish
(
req
)
);
}
private
void
downM3mu
(
MediaReqVo
req
)
{
try
{
String
targetFile
=
getTargetFile
(
req
.
getImageUrl
());
private
void
downM3mu
(
MediaReqVo
req
)
throws
Exception
{
String
localUrl
=
getDownM3muFile
(
req
).
getAbsolutePath
();
HlsDownloader
downloader
=
new
HlsDownloader
(
req
.
getUrl
(),
targetFile
+
".mp4"
,
localUrl
,
1
,
req
.
getCount
()
1
);
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"
);
String
localTempUrl
=
this
.
getDownM3muFile
(
req
).
getAbsolutePath
();
String
localCatUrl
=
this
.
getCatTempFile
(
req
).
getAbsolutePath
();
MediaHelper
.
getVideoFirstImage
(
localTempUrl
,
localCatUrl
);
}
private
String
getTargetFile
(
String
file
)
{
// 注意,路径应为文件在工程中的相对路径
File
f
=
new
File
(
file
);
return
f
.
getAbsolutePath
();
public
boolean
isFinish
(
MediaReqVo
req
)
{
MediaResVo
res
=
this
.
getCache
().
get
(
req
);
System
.
out
.
println
(
"剩余次数:"
+
res
.
getCount
());
// 判断是否还有执行次数
return
res
.
getCount
()
<
1
;
}
}
yzg-util-image/src/test/java/helper/MediaFirstTest.java
View file @
f1f7d5ee
...
...
@@ -31,16 +31,22 @@ public class MediaFirstTest {
@Test
public
void
test
()
{
MediaFirst
first
=
new
MediaFirst
(
new
MediaCacheLocal
());
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
MediaReqVo
req
=
new
MediaReqVo
();
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
);
req
.
setImageUrl
(
getTargetFile
(
"target/xxx.jpg"
));
req
.
setCount
(
100
);
req
.
setSplit
(
1000
);
for
(
int
i
=
0
;
i
<
1000
;
i
++)
{
MediaResVo
start
=
first
.
start
(
req
);
ThreadHelper
.
sleep
(
5000
);
ThreadHelper
.
sleep
(
50
*
10
00
);
}
// 等待视频处理结束
while
(!
first
.
isFinish
(
req
))
{
ThreadHelper
.
sleep
(
100
);
}
}
...
...
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