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
7533cb00
Commit
7533cb00
authored
Jul 13, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
视频转换
parent
4951ed52
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
502 additions
and
313 deletions
+502
-313
MediaHelper.java
...image/src/main/java/com/yanzuoguang/util/MediaHelper.java
+84
-86
MediaParameter.java
...ge/src/main/java/com/yanzuoguang/util/MediaParameter.java
+406
-0
TestMediaHelper.java
yzg-util-image/src/test/java/helper/TestMediaHelper.java
+12
-227
100M.mp4.size_50.quot_50.mpeg.mp4
yzg-util-image/个是/100M.mp4.size_50.quot_50.mpeg.mp4
+0
-0
No files found.
yzg-util-image/src/main/java/com/yanzuoguang/util/MediaHelper.java
View file @
7533cb00
package
com
.
yanzuoguang
.
util
;
import
com.yanzuoguang.util.log.Log
;
import
org.bytedeco.javacpp.avcodec
;
import
org.bytedeco.javacpp.opencv_core
;
import
org.bytedeco.javacv.*
;
...
...
@@ -20,8 +21,6 @@ import java.util.List;
* @author 颜佐光
*/
public
class
MediaHelper
extends
ImageHelper
{
private
static
final
String
IMAGEMAT
=
"png"
;
private
static
final
String
ROTATE
=
"rotate"
;
/**
* 默认截取视频的中间帧为封面
*/
...
...
@@ -30,100 +29,99 @@ public class MediaHelper extends ImageHelper {
/**
* 从视频文件中获取第一张图片
*
* @param fromFile 视频文件
* @param toFile 第一帧图片
* @param format 要支持的格式
* @param codeC 视频的格式
* @param size 宽度和高度是否压缩
* @param bitRate 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。
* 该值应以位/秒表示。在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
* @param fromFile 视频文件
* @param toFile 转码后保存的文件
* @param parameter 转码后的参数,在转码完成后该参数值会被改变
*/
public
static
void
zipVideoMp4
(
String
fromFile
,
String
toFile
,
MediaParameter
parameter
)
throws
IOException
{
// 视频压缩参数初始化
if
(
parameter
==
null
)
{
parameter
=
new
MediaParameter
();
}
parameter
.
initZipMp4
();
convertVideo
(
fromFile
,
toFile
,
parameter
);
}
/**
* 转换成mp4
*
* @param fromFile
* @param toFile
* @param parameter
*/
public
static
void
convertVideoMp4
(
String
fromFile
,
String
toFile
,
MediaParameter
parameter
)
throws
IOException
{
// 视频压缩参数初始化
if
(
parameter
==
null
)
{
parameter
=
new
MediaParameter
();
}
parameter
.
initMp4
();
convertVideo
(
fromFile
,
toFile
,
parameter
);
}
/**
* 从视频文件中获取第一张图片
*
* @param fromFile 视频文件
* @param toFile 转码后保存的文件
* @param parameter 转码后的参数,在转码完成后该参数值会被改变
*/
public
static
void
zipVideo
(
String
fromFile
,
String
toFile
,
String
format
,
String
codeC
,
int
width
,
int
height
,
float
sizeZip
,
int
videoBitRate
,
float
videoBitRateZip
,
int
videoBitRate
,
float
videoBitRateZip
)
{
if
(
size
>
1
)
{
throw
new
RuntimeException
(
"size请在0~1之间"
);
public
static
void
convertVideo
(
String
fromFile
,
String
toFile
,
MediaParameter
parameter
)
throws
IOException
{
// 视频压缩参数初始化
if
(
parameter
==
null
)
{
parameter
=
new
MediaParameter
();
}
parameter
.
check
();
// 源文件判断
File
source
=
new
File
(
fromFile
);
if
(!
source
.
exists
())
{
throw
new
RuntimeException
(
"视频转码压缩时不存在"
);
}
File
target
=
new
File
(
toFile
);
if
(!
target
.
getParentFile
().
exists
())
{
target
.
getParentFile
().
mkdirs
();
}
// 编码方式
Encoder
encoder
=
new
Encoder
();
MultimediaInfo
fromInfo
=
encoder
.
getInfo
(
source
);
VideoInfo
fromVideo
=
fromInfo
.
getVideo
();
AudioInfo
fromAudio
=
fromInfo
.
getAudio
();
if
(
fromAudio
==
null
)
{
fromAudio
=
new
AudioInfo
();
}
if
(
fromVideo
==
null
)
{
fromVideo
=
new
VideoInfo
();
}
if
(
FORMAT_EMPTY
.
equals
(
format
)
||
format
==
null
)
{
format
=
fromInfo
.
getFormat
();
}
if
(
FORMAT_EMPTY
.
equals
(
codeC
)
||
codeC
==
null
)
{
codeC
=
fromVideo
.
getDecoder
();
}
FFmpegFrameGrabber
grabber
=
new
FFmpegFrameGrabber
(
fromFile
);
try
{
grabber
.
start
();
// 设置视频参数
parameter
.
init
(
grabber
);
Log
.
info
(
MediaHelper
.
class
,
"开始转换...源文件:%s 目标文件:%s 旋转参数:%s"
,
fromFile
,
toFile
,
parameter
.
toString
());
// 定义转码工具
FFmpegFrameRecorder
recorder
=
new
FFmpegFrameRecorder
(
toFile
,
parameter
.
getVideoWidthFinally
(),
parameter
.
getVideoHeightFinally
(),
grabber
.
getAudioChannels
()
);
parameter
.
init
(
grabber
,
recorder
);
try
{
// 开始转换
recorder
.
start
(
grabber
.
getFormatContext
());
avcodec
.
AVPacket
packet
;
long
dts
=
0
;
while
((
packet
=
grabber
.
grabPacket
())
!=
null
)
{
long
currentDts
=
packet
.
dts
();
if
(
currentDts
>=
dts
)
{
recorder
.
recordPacket
(
packet
);
}
dts
=
currentDts
;
}
}
finally
{
recorder
.
stop
();
recorder
.
release
();
}
// 音频编码设置
int
toBitRate
=
fromAudio
.
getBitRate
();
if
(
toBitRate
==
-
1
)
{
toBitRate
=
64000
;
Log
.
info
(
MediaHelper
.
class
,
"转换完成...源文件:%s 目标文件:%s 旋转参数:%s "
,
fromFile
,
toFile
,
parameter
.
toString
());
}
finally
{
grabber
.
stop
();
grabber
.
close
();
}
int
toChannels
=
fromAudio
.
getChannels
();
int
toSamplingRate
=
Math
.
min
(
22050
,
fromAudio
.
getSamplingRate
());
AudioAttributes
audio
=
new
AudioAttributes
();
audio
.
setCodec
(
"libmp3lame"
);
audio
.
setBitRate
(
toBitRate
);
audio
.
setChannels
(
toChannels
);
audio
.
setSamplingRate
(
toSamplingRate
);
// 视频编码设置
VideoAttributes
video
=
new
VideoAttributes
();
// video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
video
.
setCodec
(
codeC
);
// video.setTag("DIVX");
int
toFrameRate
=
15
;
video
.
setFrameRate
(
toFrameRate
);
// 压缩比特率
video
.
setBitRate
(
bitRate
);
// 压缩宽高
VideoSize
fromSize
=
fromVideo
.
getSize
();
int
toWidth
=
(
int
)
(
fromSize
.
getWidth
()
*
size
);
int
toHeight
=
(
int
)
(
fromSize
.
getHeight
()
*
size
);
VideoSize
toSize
=
new
VideoSize
(
toWidth
,
toHeight
);
video
.
setSize
(
toSize
);
Log
.
info
(
MediaHelper
.
class
,
"%s-%s "
+
"原音频:bitRate=%d,channels=%d,samplingRate=%d "
+
"新音频:bitRate=%d,channels=%d,samplingRate=%d \n"
+
"原视频:width=%d,height=%d,bitRate=%d,frameRate=%d "
+
"新视频:width=%d,height=%d,bitRate=%d,frameRate=%d "
,
format
,
codeC
,
fromAudio
.
getBitRate
(),
fromAudio
.
getChannels
(),
fromAudio
.
getSamplingRate
(),
toBitRate
,
toChannels
,
toSamplingRate
,
fromSize
.
getWidth
(),
fromSize
.
getHeight
(),
fromVideo
.
getBitRate
(),
toFrameRate
,
toSize
.
getWidth
(),
toSize
.
getHeight
(),
bitRate
,
toFrameRate
);
// 视频转码编码设置
EncodingAttributes
attrs
=
new
EncodingAttributes
();
attrs
.
setFormat
(
format
);
attrs
.
setAudioAttributes
(
audio
);
attrs
.
setVideoAttributes
(
video
);
// 编码器
encoder
.
encode
(
source
,
target
,
attrs
);
System
.
out
.
println
(
"压缩完成..."
);
}
/**
...
...
@@ -161,7 +159,7 @@ public class MediaHelper extends ImageHelper {
// 打开视频
ff
.
start
();
// 获取视频的角度
String
rotate
=
ff
.
getVideoMetadata
(
ROTATE
);
String
rotate
=
ff
.
getVideoMetadata
(
MediaParameter
.
ROTATE
);
// 整个视频的长度
int
ffLength
=
ff
.
getLengthInFrames
();
// 取得能够取得的帧数
...
...
yzg-util-image/src/main/java/com/yanzuoguang/util/MediaParameter.java
0 → 100644
View file @
7533cb00
package
com
.
yanzuoguang
.
util
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.vo.BaseVo
;
import
io.swagger.annotations.ApiModelProperty
;
import
org.bytedeco.javacpp.avcodec
;
import
org.bytedeco.javacpp.avutil
;
import
org.bytedeco.javacv.FFmpegFrameGrabber
;
import
org.bytedeco.javacv.FFmpegFrameRecorder
;
import
static
org
.
bytedeco
.
javacpp
.
avcodec
.
AV_CODEC_ID_NONE
;
import
static
org
.
bytedeco
.
javacpp
.
avutil
.
AV_PIX_FMT_NONE
;
import
static
org
.
bytedeco
.
javacpp
.
avutil
.
AV_SAMPLE_FMT_NONE
;
/**
* 压缩视频参数
*
* @author 颜佐光
*/
public
class
MediaParameter
extends
BaseVo
{
public
static
final
int
VIDEO_CODEC_MP4
=
avcodec
.
AV_CODEC_ID_H264
;
public
static
final
String
VIDEO_FORMAT_MP4
=
"mp4"
;
public
static
final
int
VIDEO_FIXED_MP4
=
avutil
.
AV_PIX_FMT_YUV420P
;
public
static
final
String
ROTATE
=
"rotate"
;
/**
* 音频转码后的格式,为空时表示不改变格式
*/
@ApiModelProperty
(
notes
=
"音频转码后的格式,为空时表示不改变格式"
)
private
int
audioCodec
=
AV_CODEC_ID_NONE
;
/**
* 音频帧速率FrameRate
*/
@ApiModelProperty
(
notes
=
"音频转码后的格式,为空时表示不改变格式"
)
private
double
audioFrameRate
=
AV_SAMPLE_FMT_NONE
;
/**
* 音频转码的比特率,为0时表示采取原来的值
*/
@ApiModelProperty
(
notes
=
"音频转码的比特率,为0时表示采取原来的值"
)
private
int
audioBitRate
;
/**
* 音频最小比率率,为0时表示最小无效
*/
@ApiModelProperty
(
notes
=
"音频最小比率率,为0时表示最小无效"
)
private
int
minAudioBitRate
;
/**
* 音频比特率压缩比,算法为: 最终比特率= Math.max(minAudioBitRate,audioBitRate * audioBitRateZip)
*/
@ApiModelProperty
(
notes
=
"音频比特率压缩比,算法为: 最终比特率= Math.max(minAudioBitRate,audioBitRate * audioBitRateZip)"
)
private
float
audioBitRateZip
=
1
;
/**
* 视频转码后的格式
*/
@ApiModelProperty
(
notes
=
"视频转码后的格式"
)
private
String
videoFormat
;
/**
* 视频转码后的央视
*/
@ApiModelProperty
(
notes
=
"视频转码后的央视"
)
private
int
videoCodeC
=
AV_CODEC_ID_NONE
;
/**
* 视频像素格式
*/
@ApiModelProperty
(
notes
=
"视频像素格式"
)
private
int
videoPixelFormat
=
AV_PIX_FMT_NONE
;
/**
* 视频宽度,为0时默认为原来的宽度
*/
@ApiModelProperty
(
notes
=
"视频宽度,为0时默认为原来的宽度"
)
private
int
videoWidth
;
/**
* 视频高度,为0时默认为原来的高度
*/
@ApiModelProperty
(
notes
=
"视频高度,为0时默认为原来的高度"
)
private
int
videoHeight
;
/**
* 最小视频宽度
*/
@ApiModelProperty
(
notes
=
"最小视频宽度"
)
private
int
minVideoWidth
;
/**
* 最小视频高度
*/
@ApiModelProperty
(
notes
=
"最小视频高度"
)
private
int
minVideoHeight
;
/**
* 视频帧速率FrameRate
*/
@ApiModelProperty
(
notes
=
"视频帧速率FrameRate"
)
private
double
videoFrameRate
;
/**
* 视频原角度
*/
@ApiModelProperty
(
notes
=
"视频原角度"
)
private
double
videoRotate
;
/**
* 视频变更角度,最终角度=视频原角度+视频变更角度
*/
@ApiModelProperty
(
notes
=
"视频变更角度,最终角度=视频原角度+视频变更角度"
)
private
double
videoRotateChange
;
/**
* 视频宽度高度压缩比,算法为: 最终宽度=Math.max(minVideoWidth,videoWidth * videoSizeZip),
* * 最终高度:=Math.max(minVideoHeight,videoHeight * videoSizeZip)
*/
@ApiModelProperty
(
notes
=
"视频宽度高度压缩比,算法为: 最终宽度=Math.max(minVideoWidth,videoWidth * videoSizeZip),"
+
"最终高度:=Math.max(minVideoHeight,videoHeight * videoSizeZip)"
)
private
float
videoSizeZip
=
1
;
/**
* 视频转码的比特率,为0时表示采取原来的值
*/
@ApiModelProperty
(
notes
=
"视频转码的比特率,为0时表示采取原来的值"
)
private
int
videoBitRate
;
/**
* 视频最小比率率,为0时表示最小无效
*/
@ApiModelProperty
(
notes
=
"视频最小比率率,为0时表示最小无效"
)
private
int
minVideoBitRate
;
/**
* 视频比特率压缩比,算法为: 最终比特率= Math.max(minAudioBitRate,audioBitRate * audioBitRateZip)
*/
@ApiModelProperty
(
notes
=
"视频比特率压缩比,算法为: 最终比特率= Math.max(minAudioBitRate,audioBitRate * audioBitRateZip)"
)
private
float
videoBitRateZip
=
1
;
/**
* 音频最终比特率
*
* @return
*/
public
int
getAudioBitRateFinally
()
{
return
Math
.
max
(
this
.
minAudioBitRate
,
(
int
)
(
this
.
audioBitRate
*
this
.
audioBitRateZip
));
}
/**
* 视频最终比特率
*
* @return
*/
public
int
getVideoBitRateFinally
()
{
return
Math
.
max
(
this
.
minVideoBitRate
,
(
int
)
(
this
.
videoBitRate
*
this
.
videoBitRateZip
));
}
/**
* 音频最终宽度
*
* @return
*/
public
int
getVideoWidthFinally
()
{
return
Math
.
max
(
this
.
minVideoWidth
,
(
int
)
(
this
.
videoWidth
*
this
.
videoSizeZip
));
}
/**
* 音频最终高度
*
* @return
*/
public
int
getVideoHeightFinally
()
{
return
Math
.
max
(
this
.
minVideoHeight
,
(
int
)
(
this
.
videoHeight
*
this
.
videoSizeZip
));
}
/**
* 音频最终高度
*
* @return
*/
public
double
getVideoRotateFinally
()
{
return
this
.
videoRotate
+
this
.
videoRotateChange
;
}
/**
* 检查参数
*/
public
void
check
()
{
if
(
audioBitRateZip
>
1
)
{
throw
new
RuntimeException
(
"audioBitRateZip请在0~1之间"
);
}
if
(
videoSizeZip
>
1
)
{
throw
new
RuntimeException
(
"videoSizeZip请在0~1之间"
);
}
if
(
videoBitRateZip
>
1
)
{
throw
new
RuntimeException
(
"videoBitRateZip请在0~1之间"
);
}
}
/**
* 设置默认参数
*
* @param grabber 参数的视频
*/
public
void
init
(
FFmpegFrameGrabber
grabber
)
{
this
.
check
();
// 读取音频属性
this
.
audioBitRate
=
StringHelper
.
getFirst
(
this
.
audioBitRate
,
grabber
.
getAudioBitrate
());
this
.
audioCodec
=
StringHelper
.
getFirstRun
(
AV_CODEC_ID_NONE
,
AV_CODEC_ID_NONE
,
this
.
audioCodec
,
grabber
.
getAudioCodec
());
this
.
audioFrameRate
=
StringHelper
.
getFirstRun
((
double
)
AV_SAMPLE_FMT_NONE
,
(
double
)
AV_SAMPLE_FMT_NONE
,
this
.
audioFrameRate
,
grabber
.
getAudioFrameRate
());
// 视频宽度高度
this
.
videoWidth
=
StringHelper
.
getFirst
(
this
.
videoWidth
,
grabber
.
getImageWidth
());
this
.
videoHeight
=
StringHelper
.
getFirst
(
this
.
videoHeight
,
grabber
.
getImageHeight
());
// 读取视频格式
this
.
videoFormat
=
StringHelper
.
getFirst
(
this
.
videoFormat
,
grabber
.
getFormat
());
this
.
videoCodeC
=
StringHelper
.
getFirst
(
this
.
videoCodeC
,
grabber
.
getVideoCodec
());
this
.
videoPixelFormat
=
StringHelper
.
getFirstRun
(
AV_PIX_FMT_NONE
,
AV_PIX_FMT_NONE
,
this
.
videoPixelFormat
,
grabber
.
getPixelFormat
());
// 读取视频属性
this
.
videoRotate
=
StringHelper
.
toDouble
(
grabber
.
getVideoMetadata
(
ROTATE
));
this
.
videoFrameRate
=
StringHelper
.
getFirst
(
this
.
videoFrameRate
,
grabber
.
getFrameRate
());
this
.
videoBitRate
=
StringHelper
.
getFirst
(
this
.
videoBitRate
,
grabber
.
getVideoBitrate
());
}
/**
* 设置转码参数
*
* @param recorder 需要转码的对象
*/
public
void
init
(
FFmpegFrameGrabber
grabber
,
FFmpegFrameRecorder
recorder
)
{
// 设置音频比特率
recorder
.
setAudioBitrate
(
this
.
getAudioBitRateFinally
());
recorder
.
setAudioCodec
(
this
.
audioCodec
);
// 设置声道
recorder
.
setAudioChannels
(
grabber
.
getAudioChannels
());
// 视频格式
recorder
.
setFormat
(
this
.
videoFormat
);
// 设置转码视频格式
recorder
.
setVideoCodec
(
this
.
videoCodeC
);
// 设置像素格式
recorder
.
setPixelFormat
(
this
.
videoPixelFormat
);
// 设置转码后的视频角度
recorder
.
setMetadata
(
ROTATE
,
String
.
valueOf
(
this
.
getVideoRotateFinally
()));
// 设置视频比特率
recorder
.
setVideoBitrate
(
StringHelper
.
getFirst
(
this
.
getVideoBitRateFinally
(),
this
.
getAudioBitRateFinally
()));
// 设置帧速率
recorder
.
setFrameRate
(
grabber
.
getFrameRate
());
}
public
void
initZipMp4
()
{
this
.
initMp4
();
// 设置压缩帧
this
.
videoFrameRate
=
15
;
// 设置压缩大小
this
.
videoSizeZip
=
0.5f
;
// 设置视频压缩率
this
.
videoBitRateZip
=
0.5f
;
}
public
void
initMp4
()
{
this
.
videoCodeC
=
VIDEO_CODEC_MP4
;
this
.
videoFormat
=
VIDEO_FORMAT_MP4
;
this
.
videoPixelFormat
=
VIDEO_FIXED_MP4
;
}
public
int
getAudioCodec
()
{
return
audioCodec
;
}
public
void
setAudioCodec
(
int
audioCodec
)
{
this
.
audioCodec
=
audioCodec
;
}
public
double
getAudioFrameRate
()
{
return
audioFrameRate
;
}
public
void
setAudioFrameRate
(
double
audioFrameRate
)
{
this
.
audioFrameRate
=
audioFrameRate
;
}
public
int
getAudioBitRate
()
{
return
audioBitRate
;
}
public
void
setAudioBitRate
(
int
audioBitRate
)
{
this
.
audioBitRate
=
audioBitRate
;
}
public
int
getMinAudioBitRate
()
{
return
minAudioBitRate
;
}
public
void
setMinAudioBitRate
(
int
minAudioBitRate
)
{
this
.
minAudioBitRate
=
minAudioBitRate
;
}
public
float
getAudioBitRateZip
()
{
return
audioBitRateZip
;
}
public
void
setAudioBitRateZip
(
float
audioBitRateZip
)
{
this
.
audioBitRateZip
=
audioBitRateZip
;
}
public
String
getVideoFormat
()
{
return
videoFormat
;
}
public
void
setVideoFormat
(
String
videoFormat
)
{
this
.
videoFormat
=
videoFormat
;
}
public
int
getVideoCodeC
()
{
return
videoCodeC
;
}
public
void
setVideoCodeC
(
int
videoCodeC
)
{
this
.
videoCodeC
=
videoCodeC
;
}
public
int
getVideoPixelFormat
()
{
return
videoPixelFormat
;
}
public
void
setVideoPixelFormat
(
int
videoPixelFormat
)
{
this
.
videoPixelFormat
=
videoPixelFormat
;
}
public
int
getVideoWidth
()
{
return
videoWidth
;
}
public
void
setVideoWidth
(
int
videoWidth
)
{
this
.
videoWidth
=
videoWidth
;
}
public
int
getVideoHeight
()
{
return
videoHeight
;
}
public
void
setVideoHeight
(
int
videoHeight
)
{
this
.
videoHeight
=
videoHeight
;
}
public
int
getMinVideoWidth
()
{
return
minVideoWidth
;
}
public
void
setMinVideoWidth
(
int
minVideoWidth
)
{
this
.
minVideoWidth
=
minVideoWidth
;
}
public
int
getMinVideoHeight
()
{
return
minVideoHeight
;
}
public
void
setMinVideoHeight
(
int
minVideoHeight
)
{
this
.
minVideoHeight
=
minVideoHeight
;
}
public
double
getVideoFrameRate
()
{
return
videoFrameRate
;
}
public
void
setVideoFrameRate
(
double
videoFrameRate
)
{
this
.
videoFrameRate
=
videoFrameRate
;
}
public
double
getVideoRotate
()
{
return
videoRotate
;
}
public
void
setVideoRotate
(
double
videoRotate
)
{
this
.
videoRotate
=
videoRotate
;
}
public
double
getVideoRotateChange
()
{
return
videoRotateChange
;
}
public
void
setVideoRotateChange
(
double
videoRotateChange
)
{
this
.
videoRotateChange
=
videoRotateChange
;
}
public
float
getVideoSizeZip
()
{
return
videoSizeZip
;
}
public
void
setVideoSizeZip
(
float
videoSizeZip
)
{
this
.
videoSizeZip
=
videoSizeZip
;
}
public
int
getVideoBitRate
()
{
return
videoBitRate
;
}
public
void
setVideoBitRate
(
int
videoBitRate
)
{
this
.
videoBitRate
=
videoBitRate
;
}
public
int
getMinVideoBitRate
()
{
return
minVideoBitRate
;
}
public
void
setMinVideoBitRate
(
int
minVideoBitRate
)
{
this
.
minVideoBitRate
=
minVideoBitRate
;
}
public
float
getVideoBitRateZip
()
{
return
videoBitRateZip
;
}
public
void
setVideoBitRateZip
(
float
videoBitRateZip
)
{
this
.
videoBitRateZip
=
videoBitRateZip
;
}
}
yzg-util-image/src/test/java/helper/TestMediaHelper.java
View file @
7533cb00
package
helper
;
import
com.yanzuoguang.util.ImageHelper
;
import
com.yanzuoguang.util.MediaHelper
;
import
com.yanzuoguang.util.MediaParameter
;
import
com.yanzuoguang.util.thread.RunnableListAuto
;
import
it.sauronsoftware.jave.Encoder
;
import
it.sauronsoftware.jave.EncoderException
;
import
org.junit.Test
;
import
java.io.File
;
...
...
@@ -19,57 +17,6 @@ public class TestMediaHelper {
public
float
[]
quotes
=
new
float
[]{
1
f
,
1
f
,
0.5f
,
0.25f
,
0.5f
,
0.25f
};
@Test
public
void
testMediaType
()
throws
EncoderException
,
IOException
{
System
.
out
.
println
(
"100M.mp4 "
+
ImageHelper
.
getMediaType
(
getFile
(
"100M.mp4"
)));
System
.
out
.
println
(
"100M.mp4.jpg "
+
ImageHelper
.
getMediaType
(
getFile
(
"100M.mp4.jpg"
)));
System
.
out
.
println
(
"xuziming.mp4 "
+
ImageHelper
.
getMediaType
(
getFile
(
"xuziming.mp4"
)));
System
.
out
.
println
(
"z001594372388232a3017ad69c82a342.MOV "
+
ImageHelper
.
getMediaType
(
getFile
(
"z001594372388232a3017ad69c82a342.MOV"
)));
}
@Test
public
void
printEncoding
()
throws
EncoderException
{
Encoder
encoder
=
new
Encoder
();
String
[]
audioEncoders
=
encoder
.
getVideoEncoders
();
for
(
String
item
:
audioEncoders
)
{
System
.
out
.
println
(
item
);
}
}
@Test
public
void
testConvert
()
throws
EncoderException
{
Encoder
encoder
=
new
Encoder
();
String
[]
audioEncoders
=
encoder
.
getVideoEncoders
();
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
String
item
:
audioEncoders
)
{
list
.
add
(
testConvertThread
(
item
,
1
f
,
1
f
));
}
RunnableListAuto
.
run
(
list
);
}
private
Runnable
testConvertThread
(
String
item
,
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
testConvert
(
item
,
size
,
quote
);
}
catch
(
EncoderException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
};
}
private
void
testConvert
(
String
item
,
float
size
,
float
quote
)
throws
EncoderException
{
String
file
=
getFile
(
"100M.mp4"
);
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
String
name
=
String
.
format
(
"%s.size_%d.quot_%d.%s.mp4"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
),
item
);
MediaHelper
.
zipVideo
(
file
,
name
,
"mp4"
,
item
,
size
,
(
int
)
(
MediaHelper
.
DEFAULT_BIT_RATE
*
quote
));
}
private
String
getFile
(
String
file
)
{
// 注意,路径应为文件在工程中的相对路径
File
f
=
new
File
(
"src/test/java/helper/"
+
file
);
...
...
@@ -88,198 +35,36 @@ public class TestMediaHelper {
}
@Test
public
void
testVideoFirstImage
()
throws
IOException
{
if
(
isFirstImage
)
{
return
;
}
String
file
=
getFile
(
"100M.mp4"
);
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
MediaHelper
.
getVideoFirstImage
(
file
,
targetFile
+
".jpg"
);
isFirstImage
=
true
;
}
@Test
public
void
testVideoFirstImage1
()
throws
IOException
{
if
(
isFirstImage
)
{
return
;
}
String
file
=
getFile
(
"xuziming.mp4"
);
String
targetFile
=
getTargetFile
(
"target/xuziming.mp4"
);
MediaHelper
.
getVideoFirstImage
(
file
,
targetFile
+
".jpg"
);
isFirstImage
=
true
;
}
@Test
public
void
testImageZip
()
throws
IOException
{
// 生成测试图片
testVideoFirstImage
();
public
void
testConvert
()
{
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
sizes
.
length
;
i
++)
{
list
.
add
(
test
ImageZip
Thread
(
sizes
[
i
],
quotes
[
i
]));
list
.
add
(
test
Convert
Thread
(
sizes
[
i
],
quotes
[
i
]));
}
RunnableListAuto
.
run
(
list
);
}
private
Runnable
testImageZipThread
(
float
size
,
float
quote
)
{
private
Runnable
testConvertThread
(
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
test
ImageZip
(
size
,
quote
);
test
Convert
(
size
,
quote
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
};
}
private
void
testImageZip
(
float
size
,
float
quote
)
throws
IOException
{
String
file
=
getTargetFile
(
"target/100M.mp4"
)
+
".jpg"
;
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
String
toName
=
String
.
format
(
"%s.size_%d.quot_%d.jpg"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
String
toNameSystem
=
String
.
format
(
"%s.size_%d.quot_%d.system.jpg"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
MediaHelper
.
compressPic
(
file
,
toName
,
quote
,
size
);
MediaHelper
.
compressPicBySystem
(
file
,
toNameSystem
,
quote
,
size
);
}
@Test
public
void
testVideoZipFlv
()
{
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
sizes
.
length
;
i
++)
{
list
.
add
(
testVideoZipFlvThread
(
sizes
[
i
],
quotes
[
i
]));
}
RunnableListAuto
.
run
(
list
);
}
private
Runnable
testVideoZipFlvThread
(
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
testVideoZipFlv
(
size
,
quote
);
}
catch
(
EncoderException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
};
}
private
void
testVideoZipFlv
(
float
size
,
float
quote
)
throws
EncoderException
{
String
file
=
getFile
(
"100M.mp4"
);
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
String
name
=
String
.
format
(
"%s.size_%d.quot_%d.flv"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
MediaHelper
.
zipVideoFlv
(
file
,
name
,
size
,
(
int
)
(
MediaHelper
.
DEFAULT_BIT_RATE
*
quote
));
}
@Test
public
void
testVideoZipMpeg_50_50
()
throws
EncoderException
{
testVideoZipMpeg
(
0.5f
,
0.5f
);
}
@Test
public
void
testVideoZipMpeg
()
throws
EncoderException
{
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
sizes
.
length
;
i
++)
{
list
.
add
(
testVideoZipMpegThread
(
sizes
[
i
],
quotes
[
i
]));
}
RunnableListAuto
.
run
(
list
);
}
private
Runnable
testVideoZipMpegThread
(
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
testVideoZipMpeg
(
size
,
quote
);
}
catch
(
EncoderException
e
)
{
throw
new
RuntimeException
(
e
);
e
.
printStackTrace
();
}
}
};
}
private
void
test
VideoZipMpeg
(
float
size
,
float
quote
)
throws
Encoder
Exception
{
private
void
test
Convert
(
float
size
,
float
quote
)
throws
IO
Exception
{
String
file
=
getFile
(
"100M.mp4"
);
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
String
name
=
String
.
format
(
"%s.size_%d.quot_%d.mpeg.mp4"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
MediaHelper
.
zipVideoMpeg
(
file
,
name
,
size
,
(
int
)
(
MediaHelper
.
DEFAULT_BIT_RATE
*
quote
));
}
String
toFile
=
String
.
format
(
"%s.size_%d.quot_%d.mp4"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
@Test
public
void
testVideoZip3pg
()
throws
EncoderException
{
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
sizes
.
length
;
i
++)
{
list
.
add
(
testVideoZip3pgThread
(
sizes
[
i
],
quotes
[
i
]));
}
RunnableListAuto
.
run
(
list
);
}
MediaParameter
parameter
=
new
MediaParameter
();
parameter
.
setVideoSizeZip
(
size
);
parameter
.
setVideoBitRateZip
(
quote
);
private
Runnable
testVideoZip3pgThread
(
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
testVideoZip3pg
(
size
,
quote
);
}
catch
(
EncoderException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
};
}
private
void
testVideoZip3pg
(
float
size
,
float
quote
)
throws
EncoderException
{
String
file
=
getFile
(
"100M.mp4"
);
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
String
name
=
String
.
format
(
"%s.size_%d.quot_%d.3pg"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
MediaHelper
.
zipVideo3pg
(
file
,
name
,
size
,
(
int
)
(
MediaHelper
.
DEFAULT_BIT_RATE
*
quote
));
}
@Test
public
void
testVideoZip_50_50
()
throws
EncoderException
{
testVideoZip
(
0.5f
,
0.5f
);
}
@Test
public
void
testVideoZip
()
throws
EncoderException
{
List
<
Runnable
>
list
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
sizes
.
length
;
i
++)
{
list
.
add
(
testVideoZipThread
(
sizes
[
i
],
quotes
[
i
]));
}
RunnableListAuto
.
run
(
list
);
}
private
Runnable
testVideoZipThread
(
float
size
,
float
quote
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
testVideoZip
(
size
,
quote
);
}
catch
(
EncoderException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
};
}
private
void
testVideoZip
(
float
size
,
float
quote
)
throws
EncoderException
{
String
file
=
getFile
(
"100M.mp4"
);
String
targetFile
=
getTargetFile
(
"target/100M.mp4"
);
String
name
=
String
.
format
(
"%s.size_%d.quot_%d.mp4"
,
targetFile
,
(
int
)
(
size
*
100
),
(
int
)
(
quote
*
100
));
MediaHelper
.
zipVideo
(
file
,
name
,
size
,
(
int
)
(
MediaHelper
.
DEFAULT_BIT_RATE
*
quote
));
MediaHelper
.
convertVideoMp4
(
file
,
toFile
,
parameter
);
}
}
yzg-util-image/个是/100M.mp4.size_50.quot_50.mpeg.mp4
deleted
100644 → 0
View file @
4951ed52
File deleted
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