生成html快照,javascript – 如何从HTML中的MJPEG流创建快照

我有以下HTML网页:

video.cgi

此网页显示IP摄像机流MJPEG数据的馈送.你可以在这里尝试上面的代码:http://jsfiddle.net/jU4aq/(它不适用于IE)

我的问题是如何制作该Feed的快照.基本上我想添加一个按钮,当用户点击它时,会弹出一个对话框,提供保存图像的选项.

最佳答案:

你的流现在似乎没有工作,但尝试一些画布javascript,如:

video.cgi

document.getElementById('save').onclick = function () {

var c = document.createElement('canvas');

var img = document.getElementById('myImage');

c.width = img.width;

c.height = img.height;

var ctx = c.getContext('2d');

ctx.drawImage(img, 0, 0);

//window.location = c.toDataURL('image/png');

window.open(c.toDataURL('image/png'))

};

标签:javascript,canvas,html,mjpeg

来源: https://codeday.me/bug/20190515/1109546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将MJPEG格式的转换为H.264格式的数据,可以借助FFmpeg这个开源的多媒体处理库来实现。下面是一个简单的示例代码,可以实现将MJPEG转换为H.264的功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavutil/imgutils.h> #include <libavutil/opt.h> #include <libavutil/parseutils.h> #include <libavutil/pixdesc.h> #include <libavcodec/avcodec.h> #define INBUF_SIZE 4096 int main(int argc, char **argv) { AVCodec *codec; AVCodecContext *c = NULL; int ret; AVFrame *frame; AVPacket *pkt; uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; uint8_t *data; size_t data_size; FILE *f; int i; if (argc <= 2) { fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]); exit(0); } avcodec_register_all(); codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } c = avcodec_alloc_context3(codec); if (!c) { fprintf(stderr, "Could not allocate video codec context\n"); exit(1); } /* Set the codec context properties */ c->bit_rate = 400000; c->width = 352; c->height = 288; /* Set the frame rate */ AVRational framerate = {25,1}; c->time_base = framerate; c->framerate = framerate; c->gop_size = 10; c->max_b_frames = 1; c->pix_fmt = AV_PIX_FMT_YUV420P; if (avcodec_open2(c, codec, NULL) < 0) { fprintf(stderr, "Could not open codec\n"); exit(1); } /* Allocate the input buffer */ pkt = av_packet_alloc(); if (!pkt) { fprintf(stderr, "Could not allocate packet\n"); exit(1); } /* Open the input file */ f = fopen(argv[1], "rb"); if (!f) { fprintf(stderr, "Could not open %s\n", argv[1]); exit(1); } /* Open the output file */ FILE *outfile = fopen(argv[2], "wb"); if (!outfile) { fprintf(stderr, "Could not open %s\n", argv[2]); exit(1); } /* Allocate the frame */ frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate video frame\n"); exit(1); } /* Fill the input buffer */ data_size = fread(inbuf, 1, INBUF_SIZE, f); data = inbuf; while (data_size > 0) { /* Set the input buffer properties */ AVFrame *frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate video frame\n"); exit(1); } frame->width = c->width; frame->height = c->height; frame->format = c->pix_fmt; int ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, c->pix_fmt, 32); if (ret < 0) { fprintf(stderr, "Could not allocate raw picture buffer\n"); exit(1); } /* Fill the frame with the input buffer data */ av_image_fill_arrays(frame->data, frame->linesize, data, c->pix_fmt, c->width, c->height, 32); /* Encode the frame */ ret = avcodec_send_frame(c, frame); if (ret < 0) { fprintf(stderr, "Error sending a frame for encoding\n"); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(c, pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error during encoding\n"); exit(1); } fwrite(pkt->data, 1, pkt->size, outfile); av_packet_unref(pkt); } data += data_size; data_size = fread(inbuf, 1, INBUF_SIZE, f); } /* Flush the encoder */ ret = avcodec_send_frame(c, NULL); if (ret < 0) { fprintf(stderr, "Error flushing the encoder\n"); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(c, pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error during encoding\n"); exit(1); } fwrite(pkt->data, 1, pkt->size, outfile); av_packet_unref(pkt); } /* Free resources */ fclose(f); fclose(outfile); avcodec_free_context(&c); av_frame_free(&frame); av_packet_free(&pkt); return 0; } ``` 请注意,这个示例代码只是一个简单的转换过程,可能需要根据实际情况进行修改和优化。同时,这个示例代码也不能处理所有的MJPEG,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值