基於live555實現即時視頻監控

來源:互聯網
上載者:User

目錄

1 所需軟體、源碼及下載地址----------------------------------------------------------------------------------------------------------------------------------- 1

2安裝軟體及編譯------------------------------------------------------------------------------------------------------------------------------------------------------ 1

2.1 安裝Windows版的VLC播放器(勾選上外掛程式,預設已經勾選)--------------------------------------------------------------------- 1

2.2 編譯live555-------------------------------------------------------------------------------------------------------------------------------------------------- 1

2.3 編譯ffmpeg-------------------------------------------------------------------------------------------------------------------------------------------------- 1

2.3.1 編譯yasm------------------------------------------------------------------------------------------------------------------------------------------- 2

2.3.2 編譯v4l-utils--------------------------------------------------------------------------------------------------------------------------------------- 2

2.3.3 編譯libx264---------------------------------------------------------------------------------------------------------------------------------------- 2

2.3.4 編譯ffmpeg---------------------------------------------------------------------------------------------------------------------------------------- 2

3伺服器程式------------------------------------------------------------------------------------------------------------------------------------------------------------- 2

3.1 源碼------------------------------------------------------------------------------------------------------------------------------------------------------------- 2

3.2 編譯步驟------------------------------------------------------------------------------------------------------------------------------------------------------ 6

4 html頁面源碼--------------------------------------------------------------------------------------------------------------------------------------------------------- 6

5 運行---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 7

 

 

 

QQ:809205580

部落格地址: http://blog.csdn.net/zjhsucceed_329

QCW(Qt自訂控制項)下載地址: https://sourceforge.net/projects/qcw/files/?source=navbar

  1 所需軟體、源碼及下載地址

VLC播放器:http://www.videolan.org/

live555-latest.tar.gz:http://www.live555.com/liveMedia/public/

ffmpeg:http://ffmpeg.org/olddownload.html、

yasm:http://yasm.tortall.net/Download.html

v4l-utils:http://www.linuxtv.org/downloads/v4l-utils/

libx264:ftp://ftp.videolan.org/pub/videolan/x264/snapshots/ 2安裝軟體及編譯 2.1 安裝Windows版的VLC播放器(勾選上外掛程式,預設已經勾選) 2.2 編譯live555

root@zjh:/home/work#tar zxvf live555-latest.tar.gz

root@zjh:/home/work#cd live

root@zjh:/home/work/live#./genMakefiles linux

root@zjh:/home/work/live#make

root@zjh:/home/work/live#ls mediaServer/

live555MediaServer 2.3 編譯ffmpeg

編譯說明文檔下載地址:http://yunpan.cn/QD8RRSPCIV4aS 2.3.1 編譯yasm 2.3.2 編譯v4l-utils 2.3.3 編譯libx264 2.3.4 編譯ffmpeg 3伺服器程式 3.1 源碼

encoding.c

#include<math.h>

 

#include<libavutil/opt.h>

#include<libavcodec/avcodec.h>

#include<libavutil/imgutils.h>

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>

#include<sys/stat.h>

#include<errno.h>

#include<signal.h>

 

intstop = 0;

constchar *FIFO = "test.264";

 

/*

 * Video encoding example

 */

staticvoid video_encode_example(const char *filename, int codec_id)

