Skip to content

Revert "Bug/347 invalid chars in hls time (#348)" #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
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
8 changes: 0 additions & 8 deletions src/main/java/net/bramp/ffmpeg/FFmpegUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ 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,7 +4,6 @@
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 @@ -107,7 +106,7 @@ public FFmpegHlsOutputBuilder setHlsBaseUrl(String baseurl) {
protected void addFormatArgs(ImmutableList.Builder<String> args) {
super.addFormatArgs(args);
if (hls_time != null) {
args.add("-hls_time", toSeconds(hls_time, TimeUnit.MILLISECONDS));
args.add("-hls_time", toTimecode(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,16 +2,12 @@

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 @@ -37,7 +33,7 @@ public void testAddHlsOutput() {
.done()
.build();

assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input", "-f", "hls", "-hls_time", "0.005",
assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input", "-f", "hls", "-hls_time", "00:00:00.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 @@ -58,14 +54,16 @@ public void mixedHlsAndDefault() {
.done()
.build();

assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input","-f","hls","-b:v","3","-vf","TEST","-hls_time", "0.005",
assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input","-f","hls","-b:v","3","-vf","TEST","-hls_time", "00:00:00.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 {
cleanupTmp();
Files.createDirectories(Paths.get("tmp/"));
Files.deleteIfExists(Paths.get("tmp/output.m3u8"));
Files.deleteIfExists(Paths.get("tmp/file000.m3u8"));

List<String> command = new FFmpegBuilder()
.setInput(Samples.TEST_PREFIX + Samples.base_big_buck_bunny_720p_1mb)
Expand All @@ -84,64 +82,4 @@ 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);
}
}
}
}
}
Loading