Qt显示视频总时长

Qt显示导入视频的总时长需要通过Opencv来实现。。。

 QTime timer;

 int fps = cvGetCaptureProperty(pCapture, CV_CAP_PROP_FPS);

  int totalFrames = cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_COUNT);

  QString totalTime;

  QTime tTime = timer.addSecs(totalFrames/fps);

  QString timeLength;

  timeLength= tTime.toString("hh:mm:ss");

  m_pTimeLabelText->setText(timeLength);

首先,通过opencv的cvGetCaptureProperty得到视频文件的帧率及总帧数,然后把时间加到当前时间之上就可以得到视频文件的总时间。

下面说说QTime这个类的用法:

QTime::QTime()
默认构造函数,构造一个时,分,秒都为0的时间,如00:00:00.000(午夜)

QTime::QTime(int h, int m, int s=0, int ms = 0)
构造一个用户指定时,分,秒的时间.

QTime QTime::addMSecs(int ms) const//返回值为const说明这个返回的参数是不能修改的,而传入的参数如果为const,则说明该参数在这个函数内只能用而不能修改,即只读
返回一个当前时间对象之后或之前ms毫秒的时间对象(之前还是之后视ms的符号,如为正则之后,反之之前)
如:QTime time(3,0,0);
   QTime newTime1 = time.addMSecs(1000);
   QTime newTime2 = time.addMSecs(-1000);
则newTime1是一个比time所指定时间(03:00:00.000)延后1000毫秒也即1秒的时间(03:00:01.000),而newTime2则提前1000毫秒(02:59:59.000)

QTime QTime::addSecs(int nsecs) const
与addMSecs()相同,只是nsecs单位是秒.即返回一个当前时间对象之前或之后的时间对象.

int QTime::elapsed() const
返回最后一次调用start()或restart()到现在已经经过的毫秒数.如果经过了24小时之后,则计数器置0.如果系统时间设置改变,则结果不确定.

int QTime::hour() const
返回时间对象的小时,取值范围(0--23)

int QTime::minute() const
返回时间对象的分钟,取值范围(0--59)

int QTime::second() const
返回时间对象的秒,取值范围(0--59)

int QTime::msec() const
返回时间对象的毫秒,取值范围(0--999)

bool QTime::isNull() const
如果时间对象等于00:00:00.000,则返回true;反之返回false.

bool QTime::isValid() const
如果时间对象是有效的,则返回true;反之返回false.(即:时,分,秒,毫秒都在其取值范围之内)

int QTime::msecsTo(const QTime &t) const
返回当前时间对象到t所指定的时间之间的毫秒数.如果t早于当前时间对象的时间,则返回的值是负值.因为一天的时间是86400000毫秒,所以返回值范围是-86400000--86400000

int QTime::secsTo(const QTime &t) const
与msecsTo()基本相同,只是返回的是秒数,返回值的有效范围是-86400--86400

int QTime::restart()
设置当前时间对象的值为当前系统时间,并且返回从最后一次调用start()或restart()到现在的毫秒数.如果计数器超出24小时,则置0.如果计数器计数时系统时间设置改变,则结果不确定.

bool QTime::setHMS(int h, int m, int s, int ms = 0)
设置当前时间对象的时,分,秒和毫秒.如果给定的参数值有效,则返回true,否则返回false.

void QTime::start()
设置当前时间对象的值为当前系统时间,这个函数实际是结合restart()和elapsed()用来计数的.

QString QTime::toString(const QString &format) const
按照参数format指定的格式用字符串形式输出当前时间对象的时间.
参数format用来指定时,分,秒,毫秒的输出格式.如(hh:mm:ss.zzz)
h:表示小时,范围是0--23
hh:用两位数表示小时,不足两位的前面用0补足,如(0点:00,3点:03,11点:11)
m:表示分钟,范围0--59
mm:用两位数表示分钟,不足两位的前面用0补足.
s:表示秒,范围0--59
ss:用两位数表示秒,不足两位的前面用0补足.
z:表示毫秒,范围0--999
zzz:用三位数表示毫秒,不足三位的前面用0补足.
AP:用AM/PM显示.
ap:用ap/pm显示.
例如:
QTime time(14,3,9,42);//设置时间为14:03:09.042
QString i = time.toString("hh:mm:ss.zzz");//结果为14:03:09.042
QString j = time.toString("h:m:s.z");//结果为14:3:9.42
QString m = time.toString("h:m:s.z AP");//结果为2:3:9.42 PM
QString n = time.toString("h:m:s.z ap");//结果为2:3:9.42 pm

