Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/net/bramp/ffmpeg/builder/FFmpegOutputBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.commons.lang3.math.Fraction;

import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class FFmpegOutputBuilder implements Cloneable {
public String video_preset;
public String video_filter;
public String video_filter_complex;
public List<String> video_meta_tags;

public boolean subtitle_enabled = true;

Expand Down Expand Up @@ -223,6 +225,24 @@ public FFmpegOutputBuilder setComplexVideoFilter(String filter) {
return this;
}

/**
* Allows to add metadata to output video. Which keys are possible depends on the used output
* video codec.
*
* @param key Metadata key, e.g. "comment"
* @param value Value to set for key
* @return This instance
*/
public FFmpegOutputBuilder addMetaTag(String key, String value) {
this.video_enabled = true;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with elsewhere, can we do a quick checkNotNull(key), checkNotNull(value)

if (this.video_meta_tags == null) {
this.video_meta_tags = new ArrayList<>();
}

this.video_meta_tags.add(checkNotNull(key) + "=" + checkNotNull(value).replace("\"", ""));
return this;
}

public FFmpegOutputBuilder setAudioCodec(String codec) {
this.audio_enabled = true;
this.audio_codec = checkNotNull(codec);
Expand Down Expand Up @@ -448,6 +468,12 @@ protected List<String> build(int pass) {
args.add("-filter_complex").add(video_filter_complex);
}

if (video_meta_tags != null) {
for (String meta : video_meta_tags) {
args.add("-metadata").add("\"" + meta + "\"");
}
}

} else {
args.add("-vn");
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/bramp/ffmpeg/FFmpegBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ public void testMultipleOutputs() {
"output1", "-s", "640x480", "output2")));
}

/**
* Tests whether setting meta tags works correctly.
*/
@Test
public void testMetaTags() {
FFmpegBuilder builder =
new FFmpegBuilder().setInput("input").addOutput("output").disableAudio().disableSubtitle()
.addMetaTag("comment", "My Comment").addMetaTag("title", "\"Video\"").done();

List<String> args = builder.build();
assertThat(args, is(Arrays.asList("-y", "-v", "error", "-i", "input", "-metadata",
"\"comment=My Comment\"", "-metadata", "\"title=Video\"", "-an", "-sn", "output")));
}

@Test(expected = IllegalArgumentException.class)
public void testNothing() {
FFmpegBuilder builder = new FFmpegBuilder();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/bramp/ffmpeg/FFmpegExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public void testFilter() throws InterruptedException, ExecutionException, IOExce
assertEquals(FFmpegJob.State.FINISHED, job.getState());
}

@Test
public void testMetaTags() throws InterruptedException, ExecutionException, IOException {

FFmpegBuilder builder =
new FFmpegBuilder().setInput(Samples.big_buck_bunny_720p_1mb).overrideOutputFiles(true)
.addOutput(Samples.output_mp4).setFormat("mp4").disableAudio().setVideoCodec("mpeg4")
.addMetaTag("comment", "This=Nice!").addMetaTag("title", "Big Buck Bunny").done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

FFmpegJob job = executor.createJob(builder);
runAndWait(job);

assertEquals(FFmpegJob.State.FINISHED, job.getState());
}

protected void runAndWait(FFmpegJob job) throws ExecutionException, InterruptedException {
Future<?> future = executor.submit(job);

Expand Down