1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| void open_device() { av_log_set_level(AV_LOG_DEBUG); int ret = 0; char *device_name = "0"; AVDictionary *options = NULL; av_dict_set(&options, "video_size", "640x480", 0); av_dict_set(&options, "framerate", "30", 0); av_dict_set(&options, "pixel_format", "nv12", 0); avdevice_register_all(); AVInputFormat *inputFormat = av_find_input_format("avfoundation"); ret = avformat_open_input(&fmt_ctx, device_name, inputFormat, &options); if (ret < 0 || !fmt_ctx) { goto __ERROR; } printf("成功打开视频设备\n"); return; __ERROR: log_error(ret); return; }
void read_video() { if (!fmt_ctx) { printf("不能使用设备\n"); goto __ERROR; } FILE *output_yuv = fopen("/Users/bq/Movies/test/video.yuv", "wb+"); if (!output_yuv) { printf("文件创建失败\n"); } AVPacket pkt; av_init_packet(&pkt); int count = 0; int ret = 0; while ((ret == 0 || ret == AVERROR(EAGAIN)) && count < 100) { ret = av_read_frame(fmt_ctx, &pkt); if (ret == AVERROR(EAGAIN)) continue; printf("[%d] pkt size is %d\n", count, pkt.size); count++;
const size_t y_length = kWidth * kHeight; const size_t u_v_length = y_length / 4; memcpy(frame->data[0], pkt.data, y_length); const int stride = 2; for (int i = 0; i < u_v_length; i++) { const size_t base = y_length + i * stride; frame->data[1][i] = pkt.data[base]; frame->data[2][i] = pkt.data[base + 1]; } fwrite(frame->data[0], 1, y_length, output_yuv); fwrite(frame->data[1], 1, u_v_length, output_yuv); fwrite(frame->data[2], 1, u_v_length, output_yuv); av_packet_unref(&pkt); } printf("完成写入\n"); __ERROR: fclose(output_yuv); }
|