Skip to content

Instantly share code, notes, and snippets.

@rishabh9
Created November 6, 2017 14:02
Show Gist options
  • Save rishabh9/58b8a82879706632bb6befe4e7998b4e to your computer and use it in GitHub Desktop.
Save rishabh9/58b8a82879706632bb6befe4e7998b4e to your computer and use it in GitHub Desktop.
Java implementation of transcoding.c and output on running it.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/zzz/movie.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42mp41isomavc1
creation_time : 2017-05-04T16:02:52.000000Z
Duration: 00:02:00.17, start: 0.000000, bitrate: 22217 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 4096x2048 [SAR 1:1 DAR 2:1], 21957 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default)
Metadata:
creation_time : 2017-05-04T16:02:52.000000Z
handler_name : L-SMASH Video Handler
encoder : AVC Coding
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
Metadata:
creation_time : 2017-05-04T16:02:52.000000Z
handler_name : L-SMASH Audio Handler
Exception in thread "main" java.lang.NullPointerException
at org.bytedeco.javacpp.Loader.sizeof(Loader.java:891)
at Tutorial2.isSuccessOpenInputFile(Tutorial2.java:72)
at Tutorial2.main(Tutorial2.java:34)
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.avcodec.av_packet_unref;
import static org.bytedeco.javacpp.avfilter.avfilter_register_all;
import static org.bytedeco.javacpp.avformat.*;
import static org.bytedeco.javacpp.avutil.*;
public class Tutorial2 {
private Tutorial2() {
}
private avformat.AVFormatContext inputAVFormatContext = new avformat.AVFormatContext(null);
private avformat.AVFormatContext outputAVFormatContext = new avformat.AVFormatContext(null);
private final avcodec.AVPacket packet = new avcodec.AVPacket();
private final avutil.AVFrame frame = new avutil.AVFrame();
public static void main(String[] args) {
final Tutorial2 tutorial = new Tutorial2();
if (tutorial.isValidCountOfArguments(args)) {
av_register_all();
avfilter_register_all();
// Create new variables solely for readability.
final String inputFilename = args[0];
final String outputFilename = args[1];
if (!tutorial.isSuccessOpenInputFile(inputFilename)) {
tutorial.gracefulExit();
}
if (!tutorial.isSuccessOpenOutputFile(outputFilename)) {
tutorial.gracefulExit();
}
}
}
private boolean isValidCountOfArguments(String[] args) {
if (args.length != 2) {
String errorMessage = "Usage:\n\tjava Tutorial2 <input_file> <output_file>";
// Log to STD ERR by FFmpeg
av_log(new Pointer(), AV_LOG_ERROR, errorMessage);
return false;
}
return true;
}
private boolean isSuccessOpenInputFile(String inputFilename) {
avformat.AVInputFormat format = null;
avutil.AVDictionary options = null;
int result = avformat_open_input(inputAVFormatContext, inputFilename, format, options);
if (result < 0) {
av_log(null, AV_LOG_ERROR, "Cannot open input file!\n");
return false;
}
result = avformat_find_stream_info(inputAVFormatContext, options);
if (result < 0) {
av_log(null, AV_LOG_ERROR, "Cannot read input stream!\n");
return false;
}
av_dump_format(inputAVFormatContext, 0, inputFilename, 0);
av_mallocz_array(inputAVFormatContext.nb_streams(), Loader.sizeof(StreamContext.class));
//avformat_close_input(inputAVFormatContext);
return true;
}
private boolean isSuccessOpenOutputFile(String outputFilename) {
return false;
}
private void gracefulExit() {
av_packet_unref(packet);
av_frame_free(frame);
}
public static class StreamContext extends Pointer {
static {
Loader.load();
}
public StreamContext() {
super((Pointer) null);
allocate();
}
public StreamContext(long size) {
super((Pointer) null);
allocateArray(size);
}
public StreamContext(Pointer p) {
super(p);
}
private native void allocate();
private native void allocateArray(long size);
@Override
public StreamContext position(long position) {
return (StreamContext) super.position(position);
}
// private avcodec.AVCodecContext decodingContext;
// private avcodec.AVCodecContext encodingContext;
public native avcodec.AVCodecContext dec_ctx();
public native StreamContext dec_ctx(avcodec.AVCodecContext dec_ctx);
public native avcodec.AVCodecContext enc_ctx();
public native StreamContext enc_ctx(avcodec.AVCodecContext enc_ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment