Hello, I am trying to capture the screen with FFmpeg and as a reference I am using Low-Latency-Live-Streaming/capture_screen.c at master · JingyZhu/Low-Latency-Live-Streaming · GitHub.
But now I got the problem that I AVFormatContext is null while execing it while the correspond C code runs fine, and I can not find the source of the problem.
Zig code:
const std = @import("std");
const ffmpeg = @cImport({
@cInclude("ffmpeg4.4/libavformat/avformat.h");
@cInclude("ffmpeg4.4/libavdevice/avdevice.h");
@cInclude("ffmpeg4.4/libavcodec/avcodec.h");
});
pub fn main() !void {
var c_error: c_int = 0;
// const codec_ctx: ?*ffmpeg.AVCodecContext = null;
// const codec: ?*ffmpeg.AVCodec = null;
var options: ?*ffmpeg.AVDictionary = null;
var AVFormatContext = ffmpeg.avformat_alloc_context();
_ = ffmpeg.avformat_network_init();
ffmpeg.avdevice_register_all();
c_error = ffmpeg.av_dict_set(&options, "framerate", "30", 0);
std.debug.print("{any}\n", .{c_error});
//c_error = ffmpeg.av_dict_set(&options, "preset", "medium", 0);
//std.debug.print("{any}\n", .{c_error});
c_error = ffmpeg.av_dict_set(&options, "video_size", "1920*1080", 0);
std.debug.print("size:{any}\n", .{c_error});
const input_format = ffmpeg.av_find_input_format("x11grab");
c_error = ffmpeg.avformat_open_input(&AVFormatContext, ":0.0", input_format, &options);
std.debug.print("{any}\n", .{c_error});
c_error = ffmpeg.avformat_find_stream_info(AVFormatContext, &options);
std.debug.print("info:{any}\n", .{c_error});
std.debug.print("{any}\n", .{AVFormatContext.*.streams[0].*.codecpar.*});
}
C code:
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
int main(int argc, char* argv[])
{
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
char* video_size = "1920*1080";
char* framerate = "30";
// Deprecated
// av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
//Register Device Deprecated
avdevice_register_all();
AVDictionary* options = NULL;
//Set some options
//grabbing frame rate
av_dict_set(&options,"framerate", framerate, 0);
//Make the grabbed area follow the mouse
//av_dict_set(&options,"follow_mouse","centered",0);
//Video frame size. The default is to capture the full screen
av_dict_set(&options, "video_size", video_size, 0);
const AVInputFormat *ifmt=av_find_input_format("x11grab");
//Grab at position 10,20
if(avformat_open_input(&pFormatCtx,":0.0",ifmt,&options)!=0){
fprintf(stderr, "Couldn't open input stream.\n");
return -1;
}
fprintf(stdout, "test %d\n", pFormatCtx->streams[0]->codecpar->codec_type);
if(avformat_find_stream_info(pFormatCtx,NULL)<0){
fprintf(stderr, "Couldn't find stream information.\n");
return -1;
}
}