{

    AVCodec *codec;

    AVCodecContext *c= NULL;

    int i, ret, x, y, got_output;

    FILE *f;

    AVFrame *frame;

    AVPacket pkt;

    uint8_t endcode[] = { 0, 0, 1, 0xb7 };

 

    printf("Encode video file %s\n",filename);

 

    /* find the mpeg1 video encoder */

    codec = avcodec_find_encoder(codec_id);

    if (!codec)

         {

        fprintf(stderr, "Codec notfound\n");

        exit(1);

    }

 

    c = avcodec_alloc_context3(codec);

    if (!c)

         {

        fprintf(stderr, "Could notallocate video codec context\n");

        exit(1);

    }

 

    /* put sample parameters */

    c->bit_rate = 400000;

    /* resolution must be a multiple of two */

    c->width = 352;

    c->height = 288;

    /* frames per second */

    c->time_base= (AVRational){1,25};

    c->gop_size = 10; /* emit one intraframe every ten frames */

    c->max_b_frames=1;

    c->pix_fmt = AV_PIX_FMT_YUV420P;

 

    if(codec_id == AV_CODEC_ID_H264)

        av_opt_set(c->priv_data,"preset", "slow", 0);

 

    /* open it */

    if (avcodec_open2(c, codec, NULL) < 0)

         {

        fprintf(stderr, "Could not opencodec\n");

        exit(1);

    }

 

    f = fopen(filename, "w");

    if (!f)

         {

        fprintf(stderr, "Could not open%s\n", filename);

        exit(1);

    }

 

    frame = avcodec_alloc_frame();

    if (!frame)

         {

        fprintf(stderr, "Could notallocate video frame\n");

        exit(1);

    }

    frame->format = c->pix_fmt;

    frame->width  = c->width;

    frame->height = c->height;

 

    /* the image can be allocated by any meansand av_image_alloc() is

     * just the most convenient way ifav_malloc() is to be used */

    ret = av_image_alloc(frame->data,frame->linesize, c->width, c->height, c->pix_fmt, 32);

    if (ret < 0)

         {

        fprintf(stderr, "Could notallocate raw picture buffer\n");

        exit(1);

    }

 

    /* encode time second of video */

         i = 0;

    while (!stop)

         {

        av_init_packet(&pkt);

        pkt.data = NULL;    // packet data will be allocated by theencoder

        pkt.size = 0;

 

        fflush(stdout);

        /* prepare a dummy image */

        /* Y */

        for(y=0;y<c->height;y++) {

            for(x=0;x<c->width;x++) {

                frame->data[0][y *frame->linesize[0] + x] = x + y + i * 3;

            }

        }

 

        /* Cb and Cr */

        for(y=0;y<c->height/2;y++) {

            for(x=0;x<c->width/2;x++) {

                frame->data[1][y *frame->linesize[1] + x] = 128 + y + i * 2;

                frame->data[2][y *frame->linesize[2] + x] = 64 + x + i * 5;

            }

        }

 

        frame->pts = i;

 

        /* encode the image */

        ret = avcodec_encode_video2(c,&pkt, frame, &got_output);

        if (ret < 0) {

            fprintf(stderr, "Errorencoding frame\n");

            exit(1);

        }

 

        if (got_output) {

            printf("Write frame %3d (size=%5d)\n",i, pkt.size);

            fwrite(pkt.data, 1, pkt.size, f);

            av_free_packet(&pkt);

        }

                   i++;

    }

 

    /* get the delayed frames */

    for (got_output = 1; got_output; i++) {

        fflush(stdout);

 

        ret = avcodec_encode_video2(c,&pkt, NULL, &got_output);

        if (ret < 0) {

            fprintf(stderr, "Errorencoding frame\n");

            exit(1);

        }

 

        if (got_output) {

            printf("Write frame %3d(size=%5d)\n", i, pkt.size);

            fwrite(pkt.data, 1, pkt.size, f);

            av_free_packet(&pkt);

        }

    }

 

    /* add sequence end code to have a realmpeg file */

    fwrite(endcode, 1, sizeof(endcode), f);

    fclose(f);

 

    avcodec_close(c);

    av_free(c);

    av_freep(&frame->data[0]);

    avcodec_free_frame(&frame);

    printf("\n");

}

 

voidhandle(int sig)

{

         if (sig == SIGINT)

                   stop = 1;

}

 

intmain(int argc, char **argv)

{       

         signal(SIGINT, handle);

         signal(SIGPIPE, SIG_IGN);

 

    /* register all the codecs */

    avcodec_register_all();

 

         if (mkfifo(FIFO, 0644)< 0)

         {

                   if (EEXIST != errno)

                   {

                            perror("mkfifo");

                            exit(1);

                   }

         }

 

    video_encode_example(FIFO,AV_CODEC_ID_H264);

 

    return 0;

} 3.2 編譯步驟

將encoding.c拷貝到ffmpeg源碼目錄的ffmpeg-1.1/doc/examples目錄下,進入該目錄,修改Makefile

EXAMPLES=       decoding_encoding                  \  

                demuxing                           \  

                filtering_video                    \  

                filtering_audio                    \  

                metadata                           \  

                muxing                             \  

                resampling_audio                   \  

                scaling_video                      \  

                encoding

然後執行make將產生可執行檔encoding 4 html頁面源碼

vod.html

<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml" >

<head>

<metahttp-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<script>

         var itemId = 0;

         function getVLC(name)

         {

                   if(window.document[name])   

                   {

                            returnwindow.document[name];

                   }

                   if(navigator.appName.indexOf("Microsoft Internet")== -1)

                   {

                            if (document.embeds&& document.embeds[name])

                                     return document.embeds[name];   

                   }

                   else

                   {

                            returndocument.getElementById(name);

                   }

         }

 

         function doAddPlaylist()

         {

                   var vlc =getVLC("vlc");

           &nbs

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.