public VideoWriter(string filename) { Filename = filename; videoStream = new OutputStream(); audioStream = new OutputStream(); }
private void AddStream(ref OutputStream stream, ref AVCodec *codec, AVCodecID codecId) { AVCodecContext *c; codec = avcodec_find_encoder(codecId); if (codec == null) { throw new NotSupportedException("No encoder for codec " + avcodec_get_name(codecId)); } stream.st = avformat_new_stream(ctx, null); if (stream.st == null) { throw new FFmpegException("Could not allocate stream"); } stream.st->id = (int)(ctx->nb_streams - 1); c = avcodec_alloc_context3(codec); if (c == null) { throw new FFmpegException("Could not allocate encoding context"); } stream.enc = c; if (codec->type == AVMediaType.AVMEDIA_TYPE_AUDIO) { c->sample_fmt = codec->sample_fmts != null ? codec->sample_fmts[0] : AVSampleFormat.AV_SAMPLE_FMT_FLTP; c->bit_rate = AudioBitrate; c->sample_rate = AudioSampleRate; c->channels = av_get_channel_layout_nb_channels(c->channel_layout); c->channel_layout = AV_CH_LAYOUT_STEREO; if (codec->channel_layouts != null) { c->channel_layout = codec->channel_layouts[0]; for (int i = 0; codec->channel_layouts[i] != 0; i++) { if (codec->channel_layouts[i] == AV_CH_LAYOUT_STEREO) { c->channel_layout = AV_CH_LAYOUT_STEREO; } } } c->channels = av_get_channel_layout_nb_channels(c->channel_layout); stream.st->time_base = new AVRational() { num = 1, den = c->sample_rate }; } else if (codec->type == AVMediaType.AVMEDIA_TYPE_VIDEO) { c->codec_id = codecId; c->bit_rate = VideoBitrate; c->width = Width; c->height = Height; stream.st->time_base = new AVRational() { num = 1, den = Fps }; c->time_base = stream.st->time_base; c->gop_size = 12; c->pix_fmt = AVPixelFormat.AV_PIX_FMT_YUV420P; if (c->codec_id == AVCodecID.AV_CODEC_ID_MPEG1VIDEO) { c->mb_decision = 2; } } if ((ctx->oformat->flags & AVFMT_GLOBALHEADER) != 0) { c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } }