ffmpeg cli wrapper
1.0.0
作者:安德鲁·布兰普顿 (bramp.net) (c) 2013-2024
用于从 Java 运行 FFmpeg 的流畅接口。
GitHub | API文档
我们目前支持 Java 8 及更高版本。使用Maven安装依赖。
< dependency >
< groupId >net.bramp.ffmpeg</ groupId >
< artifactId >ffmpeg</ artifactId >
< version >0.8.0</ version >
</ dependency >
代码:
FFmpeg ffmpeg = new FFmpeg ( "/path/to/ffmpeg" );
FFprobe ffprobe = new FFprobe ( "/path/to/ffprobe" );
FFmpegBuilder builder = new FFmpegBuilder ()
. setInput ( "input.mp4" ) // Filename, or a FFmpegProbeResult
. done ()
. overrideOutputFiles ( true ) // Override the output if it exists
. addOutput ( "output.mp4" ) // Filename for the destination
. setFormat ( "mp4" ) // Format is inferred from filename, or can be set
. setTargetSize ( 250_000 ) // Aim for a 250KB file
. disableSubtitle () // No subtiles
. setAudioChannels ( 1 ) // Mono audio
. setAudioCodec ( "aac" ) // using the aac codec
. setAudioSampleRate ( 48_000 ) // at 48KHz
. setAudioBitRate ( 32768 ) // at 32 kbit/s
. setVideoCodec ( "libx264" ) // Video using x264
. setVideoFrameRate ( 24 , 1 ) // at 24 frames per second
. setVideoResolution ( 640 , 480 ) // at 640x480 resolution
. setStrict ( FFmpegBuilder . Strict . EXPERIMENTAL ) // Allow FFmpeg to use experimental specs
. done ();
FFmpegExecutor executor = new FFmpegExecutor ( ffmpeg , ffprobe );
// Run a one-pass encode
executor . createJob ( builder ). run ();
// Or run a two-pass encode (which is better quality at the cost of being slower)
executor . createTwoPassJob ( builder ). run ();
代码:
FFprobe ffprobe = new FFprobe ( "/path/to/ffprobe" );
FFmpegProbeResult probeResult = ffprobe . probe ( "input.mp4" );
FFmpegFormat format = probeResult . getFormat ();
System . out . format ( "%nFile: '%s' ; Format: '%s' ; Duration: %.3fs" ,
format . filename ,
format . format_long_name ,
format . duration
);
FFmpegStream stream = probeResult . getStreams (). get ( 0 );
System . out . format ( "%nCodec: '%s' ; Width: %dpx ; Height: %dpx" ,
stream . codec_long_name ,
stream . width ,
stream . height
);
FFmpegExecutor executor = new FFmpegExecutor ( ffmpeg , ffprobe );
FFmpegProbeResult in = ffprobe . probe ( "input.flv" );
FFmpegBuilder builder = new FFmpegBuilder ()
. setInput ( in ) // Or filename
. done ()
. addOutput ( "output.mp4" )
. done ();
FFmpegJob job = executor . createJob ( builder , new ProgressListener () {
// Using the FFmpegProbeResult determine the duration of the input
final double duration_ns = in . getFormat (). duration * TimeUnit . SECONDS . toNanos ( 1 );
@ Override
public void progress ( Progress progress ) {
double percentage = progress . out_time_ns / duration_ns ;
// Print out interesting information about the progress
System . out . println ( String . format (
"[%.0f%%] status:%s frame:%d time:%s ms fps:%.0f speed:%.2fx" ,
percentage * 100 ,
progress . status ,
progress . frame ,
FFmpegUtils . toTimecode ( progress . out_time_ns , TimeUnit . NANOSECONDS ),
progress . fps . doubleValue (),
progress . speed
));
}
});
job . run ();
如果您想进行更改,那么构建和发布很简单:
# To build
mvn
# To test
mvn test
# To release (pushing jar to maven central)
# (don't forget to set up your ~/.m2/settings.xml)
mvn release:prepare
mvn release:perform
# To publish javadoc
git checkout ffmpeg-0.x
mvn clean javadoc:aggregate scm-publish:publish-scm
# Update Maven Plugins
mvn versions:display-plugin-updates
# Library Dependencies
mvn versions:display-dependency-updates
我们只支持原始的 FFmpeg,不支持 libav 版本。 Ubuntu 12.04 之前以及 15.04 及更高版本中提供了原始 FFmpeg。如果您必须在带有 libav 的版本上运行,您可以从 PPA 或使用静态构建安装 FFmpeg。更多信息请点击此处
我们欢迎贡献。请检查问题跟踪器。如果您看到想要处理的内容,请对该问题发表评论,或者只是发送拉取请求。想做别的事情,那就打开一个问题,我们就可以讨论!我们感谢文档改进、代码清理或新功能。请注意,所有工作都是在志愿者的基础上完成的,因此我们的回复可能会很慢。
Copyright (c) 2013-2024, Andrew Brampton
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.