GeezerVison is showing github.com — Adding fps to SequenceEncoder · codice/jcodec@b94a11f
File treeExpand file treeCollapse file tree Open diff view settings android/src/main/org/jcodec/api/android javase/src/main/java/org/jcodec/api/awt samples/main/java/org/jcodec/samples
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: android/src/main/org/jcodec/api/android/AndroidSequenceEncoder8Bit.java +22-5Lines changed: 22 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | import org.jcodec.api.SequenceEncoder8Bit; | 6 | 6 | import org.jcodec.common.io.NIOUtils; | 7 | 7 | import org.jcodec.common.io.SeekableByteChannel; | | 8 | +import org.jcodec.common.model.Rational; | 8 | 9 | import org.jcodec.scale.BitmapUtil; | 9 | 10 |
| 10 | 11 | import java.io.File; | | 18 | 19 | * | 19 | 20 | */ | 20 | 21 | public class AndroidSequenceEncoder8Bit extends SequenceEncoder8Bit { | 21 | | - | 22 | | - public static AndroidSequenceEncoder8Bit createSequenceEncoder8Bit(File out) throws IOException { | 23 | | - return new AndroidSequenceEncoder8Bit(NIOUtils.writableChannel(out)); | | 22 | + | | 23 | + public static AndroidSequenceEncoder8Bit createSequenceEncoder8Bit(File out, int fps) throws IOException { | | 24 | + return new AndroidSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(fps, 1)); | | 25 | + } | | 26 | + | | 27 | + public static AndroidSequenceEncoder8Bit create25Fps(File out) throws IOException { | | 28 | + return new AndroidSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(25, 1)); | | 29 | + } | | 30 | + | | 31 | + public static AndroidSequenceEncoder8Bit create30Fps(File out) throws IOException { | | 32 | + return new AndroidSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(30, 1)); | | 33 | + } | | 34 | + | | 35 | + public static AndroidSequenceEncoder8Bit create2997Fps(File out) throws IOException { | | 36 | + return new AndroidSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(30000, 1001)); | | 37 | + } | | 38 | + | | 39 | + public static AndroidSequenceEncoder8Bit create24Fps(File out) throws IOException { | | 40 | + return new AndroidSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(24, 1)); | 24 | 41 | } | 25 | 42 |
| 26 | | - public AndroidSequenceEncoder8Bit(SeekableByteChannel ch) throws IOException { | 27 | | - super(ch); | | 43 | + public AndroidSequenceEncoder8Bit(SeekableByteChannel ch, Rational fps) throws IOException { | | 44 | + super(ch, fps); | 28 | 45 | } | 29 | 46 |
| 30 | 47 | public void encodeImage(Bitmap bi) throws IOException { | |
Collapse file Copy file name to clipboardExpand all lines: javase/src/main/java/org/jcodec/api/awt/AWTSequenceEncoder8Bit.java +25-8Lines changed: 25 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package org.jcodec.api.awt; | 2 | 2 |
| | 3 | +import java.awt.image.BufferedImage; | | 4 | +import java.io.File; | | 5 | +import java.io.IOException; | | 6 | + | 3 | 7 | import org.jcodec.api.SequenceEncoder8Bit; | 4 | 8 | import org.jcodec.common.io.NIOUtils; | 5 | 9 | import org.jcodec.common.io.SeekableByteChannel; | | 10 | +import org.jcodec.common.model.Rational; | 6 | 11 | import org.jcodec.scale.AWTUtil; | 7 | 12 |
| 8 | | -import java.awt.image.BufferedImage; | 9 | | -import java.io.File; | 10 | | -import java.io.IOException; | 11 | | - | 12 | 13 | /** | 13 | 14 | * This class is part of JCodec ( www.jcodec.org ) This software is distributed | 14 | 15 | * under FreeBSD License | | 18 | 19 | */ | 19 | 20 | public class AWTSequenceEncoder8Bit extends SequenceEncoder8Bit { | 20 | 21 |
| 21 | | - public AWTSequenceEncoder8Bit(SeekableByteChannel out) throws IOException { | 22 | | - super(out); | | 22 | + public AWTSequenceEncoder8Bit(SeekableByteChannel out, Rational fps) throws IOException { | | 23 | + super(out, fps); | 23 | 24 | } | 24 | 25 |
| 25 | | - public static AWTSequenceEncoder8Bit createSequenceEncoder8Bit(File out) throws IOException { | 26 | | - return new AWTSequenceEncoder8Bit(NIOUtils.writableChannel(out)); | | 26 | + public static AWTSequenceEncoder8Bit createSequenceEncoder8Bit(File out, int fps) throws IOException { | | 27 | + return new AWTSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(fps, 1)); | 27 | 28 | } | 28 | 29 |
| | 30 | + public static AWTSequenceEncoder8Bit create25Fps(File out) throws IOException { | | 31 | + return new AWTSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(25, 1)); | | 32 | + } | | 33 | + | | 34 | + public static AWTSequenceEncoder8Bit create30Fps(File out) throws IOException { | | 35 | + return new AWTSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(30, 1)); | | 36 | + } | | 37 | + | | 38 | + public static AWTSequenceEncoder8Bit create2997Fps(File out) throws IOException { | | 39 | + return new AWTSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(30000, 1001)); | | 40 | + } | | 41 | + | | 42 | + public static AWTSequenceEncoder8Bit create24Fps(File out) throws IOException { | | 43 | + return new AWTSequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(24, 1)); | | 44 | + } | | 45 | + | 29 | 46 | public void encodeImage(BufferedImage bi) throws IOException { | 30 | 47 | encodeNativeFrame(AWTUtil.fromBufferedImageRGB8Bit(bi)); | 31 | 48 | } | |
Collapse file Copy file name to clipboardExpand all lines: samples/main/java/org/jcodec/samples/api/SequenceEncoderDemo.java +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | import org.jcodec.api.awt.AWTSequenceEncoder8Bit; | 4 | 4 | import org.jcodec.common.io.FileChannelWrapper; | 5 | 5 | import org.jcodec.common.io.NIOUtils; | | 6 | +import org.jcodec.common.model.Rational; | 6 | 7 | import org.jcodec.common.tools.MainUtils; | 7 | 8 | import org.jcodec.common.tools.MainUtils.Cmd; | 8 | 9 |
| | 23 | 24 | public class SequenceEncoderDemo { | 24 | 25 | private static final String FLAG_NUM_FRAMES = "num-frames"; | 25 | 26 | private static final String FLAG_IN_PATTERN = "in-pattern"; | | 27 | + private static final String FLAG_FPS = "fps"; | 26 | 28 |
| 27 | 29 | public static void main(String[] args) throws IOException { | 28 | 30 | Cmd cmd = MainUtils.parseArguments(args); | @@ -31,18 +33,21 @@ public static void main(String[] args) throws IOException { | 31 | 33 | { | 32 | 34 | put(FLAG_NUM_FRAMES, "Maximum frames to encode."); | 33 | 35 | put(FLAG_IN_PATTERN, "Output folder for the frames."); | | 36 | + put(FLAG_FPS, | | 37 | + "Rational fps, i.e 25:1 (for 25fps), 30:1 (for 30fps), 30000:1001 (for 29.97fps), etc."); | 34 | 38 | } | 35 | 39 | }, "output file name"); | 36 | 40 | return; | 37 | 41 | } | 38 | 42 |
| 39 | 43 | int maxFrames = cmd.getIntegerFlagD(FLAG_NUM_FRAMES, Integer.MAX_VALUE); | | 44 | + String fpsRaw = cmd.getStringFlagD(FLAG_FPS, "25:1"); | 40 | 45 | String outDir = cmd.getStringFlagD(FLAG_IN_PATTERN, | 41 | 46 | new File(System.getProperty("user.home"), "frame%08d.jpg").getAbsolutePath()); | 42 | 47 | FileChannelWrapper out = null; | 43 | 48 | try { | 44 | 49 | out = NIOUtils.writableChannel(MainUtils.tildeExpand(cmd.getArg(0))); | 45 | | - AWTSequenceEncoder8Bit encoder = new AWTSequenceEncoder8Bit(out); | | 50 | + AWTSequenceEncoder8Bit encoder = new AWTSequenceEncoder8Bit(out, Rational.parse(fpsRaw)); | 46 | 51 |
| 47 | 52 | for (int i = 0; i < maxFrames; i++) { | 48 | 53 | File file = new File(String.format(outDir, i)); | |
Collapse file Copy file name to clipboardExpand all lines: samples/main/java/org/jcodec/samples/gen/SequenceEncoderDemo.java +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | import java.io.IOException; | 8 | 8 | import java.util.HashMap; | 9 | 9 |
| 10 | | -import static org.jcodec.api.awt.AWTSequenceEncoder8Bit.createSequenceEncoder8Bit; | 11 | | - | 12 | 10 | import org.jcodec.api.awt.AWTSequenceEncoder8Bit; | 13 | 11 | import org.jcodec.common.tools.MainUtils; | 14 | 12 | import org.jcodec.common.tools.MainUtils.Cmd; | @@ -37,7 +35,7 @@ public static void main(String[] args) throws IOException { | 37 | 35 | final int speed = 4; | 38 | 36 | final int ballSize = 40; | 39 | 37 |
| 40 | | - AWTSequenceEncoder8Bit enc = createSequenceEncoder8Bit(new File(cmd.getArg(0))); | | 38 | + AWTSequenceEncoder8Bit enc = AWTSequenceEncoder8Bit.create25Fps(new File(cmd.getArg(0))); | 41 | 39 | enc.getEncoder().setKeyInterval(25); | 42 | 40 | int framesToEncode = cmd.getIntegerFlagD("n-frames", 100); | 43 | 41 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/api/SequenceEncoder8Bit.java +34-8Lines changed: 34 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | import org.jcodec.common.io.SeekableByteChannel; | 14 | 14 | import org.jcodec.common.model.ColorSpace; | 15 | 15 | import org.jcodec.common.model.Picture8Bit; | | 16 | +import org.jcodec.common.model.Rational; | 16 | 17 | import org.jcodec.containers.mp4.Brand; | 17 | 18 | import org.jcodec.containers.mp4.MP4Packet; | 18 | 19 | import org.jcodec.containers.mp4.TrackType; | @@ -41,19 +42,38 @@ public class SequenceEncoder8Bit { | 41 | 42 | private MP4Muxer muxer; | 42 | 43 | private ByteBuffer sps; | 43 | 44 | private ByteBuffer pps; | | 45 | + private int timestamp; | | 46 | + private Rational fps; | 44 | 47 |
| 45 | | - public static SequenceEncoder8Bit createSequenceEncoder8Bit(File out) throws IOException { | 46 | | - return new SequenceEncoder8Bit(NIOUtils.writableChannel(out)); | | 48 | + public static SequenceEncoder8Bit createSequenceEncoder8Bit(File out, int fps) throws IOException { | | 49 | + return new SequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(fps, 1)); | 47 | 50 | } | 48 | 51 |
| 49 | | - public SequenceEncoder8Bit(SeekableByteChannel ch) throws IOException { | | 52 | + public static SequenceEncoder8Bit create25Fps(File out) throws IOException { | | 53 | + return new SequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(25, 1)); | | 54 | + } | | 55 | + | | 56 | + public static SequenceEncoder8Bit create30Fps(File out) throws IOException { | | 57 | + return new SequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(30, 1)); | | 58 | + } | | 59 | + | | 60 | + public static SequenceEncoder8Bit create2997Fps(File out) throws IOException { | | 61 | + return new SequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(30000, 1001)); | | 62 | + } | | 63 | + | | 64 | + public static SequenceEncoder8Bit create24Fps(File out) throws IOException { | | 65 | + return new SequenceEncoder8Bit(NIOUtils.writableChannel(out), Rational.R(24, 1)); | | 66 | + } | | 67 | + | | 68 | + public SequenceEncoder8Bit(SeekableByteChannel ch, Rational fps) throws IOException { | 50 | 69 | this.ch = ch; | | 70 | + this.fps = fps; | 51 | 71 |
| 52 | 72 | // Muxer that will store the encoded frames | 53 | 73 | muxer = MP4Muxer.createMP4Muxer(ch, Brand.MP4); | 54 | 74 |
| 55 | 75 | // Add video track to muxer | 56 | | - outTrack = muxer.addTrack(TrackType.VIDEO, 25); | | 76 | + outTrack = muxer.addTrack(TrackType.VIDEO, fps.getNum()); | 57 | 77 |
| 58 | 78 | // Allocate a buffer big enough to hold output frames | 59 | 79 | _out = ByteBuffer.allocate(1920 * 1080 * 6); | @@ -68,9 +88,13 @@ public SequenceEncoder8Bit(SeekableByteChannel ch) throws IOException { | 68 | 88 | // MP4 | 69 | 89 | spsList = new ArrayList<ByteBuffer>(); | 70 | 90 | ppsList = new ArrayList<ByteBuffer>(); | 71 | | - | 72 | 91 | } | 73 | | - | | 92 | + | | 93 | + /** | | 94 | + * Encodes a frame into a movie. | | 95 | + * @param pic | | 96 | + * @throws IOException | | 97 | + */ | 74 | 98 | public void encodeNativeFrame(Picture8Bit pic) throws IOException { | 75 | 99 | if (toEncode == null) { | 76 | 100 | toEncode = Picture8Bit.create(pic.getWidth(), pic.getHeight(), encoder.getSupportedColorSpaces()[0]); | @@ -97,8 +121,10 @@ public void encodeNativeFrame(Picture8Bit pic) throws IOException { | 97 | 121 | pps = ppsList.get(0); | 98 | 122 |
| 99 | 123 | // Add packet to video track | 100 | | - outTrack.addFrame(MP4Packet.createMP4Packet(result, frameNo, 25, 1, frameNo, nu.type == NALUnitType.IDR_SLICE, null, 0, frameNo, 0)); | 101 | | - | | 124 | + outTrack.addFrame(MP4Packet.createMP4Packet(result, timestamp, fps.getNum(), fps.getDen(), frameNo, | | 125 | + nu.type == NALUnitType.IDR_SLICE, null, 0, timestamp, 0)); | | 126 | + | | 127 | + timestamp += fps.getDen(); | 102 | 128 | frameNo++; | 103 | 129 | } | 104 | 130 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/common/model/Rational.java +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,8 @@ public int getDen() { | 45 | 45 |
| 46 | 46 | public static Rational parse(String string) { | 47 | 47 | String[] split = splitS(string, ":"); | 48 | | - return new Rational(Integer.parseInt(split[0]), Integer.parseInt(split[1])); | | 48 | + return split.length > 1 ? Rational.R(Integer.parseInt(split[0]), Integer.parseInt(split[1])) : Rational.R( | | 49 | + Integer.parseInt(string), 1); | 49 | 50 | } | 50 | 51 |
| 51 | 52 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/jcodec/common/model/RationalLarge.java +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,8 @@ public long getDen() { | 34 | 34 |
| 35 | 35 | public static RationalLarge parse(String string) { | 36 | 36 | String[] split = splitS(string, ":"); | 37 | | - return new RationalLarge(Long.parseLong(split[0]), Long.parseLong(split[1])); | | 37 | + return split.length > 1 ? RationalLarge.R(Long.parseLong(split[0]), Long.parseLong(split[1])) : RationalLarge | | 38 | + .R(Long.parseLong(string), 1); | 38 | 39 | } | 39 | 40 |
| 40 | 41 | @Override | |
|