Jaffree

Jaffree

Java实现的FFmpeg和FFprobe命令行封装库

Jaffree是Java实现的FFmpeg和FFprobe命令行封装库,通过java.lang.Process与ffmpeg集成。它支持编程方式的视频处理,提供媒体流检查、获取文件时长、重编码、剪辑缩放等功能。Jaffree还支持通过多种方式进行数据传输,并可实现直播流重新流化和屏幕捕获等高级操作。

JaffreeFFmpegJava视频处理命令行封装Github开源项目

SWUbanner

Jaffree Sparkline

Jaffree stands for JAva FFmpeg and FFprobe FREE command line wrapper. Jaffree supports programmatic video production and consumption (with transparency)

It integrates with ffmpeg via java.lang.Process.

Inspired by ffmpeg-cli-wrapper

Tested with the help of GitHub Actions

Tests Coverage

OS: Ubuntu, MacOS, Windows

JDK: 8, 11, 17

Usage

Maven Central

<dependency> <groupId>com.github.kokorin.jaffree</groupId> <artifactId>jaffree</artifactId> <version>${jaffree.version}</version> </dependency> <!-- You should also include slf4j into dependencies. This is done intentionally to allow changing of slf4j version. --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency>

Examples

Checking media streams with ffprobe

See whole example here.

FFprobeResult result = FFprobe.atPath() .setShowStreams(true) .setInput(pathToVideo) .execute(); for (Stream stream : result.getStreams()) { System.out.println("Stream #" + stream.getIndex() + " type: " + stream.getCodecType() + " duration: " + stream.getDuration() + " seconds"); }

Detecting exact media file duration

Sometimes ffprobe can't show exact duration, use ffmpeg trancoding to NULL output to get it.

See whole example here.

final AtomicLong durationMillis = new AtomicLong(); FFmpegResult ffmpegResult = FFmpeg.atPath() .addInput( UrlInput.fromUrl(pathToVideo) ) .addOutput(new NullOutput()) .setProgressListener(new ProgressListener() { @Override public void onProgress(FFmpegProgress progress) { durationMillis.set(progress.getTimeMillis()); } }) .execute(); System.out.println("Exact duration: " + durationMillis.get() + " milliseconds");

Re-encode and track progress

See whole example here.

final AtomicLong duration = new AtomicLong(); FFmpeg.atPath() .addInput(UrlInput.fromUrl(pathToSrc)) .setOverwriteOutput(true) .addOutput(new NullOutput()) .setProgressListener(new ProgressListener() { @Override public void onProgress(FFmpegProgress progress) { duration.set(progress.getTimeMillis()); } }) .execute(); FFmpeg.atPath() .addInput(UrlInput.fromUrl(pathToSrc)) .setOverwriteOutput(true) .addArguments("-movflags", "faststart") .addOutput(UrlOutput.toUrl(pathToDst)) .setProgressListener(new ProgressListener() { @Override public void onProgress(FFmpegProgress progress) { double percents = 100. * progress.getTimeMillis() / duration.get(); System.out.println("Progress: " + percents + "%"); } }) .execute();

Cut and scale media file

Pay attention that arguments related to Input must be set at Input, not at FFmpeg.

See whole example here.

FFmpeg.atPath() .addInput( UrlInput.fromUrl(pathToSrc) .setPosition(10, TimeUnit.SECONDS) .setDuration(42, TimeUnit.SECONDS) ) .setFilter(StreamType.VIDEO, "scale=160:-2") .setOverwriteOutput(true) .addArguments("-movflags", "faststart") .addOutput( UrlOutput.toUrl(pathToDst) .setPosition(10, TimeUnit.SECONDS) ) .execute();

Custom parsing of ffmpeg output

See whole example here.

// StringBuffer - because it's thread safe final StringBuffer loudnormReport = new StringBuffer(); FFmpeg.atPath() .addInput(UrlInput.fromUrl(pathToVideo)) .addArguments("-af", "loudnorm=I=-16:TP=-1.5:LRA=11:print_format=json") .addOutput(new NullOutput(false)) .setOutputListener(new OutputListener() { @Override public void onOutput(String line) { loudnormReport.append(line); } }) .execute(); System.out.println("Loudnorm report:\n" + loudnormReport);

Supplying and consuming data with SeekableByteChannel

Ability to interact with SeekableByteChannel is one of the features, which distinct Jaffree from similar libraries. Under the hood Jaffree uses tiny FTP server to interact with SeekableByteChannel.

See whole example here.

try (SeekableByteChannel inputChannel = Files.newByteChannel(pathToSrc, StandardOpenOption.READ); SeekableByteChannel outputChannel = Files.newByteChannel(pathToDst, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.TRUNCATE_EXISTING) ) { FFmpeg.atPath() .addInput(ChannelInput.fromChannel(inputChannel)) .addOutput(ChannelOutput.toChannel(filename, outputChannel)) .execute(); }

Supplying and consuming data with InputStream and OutputStream

Notice It's recommended to use ChannelInput & ChannelOutput since ffmpeg leverage seeking in input and requires seekable output for many formats.

Under the hood pipes are not OS pipes, but TCP Sockets. This allows much higher bandwidth.

See whole example here.

try (InputStream inputStream = Files.newInputStream(pathToSrc); OutputStream outputStream = Files.newOutputStream(pathToDst, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING) ) { FFmpeg.atPath() .addInput(PipeInput.pumpFrom(inputStream)) .addOutput( PipeOutput.pumpTo(outputStream) .setFormat("flv") ) .execute(); }

Live Stream Re-Streaming (HLS)

See whole example here.

FFmpeg.atPath() .addInput( UrlInput.fromUrl(liveStream) ) .addOutput( UrlOutput.toPath(dir.resolve("index.m3u8")) .setFrameRate(30) // check all available options: ffmpeg -help muxer=hls .setFormat("hls") // enforce keyframe every 2s - see setFrameRate .addArguments("-x264-params", "keyint=60") .addArguments("-hls_list_size", "5") .addArguments("-hls_delete_threshold", "5") .addArguments("-hls_time", "2") .addArguments("-hls_flags", "delete_segments") ) .setOverwriteOutput(true) .execute();

Screen Capture

See whole example here.

FFmpeg.atPath() .addInput(CaptureInput .captureDesktop() .setCaptureFrameRate(30) .setCaptureCursor(true) ) .addOutput(UrlOutput .toPath(pathToVideo) // Record with ultrafast to lower CPU usage .addArguments("-preset", "ultrafast") .setDuration(30, TimeUnit.SECONDS) ) .setOverwriteOutput(true) .execute(); //Re-encode when record is completed to optimize file size Path pathToOptimized = pathToVideo.resolveSibling("optimized-" + pathToVideo.getFileName()); FFmpeg.atPath() .addInput(UrlInput.fromPath(pathToVideo))

编辑推荐精选

潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
商汤小浣熊

商汤小浣熊

最强AI数据分析助手

小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。

imini AI

imini AI

像人一样思考的AI智能体

imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

下拉加载更多