分享

基于QT视频软件的开发和学习

 xmule 2012-07-21

马上工作需要做视频软件了,所以准备学习QT来开发,为什么选择QT呢,因为他优点多多(不说了自己网上g一下

        随着高清的不断普及,所有视频软件都向高清这个方向而前进,其中有一款开源跨平台的播放器vlc的特性和功能都是非

常的好,所以想准备学习vlc的源码来看看,在看他源码的时候,发现他还有一个可供开发人员调用的libvlc 媒体库可用(GPL),而vlc本身使用的GUI就是QT。

        好了不多说了

        先装好QT SDK,然后把环境变量设置一下(不会去G!)

        然后把vlc库加入到mingw里面

        先复制vlc-include.rar解压出来的头文件到Qt\2009.04\mingw\include里面

        在把vlc-lib.rar解压出来的文件放到C:\vlc-lib

        我们在 Qt Creator 里面建立一个空的QT项目

        新建一个Player类 ,会多出player.cpp,player.h 修改成如下

player.cpp

Cpp代码  收藏代码
  1. /*  
  2.  * Flie Name  player.cpp 
  3.  */  
  4. #include "player.h"  
  5.   
  6. #include <QVBoxLayout>  
  7. #include <QPushButton>  
  8. #include <QSlider>  
  9. #include <QTimer>  
  10. #include <QFrame>  
  11.   
  12. Player::Player()  
  13. : QWidget()  
  14. {  
  15.     //preparation of the vlc command  
  16.     const char * const vlc_args[] = {  
  17.               "-I""dummy"/* Don't use any interface */  
  18.               "--ignore-config"/* Don't use VLC's config */  
  19.               "--extraintf=logger"//log anything  
  20.               "--verbose=2"//be much more verbose then normal for debugging purpose  
  21.               "--plugin-path=C:\\vlc-0.9.9-win32\\plugins\\" };  
  22.   
  23.     _videoWidget=new QFrame(this);  
  24.   
  25.     _volumeSlider=new QSlider(Qt::Horizontal,this);  
  26.     _volumeSlider->setMaximum(100); //the volume is between 0 and 100  
  27.     _volumeSlider->setToolTip("Audio slider");  
  28.   
  29.     // Note: if you use streaming, there is no ability to use the position slider  
  30.     _positionSlider=new QSlider(Qt::Horizontal,this);  
  31.     _positionSlider->setMaximum(POSITION_RESOLUTION);  
  32.   
  33.     QVBoxLayout *layout = new QVBoxLayout;  
  34.     layout->addWidget(_videoWidget);  
  35.     layout->addWidget(_positionSlider);  
  36.     layout->addWidget(_volumeSlider);  
  37.     setLayout(layout);  
  38.   
  39.     _isPlaying=false;  
  40.     poller=new QTimer(this);  
  41.   
  42.     //Initialize an instance of vlc  
  43.     //a structure for the exception is neede for this initalization  
  44.     libvlc_exception_init(&_vlcexcep);  
  45.   
  46.     //create a new libvlc instance  
  47.     _vlcinstance=libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args,&_vlcexcep);  //tricky calculation of the char space used  
  48.     raise (&_vlcexcep);  
  49.   
  50.     // Create a media player playing environement  
  51.     _mp = libvlc_media_player_new (_vlcinstance, &_vlcexcep);  
  52.     raise (&_vlcexcep);  
  53.   
  54.     //connect the two sliders to the corresponding slots (uses Qt's signal / slots technology)  
  55.     connect(poller, SIGNAL(timeout()), this, SLOT(updateInterface()));  
  56.     connect(_positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(changePosition(int)));  
  57.     connect(_volumeSlider, SIGNAL(sliderMoved(int)), this, SLOT(changeVolume(int)));  
  58.   
  59.     poller->start(100); //start timer to trigger every 100 ms the updateInterface slot  
  60. }  
  61.   
  62. //desctructor  
  63. Player::~Player()  
  64. {  
  65.     /* Stop playing */  
  66.     libvlc_media_player_stop (_mp, &_vlcexcep);  
  67.   
  68.     /* Free the media_player */  
  69.     libvlc_media_player_release (_mp);  
  70.   
  71.     libvlc_release (_vlcinstance);  
  72.     raise (&_vlcexcep);  
  73. }  
  74.   
  75. void Player::playFile(QString file)  
  76. {  
  77.     //the file has to be in one of the following formats /perhaps a little bit outdated)  
  78.     /* 
  79.     [file://]filename              Plain media file 
  80.     http://ip:port/file            HTTP URL 
  81.     ftp://ip:port/file             FTP URL 
  82.     mms://ip:port/file             MMS URL 
  83.     screen://                      Screen capture 
  84.     [dvd://][device][@raw_device]  DVD device 
  85.     [vcd://][device]               VCD device 
  86.     [cdda://][device]              Audio CD device 
  87.     udp:[[<source address>]@[<bind address>][:<bind port>]] 
  88.     */  
  89.   
  90.     /* Create a new LibVLC media descriptor */  
  91.     _m = libvlc_media_new (_vlcinstance, file.toAscii(), &_vlcexcep);  
  92.     raise(&_vlcexcep);  
  93.   
  94.     libvlc_media_player_set_media (_mp, _m, &_vlcexcep);  
  95.     raise(&_vlcexcep);  
  96.   
  97.     // /!\ Please note /!\  
  98.     //  
  99.     // passing the widget to the lib shows vlc at which position it should show up  
  100.     // vlc automatically resizes the video to the ′given size of the widget  
  101.     // and it even resizes it, if the size changes at the playing  
  102.   
  103.     /* Get our media instance to use our window */  
  104.     #if defined(Q_OS_WIN)  
  105.         libvlc_media_player_set_drawable(_mp, reinterpret_cast<unsigned int>(_videoWidget->winId()), &_vlcexcep );  
  106.         //libvlc_media_player_set_hwnd(_mp, _videoWidget->winId(), &_vlcexcep ); // for vlc 1.0  
  107.     #elif defined(Q_OS_MAC)  
  108.         libvlc_media_player_set_drawable(_mp, _videoWidget->winId(), &_vlcexcep );  
  109.         //libvlc_media_player_set_agl (_mp, _videoWidget->winId(), &_vlcexcep); // for vlc 1.0  
  110.     #else //Linux  
  111.         libvlc_media_player_set_drawable(_mp, _videoWidget->winId(), &_vlcexcep );  
  112.         //libvlc_media_player_set_xwindow(_mp, _videoWidget->winId(), &_vlcexcep ); // for vlc 1.0  
  113.     #endif  
  114.     raise(&_vlcexcep);  
  115.   
  116.     /* Play */  
  117.     libvlc_media_player_play (_mp, &_vlcexcep );  
  118.     raise(&_vlcexcep);  
  119.   
  120.     _isPlaying=true;  
  121. }  
  122.   
  123. void Player::changeVolume(int newVolume)  
  124. {  
  125.     libvlc_exception_clear(&_vlcexcep);  
  126.     libvlc_audio_set_volume (_vlcinstance,newVolume , &_vlcexcep);  
  127.     raise(&_vlcexcep);  
  128. }  
  129.   
  130. void Player::changePosition(int newPosition)  
  131. {  
  132.     libvlc_exception_clear(&_vlcexcep);  
  133.     // It's possible that the vlc doesn't play anything  
  134.     // so check before  
  135.     libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp, &_vlcexcep);  
  136.     libvlc_exception_clear(&_vlcexcep);  
  137.     if (curMedia == NULL)  
  138.         return;  
  139.   
  140.     float pos=(float)(newPosition)/(float)POSITION_RESOLUTION;  
  141.     libvlc_media_player_set_position (_mp, pos, &_vlcexcep);  
  142.     raise(&_vlcexcep);  
  143. }  
  144.   
  145. void Player::updateInterface()  
  146. {  
  147.     if(!_isPlaying)  
  148.         return;  
  149.   
  150.     // It's possible that the vlc doesn't play anything  
  151.     // so check before  
  152.     libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp, &_vlcexcep);  
  153.     libvlc_exception_clear(&_vlcexcep);  
  154.     if (curMedia == NULL)  
  155.         return;  
  156.   
  157.     float pos=libvlc_media_player_get_position (_mp, &_vlcexcep);  
  158.     int siderPos=(int)(pos*(float)(POSITION_RESOLUTION));  
  159.     _positionSlider->setValue(siderPos);  
  160.     int volume=libvlc_audio_get_volume (_vlcinstance,&_vlcexcep);  
  161.     _volumeSlider->setValue(volume);  
  162. }  
  163. void Player::raise(libvlc_exception_t * ex)  
  164. {  
  165.     if (libvlc_exception_raised (ex))  
  166.     {  
  167.          fprintf (stderr, "error: %s\n", libvlc_exception_get_message(ex));  
  168.          exit (-1);  
  169.     }  
  170. }  

  player.h

Cpp代码  收藏代码
  1. /* libVLC and Qt sample code 
  2.  * Copyright ? 2009 Alexander Maringer <maringer@maringer-it.de> 
  3.  */  
  4. #ifndef VLC_ON_QT_H  
  5. #define VLC_ON_QT_H  
  6.   
  7. #include <vlc/vlc.h>  
  8.   
  9. #include <QWidget>  
  10.   
  11. class QVBoxLayout;  
  12. class QPushButton;  
  13. class QTimer;  
  14. class QFrame;  
  15. class QSlider;  
  16.   
  17. #define POSITION_RESOLUTION 10000  
  18.   
  19. class Player : public QWidget  
  20. {  
  21.     Q_OBJECT  
  22.     QSlider *_positionSlider;  
  23.     QSlider *_volumeSlider;  
  24.     QFrame *_videoWidget;  
  25.     QTimer *poller;  
  26.     bool _isPlaying;  
  27.     libvlc_exception_t _vlcexcep;  
  28.     libvlc_instance_t *_vlcinstance;  
  29.     libvlc_media_player_t *_mp;  
  30.     libvlc_media_t *_m;  
  31.   
  32. public:  
  33.     Player();  
  34.     ~Player();  
  35.     void raise(libvlc_exception_t * ex);  
  36.   
  37. public slots:  
  38.     void playFile(QString file);  
  39.     void updateInterface();  
  40.     void changeVolume(int newVolume);  
  41.     void changePosition(int newPosition);  
  42.   
  43. };  
  44. #endif  

 在新建立一个main.cpp 修改成如下

 

Cpp代码  收藏代码
  1. #include <QtGui/QApplication>  
  2. #include "player.h"  
  3. int main(int argc, char *argv[])  
  4. {  
  5.     QApplication a(argc, argv);  
  6.     Player p;  
  7.     p.resize(640,480);  
  8.     p.playFile("#填写你要播的文件路径#");   
  9.     p.show();  
  10.   
  11.     return a.exec();  
  12. }  
 

然后我们修改  .pro文件 成如下

Cpp代码  收藏代码
  1. TARGET = test1  
  2. TEMPLATE = app  
  3. LIBS += -L'C:\vlc-lib'  
  4. LIBS += -lvlc  
  5. SOURCES += player.cpp \  
  6.     main.cpp  
  7. HEADERS += player.h  

 然后运行qmake

然后就可以编译了

编译成功以后想要运行,还必须把C:\vlc-lib里面的libvlc.dll,和libvlccore.dll拷贝到生成exe的目录里面才能运行

现在在qt creator里面就可以运行了(单独运行还需要其它dll)

 

haha

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多