Skip to content

Instantly share code, notes, and snippets.

@commshare
Created October 15, 2015 03:29
Show Gist options
  • Select an option

  • Save commshare/ea7253ea331fd39d3dc8 to your computer and use it in GitHub Desktop.

Select an option

Save commshare/ea7253ea331fd39d3dc8 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavutil/log.h>
int main(int argc, char *argv[]) {
int r;
assert(argc == 3);
av_register_all();
avformat_network_init();
//av_log_set_level(AV_LOG_DEBUG);
AVFormatContext *in = NULL;
r = avformat_open_input(&in, argv[1], NULL, NULL);
printf("err %d\n", r);
assert(!r);
r = avformat_find_stream_info(in, NULL);
printf("err %d\n", r);
assert(r >= 0);
AVFormatContext *out = avformat_alloc_context();
assert(out);
out->oformat = av_guess_format(NULL, argv[2], NULL);
assert(out->oformat);
r = avio_open2(&out->pb, argv[2], AVIO_FLAG_WRITE, NULL, NULL);
assert(!r);
int i;
for (i = 0; i < in->nb_streams; i++) {
AVStream *inputStream = in->streams[i];
AVStream *newStream = avformat_new_stream(out, inputStream->codec->codec);
assert(newStream);
r = avcodec_copy_context(newStream->codec, inputStream->codec);
assert(!r);
newStream->codec->codec_id = inputStream->codec->codec_id;
if (newStream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
newStream->sample_aspect_ratio = (AVRational){1, 1};
}
}
r = avformat_write_header(out, NULL);
AVPacket pkt;
int64_t frames_in = 0;
while (1) {
av_init_packet(&pkt);
r = av_read_frame(in, &pkt);
if (r) {
printf("read error %d\n", r);
break;
}
printf("stream %d, pts %"PRId64", dts %"PRId64"\n",
pkt.stream_index, pkt.pts, pkt.dts);
frames_in++;
r = av_write_frame(out, &pkt);
if (r && (r != AVERROR(EINVAL))) {
printf("write error %d\n", r);
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment