Public Release

This commit is contained in:
Moulberry committed 2024-08-24 04:41:01 +08:00
1 parent 666398e480
commit 61bb497a22
126 files changed
+8435 -1240

No files matched your search

@@ -0,0 +1,87 @@
package com.moulberry.flashback.combo_options;
import org.bytedeco.ffmpeg.avcodec.AVCodec;
import org.bytedeco.ffmpeg.avcodec.AVCodecHWConfig;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacpp.Pointer;
import java.util.ArrayList;
import java.util.List;
public enum AudioCodec implements ComboOption {
AAC("AAC", avcodec.AV_CODEC_ID_AAC),
FLAC("FLAC", avcodec.AV_CODEC_ID_FLAC),
MP3("MP3", avcodec.AV_CODEC_ID_MP3),
OPUS("Opus", avcodec.AV_CODEC_ID_OPUS),
VORBIS("Vorbis", avcodec.AV_CODEC_ID_VORBIS);
private final String text;
private final int codecId;
private String[] encoders;
AudioCodec(String text, int codecId) {
this.text = text;
this.codecId = codecId;
}
@Override
public String text() {
return this.text;
}
public int codecId() {
return this.codecId;
}
public String[] getEncoders() {
if (this.encoders == null) {
List<String> encodersHardware = new ArrayList<>();
List<String> encodersHybrid = new ArrayList<>();
List<String> encodersSoftware = new ArrayList<>();
List<String> encodersAvoid = new ArrayList<>();
try (Pointer pointer = new Pointer()) {
while (true) {
try (AVCodec codec = avcodec.av_codec_iterate(pointer)) {
if (codec == null) {
break;
} else if (codec.id() == this.codecId && avcodec.av_codec_is_encoder(codec) != 0) {
int capabilities = codec.capabilities();
String name = codec.name().getString();
if ((capabilities & avcodec.AV_CODEC_CAP_HARDWARE) != 0) {
encodersHardware.add(name);
} else if ((capabilities & avcodec.AV_CODEC_CAP_HYBRID) != 0 || codecHasHwConfig(codec)) {
encodersHybrid.add(name);
} else if ((capabilities & avcodec.AV_CODEC_CAP_EXPERIMENTAL) != 0) {
encodersAvoid.add(name);
} else {
encodersSoftware.add(name);
}
}
}
}
}
List<String> encoders = new ArrayList<>();
encoders.addAll(encodersHardware);
encoders.addAll(encodersHybrid);
encoders.addAll(encodersSoftware);
encoders.addAll(encodersAvoid);
this.encoders = encoders.toArray(new String[0]);
}
return this.encoders;
}
private static boolean codecHasHwConfig(AVCodec codec) {
try (AVCodecHWConfig config = avcodec.avcodec_get_hw_config(codec, 0)) {
return config != null;
} catch (Throwable t) {
return false;
}
}
}
@@ -3,9 +3,9 @@ package com.moulberry.flashback.combo_options;
public enum Sizing implements ComboOption {
KEEP_ASPECT_RATIO("Keep Aspect Ratio"),
CHANGE_ASPECT_RATIO("Change Aspect Ratio"),
FILL("Fill"),
UNDERLAY("Underlay"),
CHANGE_ASPECT_RATIO("Change Aspect Ratio");
UNDERLAY("Underlay");
private final String text;
@@ -1,18 +1,21 @@
package com.moulberry.flashback.combo_options;
import com.moulberry.flashback.Flashback;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import com.moulberry.flashback.exporting.PixelFormatHelper;
import org.bytedeco.ffmpeg.avcodec.AVCodec;
import org.bytedeco.ffmpeg.avcodec.AVCodecContext;
import org.bytedeco.ffmpeg.avcodec.AVCodecHWConfig;
import org.bytedeco.ffmpeg.avutil.AVDictionary;
import org.bytedeco.ffmpeg.avutil.AVRational;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.IntPointer;
import org.bytedeco.javacpp.Pointer;
import java.util.*;
import static org.bytedeco.ffmpeg.global.avcodec.*;
import static org.bytedeco.ffmpeg.global.avutil.*;
import static org.bytedeco.ffmpeg.global.avutil.av_find_nearest_q_idx;
public enum VideoCodec implements ComboOption {
H264("H264 (AVC)", avcodec.AV_CODEC_ID_H264),
@@ -50,9 +53,20 @@ public enum VideoCodec implements ComboOption {
try (Pointer pointer = new Pointer()) {
while (true) {
try (AVCodec codec = avcodec.av_codec_iterate(pointer)) {
if (codec == null) {
break;
} else if (codec.id() == this.codecId && avcodec.av_codec_is_encoder(codec) != 0) {
try {
if (codec == null) {
break;
}
if (codec.id() != this.codecId) {
continue;
}
if (avcodec.av_codec_is_encoder(codec) == 0) {
continue;
}
if (!doesEncoderWork(codec)) {
continue;
}
int capabilities = codec.capabilities();
String name = codec.name().getString();
@@ -65,6 +79,10 @@ public enum VideoCodec implements ComboOption {
} else {
encodersSoftware.add(name);
}
} finally {
if (codec != null) {
codec.close();
}
}
}
}
@@ -82,12 +100,58 @@ public enum VideoCodec implements ComboOption {
return this.encoders;
}
private static boolean doesEncoderWork(AVCodec codec) {
AVCodecContext codecContext = null;
AVDictionary options = new AVDictionary(null);
try {
if ((codecContext = avcodec.avcodec_alloc_context3(codec)) == null) {
return false;
}
// Setup dummy parameters
codecContext.codec_id(codec.id());
codecContext.codec_type(AVMEDIA_TYPE_VIDEO);
codecContext.bit_rate(400000);
codecContext.width(1920);
codecContext.height(1080);
AVRational frameRate = av_d2q(60.0, 1001000);
AVRational supportedFramerates = codec.supported_framerates();
if (supportedFramerates != null) {
int idx = av_find_nearest_q_idx(frameRate, supportedFramerates);
frameRate = supportedFramerates.position(idx);
}
AVRational time_base = av_inv_q(frameRate);
codecContext.time_base(time_base);
int pixelFormat = PixelFormatHelper.getBestPixelFormat(codec.name().getString());
codecContext.pix_fmt(pixelFormat);
if ((codec.capabilities() & AV_CODEC_CAP_EXPERIMENTAL) != 0) {
codecContext.strict_std_compliance(FF_COMPLIANCE_EXPERIMENTAL);
}
return avcodec.avcodec_open2(codecContext, codec, options) >= 0;
} finally {
if (codecContext != null) {
avcodec.avcodec_close(codecContext);
codecContext.close();
}
avutil.av_dict_free(options);
}
}
private static boolean codecHasHwConfig(AVCodec codec) {
try (AVCodecHWConfig config = avcodec.avcodec_get_hw_config(codec, 0)) {
return config != null;
} catch (Throwable t) {
return false;
}
if (config != null) {
config.close();
return true;
}
} catch (Exception ignored) {}
return false;
}
}
@@ -16,7 +16,8 @@ public enum VideoContainer implements ComboOption {
private final String text;
private final String extension;
private VideoCodec[] supportedCodecs = null;
private VideoCodec[] supportedVideoCodecs = null;
private AudioCodec[] supportedAudioCodecs = null;
VideoContainer(String text, String extension) {
this.text = text;
@@ -32,8 +33,8 @@ public enum VideoContainer implements ComboOption {
return extension;
}
public VideoCodec[] getSupportedCodecs() {
if (this.supportedCodecs == null) {
public VideoCodec[] getSupportedVideoCodecs() {
if (this.supportedVideoCodecs == null) {
List<VideoCodec> supportedCodecs = new ArrayList<>();
try (AVOutputFormat outputFormat = avformat.av_guess_format(this.extension, "test."+this.extension, null)) {
for (VideoCodec codec : VideoCodec.values()) {
@@ -51,9 +52,30 @@ public enum VideoContainer implements ComboOption {
}
}
this.supportedCodecs = supportedCodecs.toArray(new VideoCodec[0]);
this.supportedVideoCodecs = supportedCodecs.toArray(new VideoCodec[0]);
}
return this.supportedCodecs;
return this.supportedVideoCodecs;
}
public AudioCodec[] getSupportedAudioCodecs() {
if (this.supportedAudioCodecs == null) {
List<AudioCodec> supportedCodecs = new ArrayList<>();
try (AVOutputFormat outputFormat = avformat.av_guess_format(this.extension, "test."+this.extension, null)) {
for (AudioCodec codec : AudioCodec.values()) {
if (codec.getEncoders().length == 0) {
continue;
}
int ret = avformat.avformat_query_codec(outputFormat, codec.codecId(), avcodec.FF_COMPLIANCE_NORMAL);
if (ret == 1) {
supportedCodecs.add(codec);
}
}
}
this.supportedAudioCodecs = supportedCodecs.toArray(new AudioCodec[0]);
}
return this.supportedAudioCodecs;
}
}
@@ -1,31 +0,0 @@
package com.moulberry.flashback.combo_options;
public enum VideoPreset implements ComboOption {
ULTRAFAST("Ultra Fast", "ultrafast"),
SUPERFAST("Super Fast", "superfast"),
VERYFAST("Very Fast", "veryfast"),
FASTER("Faster", "faster"),
FAST("Fast", "fast"),
MEDIUM("Medium", "medium"),
SLOW("Slow", "slow"),
SLOWER("Slower", "slower"),
VERYSLOW("Very Slow", "veryslow");
private final String text;
private final String presetId;
VideoPreset(String text, String presetId) {
this.text = text;
this.presetId = presetId;
}
@Override
public String text() {
return this.text;
}
public String presetId() {
return this.presetId;
}
}