QString QTime::toString(Qt::DateFormat f = Qt::TextDate) const
按照参数format指定的格式用字符串形式输出当前时间对象的时间.
参数的可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置

----------------------------------------------------------------------------------------------------------------------------------------

静态成员函数:

QTime QTime::currentTime()
返回当前的系统时间.

QTime QTime::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
使用参数format指定的格式根据参数string指定的时间返回一个时间对象。如果string指定的时间不合法,则返回一个无效的时间对象。
format可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置

QTime QTime::fromString(const QString &string, const QString &format)
使用参数format指定的格式根据参数string指定的时间返回一个时间对象.如果string指定的时间不合法,则返回一个无效的时间对象.
format的格式参看QString QTime::toString(const QString &format) const.

bool QTime::isValid(int h, int m, int s, int ms = 0)
如果参数所指定的时间是合法的,则返回true;反之返回false.

----------------------------------------------------------------------------------------------------------------------------------------

静态成员函数不依赖于对象,可以通过类直接调用,与对象无关:

如:获取当前系统时间的小时部分时不需要定义QTime对象

int hour = QTime::currentTime().hour()



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现视频播放的基本思路如下: 1. 使用QMediaPlayer类控制视频的播放和暂停等操作; 2. 使用QVideoWidget类作为视频显示窗口; 3. 使用QSlider类实现进度条; 4. 使用QLabel类显示总时长和播放进度; 5. 使用QPushButton类实现开始、暂停和停止按钮。 下面是一个简单的示例代码: ```cpp #include <QMediaPlayer> #include <QVideoWidget> #include <QSlider> #include <QLabel> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QTime> class VideoPlayer : public QWidget { Q_OBJECT public: VideoPlayer(QWidget *parent = nullptr) : QWidget(parent) { // 创建视频播放器 player = new QMediaPlayer(this); videoWidget = new QVideoWidget(this); player->setVideoOutput(videoWidget); // 创建进度条 slider = new QSlider(Qt::Horizontal, this); slider->setRange(0, 0); connect(player, &QMediaPlayer::durationChanged, this, &VideoPlayer::setDuration); connect(player, &QMediaPlayer::positionChanged, this, &VideoPlayer::setPosition); connect(slider, &QSlider::sliderMoved, this, &VideoPlayer::seek); // 创建标签 timeLabel = new QLabel("--:-- / --:--", this); // 创建按钮 playButton = new QPushButton("Play", this); connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play); pauseButton = new QPushButton("Pause", this); connect(pauseButton, &QPushButton::clicked, player, &QMediaPlayer::pause); stopButton = new QPushButton("Stop", this); connect(stopButton, &QPushButton::clicked, player, &QMediaPlayer::stop); // 布局 QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(playButton); buttonLayout->addWidget(pauseButton); buttonLayout->addWidget(stopButton); QHBoxLayout *sliderLayout = new QHBoxLayout; sliderLayout->addWidget(slider); sliderLayout->addWidget(timeLabel); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(videoWidget); layout->addLayout(sliderLayout); layout->addLayout(buttonLayout); setLayout(layout); } private slots: void setDuration(qint64 duration) { this->duration = duration; slider->setRange(0, duration); updateTimeLabel(); } void setPosition(qint64 position) { slider->setValue(position); updateTimeLabel(); } void seek(int position) { player->setPosition(position); updateTimeLabel(); } void play() { player->play(); playButton->setEnabled(false); } void updateTimeLabel() { QTime durationTime(0, (duration / 60000) % 60, (duration / 1000) % 60); QTime positionTime(0, (slider->value() / 60000) % 60, (slider->value() / 1000) % 60); timeLabel->setText(positionTime.toString("mm:ss") + " / " + durationTime.toString("mm:ss")); } private: QMediaPlayer *player; QVideoWidget *videoWidget; QSlider *slider; QLabel *timeLabel; QPushButton *playButton, *pauseButton, *stopButton; qint64 duration; }; ``` 使用方法: ```cpp VideoPlayer *player = new VideoPlayer; player->show(); player->player->setMedia(QUrl::fromLocalFile("path/to/video")); ``` 其中,`QUrl::fromLocalFile` 方法可以将本地文件路径转换为 URL。你只需要将路径替换成你自己的视频文件路径即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值