Skip to content

Commit

Permalink
Bug/347 invalid chars in hls time (#348)
Browse files Browse the repository at this point in the history
* fix: Replace timecode string with seconds for hls_time

Fixes: #347
  • Loading branch information
Euklios committed Aug 21, 2024
1 parent 9289e50 commit c932b78
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/main/java/net/bramp/ffmpeg/FFmpegUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public static String toTimecode(long duration, TimeUnit units) {
return ZERO.trimTrailingFrom(String.format("%02d:%02d:%02d.%09d", hours, minutes, seconds, ns));
}

public static String toSeconds(long duration, TimeUnit units) {
// FIXME Negative durations are also supported.
// https://www.ffmpeg.org/ffmpeg-utils.html#Time-duration
checkArgument(duration >= 0, "duration must be positive");

return Float.toString(units.toMillis(duration) / 1_000f);
}

/**
* Returns the number of nanoseconds this timecode represents. The string is expected to be in the
* format "hour:minute:second", where second can be a decimal number.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.ImmutableList;
import javax.annotation.CheckReturnValue;
import static com.google.common.base.Preconditions.*;
import static net.bramp.ffmpeg.FFmpegUtils.toSeconds;
import static net.bramp.ffmpeg.FFmpegUtils.toTimecode;
import static net.bramp.ffmpeg.Preconditions.checkNotEmpty;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -106,7 +107,7 @@ public FFmpegHlsOutputBuilder setHlsBaseUrl(String baseurl) {
protected void addFormatArgs(ImmutableList.Builder<String> args) {
super.addFormatArgs(args);
if (hls_time != null) {
args.add("-hls_time", toTimecode(hls_time, TimeUnit.MILLISECONDS));
args.add("-hls_time", toSeconds(hls_time, TimeUnit.MILLISECONDS));
}

if (!Strings.isNullOrEmpty(hls_segment_filename)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.google.common.collect.ImmutableList;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import net.bramp.ffmpeg.FFmpeg;
import net.bramp.ffmpeg.FFprobe;
import net.bramp.ffmpeg.fixtures.Samples;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
Expand All @@ -33,7 +37,7 @@ public void testAddHlsOutput() {
.done()
.build();

assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input", "-f", "hls", "-hls_time", "00:00:00.005",
assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input", "-f", "hls", "-hls_time", "0.005",
"-hls_segment_filename", "file%03d.ts", "-hls_init_time", "00:00:00.003",
"-hls_list_size", "3", "-hls_base_url", "test1234/", "output.m3u8"), args);
}
Expand All @@ -54,16 +58,14 @@ public void mixedHlsAndDefault() {
.done()
.build();

assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input","-f","hls","-b:v","3","-vf","TEST","-hls_time", "00:00:00.005",
assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input","-f","hls","-b:v","3","-vf","TEST","-hls_time", "0.005",
"-hls_segment_filename", "file%03d.ts", "-hls_init_time", "00:00:00.003",
"-hls_list_size", "3", "-hls_base_url", "test1234/", "output.m3u8"), args);
}

@Test
public void testConvertVideoToHls() throws IOException {
Files.createDirectories(Paths.get("tmp/"));
Files.deleteIfExists(Paths.get("tmp/output.m3u8"));
Files.deleteIfExists(Paths.get("tmp/file000.m3u8"));
cleanupTmp();

List<String> command = new FFmpegBuilder()
.setInput(Samples.TEST_PREFIX + Samples.base_big_buck_bunny_720p_1mb)
Expand All @@ -82,4 +84,64 @@ public void testConvertVideoToHls() throws IOException {
assertTrue(Files.exists(Paths.get("tmp/output.m3u8")));
assertTrue(Files.exists(Paths.get("tmp/file000.ts")));
}

@Test
public void testConvertVideoToHlsFileSecondDuration() throws IOException {
cleanupTmp();

List<String> command = new FFmpegBuilder()
.setInput(Samples.TEST_PREFIX + Samples.base_big_buck_bunny_720p_1mb)
.addHlsOutput("tmp/output.m3u8")
.setHlsTime(1, TimeUnit.SECONDS)
.setHlsListSize(0)
.setHlsSegmentFileName("tmp/file%03d.ts")
.addExtraArgs("-force_key_frames", "expr:gte(t,n_forced)")
.done()
.build();

new FFmpeg().run(command);

FFmpegProbeResult probe = new FFprobe().probe("tmp/file000.ts");

assertEquals(probe.getStreams().get(0).getDuration(), 1, 0.1);
}

@Test
public void testConvertVideoToHlsFileMillisDuration() throws IOException {
cleanupTmp();

List<String> command = new FFmpegBuilder()
.setInput(Samples.TEST_PREFIX + Samples.base_big_buck_bunny_720p_1mb)
.addHlsOutput("tmp/output.m3u8")
.setHlsTime(500, TimeUnit.MILLISECONDS)
.setHlsListSize(0)
.setHlsSegmentFileName("tmp/file%03d.ts")
.setFrames(30)
.addExtraArgs("-g", "5")
.done()
.build();

new FFmpeg().run(command);

FFmpegProbeResult probe = new FFprobe().probe("tmp/file000.ts");

assertEquals(0.5, probe.getStreams().get(0).getDuration(), 0.1);
}

private void cleanupTmp() throws IOException {
Path tmpFolder = Paths.get("tmp/");
if (!Files.exists(tmpFolder)) {
Files.createDirectory(tmpFolder);
return;
}


try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(tmpFolder)) {
for (Path sub : directoryStream) {
if (!tmpFolder.equals(sub)) {
Files.deleteIfExists(sub);
}
}
}
}
}

0 comments on commit c932b78

Please sign in to comment.