Skip to content

Commit

Permalink
重构transcode-4;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Mar 4, 2024
1 parent bf82c54 commit 9e15a1d
Show file tree
Hide file tree
Showing 31 changed files with 526 additions and 425 deletions.
26 changes: 13 additions & 13 deletions examples/player/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void MainWindow::onProcessEvents()
auto *speedEvent = dynamic_cast<Ffmpeg::CacheSpeedEvent *>(eventPtr.data());
d_ptr->controlWidget->setCacheSpeed(speedEvent->speed());
} break;
case Ffmpeg::PropertyChangeEvent::MediaTrack: {
case Ffmpeg::PropertyChangeEvent::EventType::MediaTrack: {
d_ptr->resetTrackMenu();

auto *tracksEvent = dynamic_cast<Ffmpeg::MediaTrackEvent *>(eventPtr.data());
Expand Down Expand Up @@ -444,7 +444,7 @@ void MainWindow::onProcessEvents()
}
}
} break;
case Ffmpeg::PropertyChangeEvent::SeekChanged: {
case Ffmpeg::PropertyChangeEvent::EventType::SeekChanged: {
auto *seekEvent = dynamic_cast<Ffmpeg::SeekChangedEvent *>(eventPtr.data());
int value = seekEvent->position() * 100 / d_ptr->playerPtr->duration();
auto text = tr("Seeked To %1 (key frame) / %2 (%3%)")
Expand All @@ -455,8 +455,8 @@ void MainWindow::onProcessEvents()
QString::number(value));
d_ptr->setTitleWidgetText(text);
} break;
case Ffmpeg::PropertyChangeEvent::Error: {
auto *errorEvent = dynamic_cast<Ffmpeg::ErrorEvent *>(eventPtr.data());
case Ffmpeg::PropertyChangeEvent::EventType::AVError: {
auto *errorEvent = dynamic_cast<Ffmpeg::AVErrorEvent *>(eventPtr.data());
const auto text = tr("Error[%1]:%2.")
.arg(QString::number(errorEvent->error().errorCode()),
errorEvent->error().errorString());
Expand Down Expand Up @@ -641,19 +641,19 @@ void MainWindow::initMenu()
renderMenu();

connect(d_ptr->audioTracksGroup, &QActionGroup::triggered, this, [this](QAction *action) {
d_ptr->playerPtr->addEvent(
Ffmpeg::EventPtr(new Ffmpeg::SelectedMediaTrackEvent(action->property("index").toInt(),
Ffmpeg::Event::AudioTarck)));
d_ptr->playerPtr->addEvent(Ffmpeg::EventPtr(
new Ffmpeg::SelectedMediaTrackEvent(action->property("index").toInt(),
Ffmpeg::Event::EventType::AudioTarck)));
});
connect(d_ptr->videoTracksGroup, &QActionGroup::triggered, this, [this](QAction *action) {
d_ptr->playerPtr->addEvent(
Ffmpeg::EventPtr(new Ffmpeg::SelectedMediaTrackEvent(action->property("index").toInt(),
Ffmpeg::Event::VideoTrack)));
d_ptr->playerPtr->addEvent(Ffmpeg::EventPtr(
new Ffmpeg::SelectedMediaTrackEvent(action->property("index").toInt(),
Ffmpeg::Event::EventType::VideoTrack)));
});
connect(d_ptr->subTracksGroup, &QActionGroup::triggered, this, [this](QAction *action) {
d_ptr->playerPtr->addEvent(
Ffmpeg::EventPtr(new Ffmpeg::SelectedMediaTrackEvent(action->property("index").toInt(),
Ffmpeg::Event::SubtitleTrack)));
d_ptr->playerPtr->addEvent(Ffmpeg::EventPtr(
new Ffmpeg::SelectedMediaTrackEvent(action->property("index").toInt(),
Ffmpeg::Event::EventType::SubtitleTrack)));
});

connect(d_ptr->mediaInfoAction, &QAction::triggered, this, &MainWindow::onShowMediaInfo);
Expand Down
72 changes: 59 additions & 13 deletions examples/transcoder/audioencoderwidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,48 @@ class AudioEncoderWidget::AudioEncoderWidgetPrivate
explicit AudioEncoderWidgetPrivate(AudioEncoderWidget *q)
: q_ptr(q)
{
const auto *comboBoxStyleSheet = "QComboBox {combobox-popup:0;}";

audioEncoderCbx = new QComboBox(q_ptr);
audioEncoderCbx->setView(new QListView(audioEncoderCbx));
audioEncoderCbx->setMaxVisibleItems(10);
audioEncoderCbx->setStyleSheet("QComboBox {combobox-popup:0;}");

auto audioEncodercs = Ffmpeg::getCurrentSupportCodecs(AVMEDIA_TYPE_AUDIO, true);
for (auto iter = audioEncodercs.cbegin(); iter != audioEncodercs.cend(); ++iter) {
audioEncoderCbx->addItem(iter.value(), iter.key());
audioEncoderCbx->setStyleSheet(comboBoxStyleSheet);
auto audioEncodercs = Ffmpeg::getCodecsInfo(AVMEDIA_TYPE_AUDIO, true);
for (const auto &codec : std::as_const(audioEncodercs)) {
auto text = QString("%1 (%2)").arg(codec.longName).arg(codec.name);
audioEncoderCbx->addItem(text, QVariant::fromValue(codec));
if (codec.codecId == AV_CODEC_ID_AAC) {
audioEncoderCbx->setCurrentText(text);
}
}
audioEncoderCbx->setCurrentIndex(audioEncoderCbx->findData(AV_CODEC_ID_AAC));
audioEncoderCbx->model()->sort(0);

chLayoutCbx = new QComboBox(q_ptr);
chLayoutCbx->setView(new QListView(chLayoutCbx));
chLayoutCbx->setMaxVisibleItems(10);
chLayoutCbx->setStyleSheet(comboBoxStyleSheet);
auto chLayoutcs = Ffmpeg::getCurrentChLayouts();
for (const auto &chLayout : std::as_const(chLayoutcs)) {
chLayoutCbx->addItem(chLayout.channelName, chLayout.channel);
}
chLayoutCbx->setCurrentIndex(chLayoutCbx->findData(AV_CH_LAYOUT_STEREO));

profileCbx = new QComboBox(q_ptr);
profileCbx->setView(new QListView(profileCbx));
profileCbx->setMaxVisibleItems(10);
profileCbx->setStyleSheet(comboBoxStyleSheet);
}

QString currentCodecName()
{
return audioEncoderCbx->currentData().value<Ffmpeg::CodecInfo>().name;
}

AudioEncoderWidget *q_ptr;

QComboBox *audioEncoderCbx;
QComboBox *chLayoutCbx;
QComboBox *profileCbx;
};

AudioEncoderWidget::AudioEncoderWidget(QWidget *parent)
Expand All @@ -48,17 +74,37 @@ auto AudioEncoderWidget::setEncoder(AVCodecID codecId) -> bool
return finded;
}

auto AudioEncoderWidget::encoder() const -> QString
auto AudioEncoderWidget::encodeParam() const -> Ffmpeg::EncodeContext
{
return d_ptr->audioEncoderCbx->currentText();
Ffmpeg::EncodeContext encodeParam;
encodeParam.mediaType = AVMEDIA_TYPE_AUDIO;
encodeParam.encoderName = d_ptr->currentCodecName();
encodeParam.channel = static_cast<AVChannel>(d_ptr->chLayoutCbx->currentData().toLongLong());

return encodeParam;
}

void AudioEncoderWidget::onEncoderChanged()
{
d_ptr->profileCbx->clear();
auto profiles = Ffmpeg::getCodecProfiles(d_ptr->currentCodecName());
for (const auto &profile : std::as_const(profiles)) {
d_ptr->profileCbx->addItem(profile.name, profile.profile);
}
}

void AudioEncoderWidget::setupUI()
{
auto *layout = new QHBoxLayout(this);
layout->addWidget(new QLabel(tr("Encoder:")));
layout->addWidget(d_ptr->audioEncoderCbx);
layout->addStretch();
auto *layout = new QFormLayout(this);
layout->addRow(tr("Encoder:"), d_ptr->audioEncoderCbx);
layout->addRow(tr("Channel Layout:"), d_ptr->chLayoutCbx);
layout->addRow(tr("Profile:"), d_ptr->profileCbx);
}

void AudioEncoderWidget::buildConnect() {}
void AudioEncoderWidget::buildConnect()
{
connect(d_ptr->audioEncoderCbx,
&QComboBox::currentIndexChanged,
this,
&AudioEncoderWidget::onEncoderChanged);
}
8 changes: 7 additions & 1 deletion examples/transcoder/audioencoderwidget.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef AUDIOENCODERWIDGET_HPP
#define AUDIOENCODERWIDGET_HPP

#include <ffmpeg/encodecontext.hpp>

#include <QWidget>

extern "C" {
Expand All @@ -15,7 +17,11 @@ class AudioEncoderWidget : public QWidget
~AudioEncoderWidget() override;

auto setEncoder(AVCodecID codecId) -> bool;
[[nodiscard]] auto encoder() const -> QString;

auto encodeParam() const -> Ffmpeg::EncodeContext;

private slots:
void onEncoderChanged();

private:
void setupUI();
Expand Down
37 changes: 18 additions & 19 deletions examples/transcoder/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "videoencoderwidget.hpp"

#include <ffmpeg/averror.h>
#include <ffmpeg/encodecontext.hpp>
#include <ffmpeg/event/errorevent.hpp>
#include <ffmpeg/event/trackevent.hpp>
#include <ffmpeg/event/valueevent.hpp>
Expand All @@ -24,15 +25,14 @@ class MainWindow::MainWindowPrivate
{
Ffmpeg::printFfmpegInfo();

Ffmpeg::EncodeContext encodeContext;

transcoder = new Ffmpeg::Transcoder(q_ptr);

sourceWidget = new SourceWidget(q_ptr);
tabWidget = new QTabWidget(q_ptr);
previewWidget = new PreviewWidget(q_ptr);
videoEncoderWidget = new VideoEncoderWidget(q_ptr);
videoEncoderWidget->setPreset(transcoder->presets(), transcoder->preset());
videoEncoderWidget->setTune(transcoder->tunes(), transcoder->tune());
videoEncoderWidget->setProfile(transcoder->profiles(), transcoder->profile());
audioEncoderWidget = new AudioEncoderWidget(q_ptr);
tabWidget->addTab(previewWidget,
QCoreApplication::translate("MainWindowPrivate", "Preview"));
Expand Down Expand Up @@ -187,20 +187,12 @@ void MainWindow::onStart()

d_ptr->transcoder->setInFilePath(inPath);
d_ptr->transcoder->setOutFilePath(outPath);
d_ptr->transcoder->setAudioEncodecName(d_ptr->audioEncoderWidget->encoder());
d_ptr->transcoder->setVideoEncodecName(d_ptr->videoEncoderWidget->encoder());
d_ptr->transcoder->setUseGpuDecode(d_ptr->videoEncoderWidget->gpuDecode());
d_ptr->transcoder->setUseGpuEncode(d_ptr->videoEncoderWidget->gpuEncode());
d_ptr->transcoder->setSize(d_ptr->videoEncoderWidget->videoSize());
d_ptr->transcoder->setVideoEncodeContext(d_ptr->videoEncoderWidget->encodeParam());
d_ptr->transcoder->setAudioEncodeContext(d_ptr->audioEncoderWidget->encodeParam());

if (QFile::exists(subtitlePath)) {
d_ptr->transcoder->setSubtitleFilename(subtitlePath);
}
d_ptr->transcoder->setQuailty(d_ptr->videoEncoderWidget->quality());
d_ptr->transcoder->setMinBitrate(d_ptr->videoEncoderWidget->minBitrate());
d_ptr->transcoder->setMaxBitrate(d_ptr->videoEncoderWidget->maxBitrate());
d_ptr->transcoder->setCrf(d_ptr->videoEncoderWidget->crf());
d_ptr->transcoder->setPreset(d_ptr->videoEncoderWidget->preset());
d_ptr->transcoder->setProfile(d_ptr->videoEncoderWidget->profile());
d_ptr->transcoder->startTranscode();
d_ptr->statusWidget->setStatus(tr("Stop"));

Expand Down Expand Up @@ -236,7 +228,7 @@ void MainWindow::onProcessEvents()
d_ptr->statusWidget->setProgress(positionEvent->position() * 100.0
/ d_ptr->transcoder->duration());
} break;
case Ffmpeg::PropertyChangeEvent::MediaTrack: {
case Ffmpeg::PropertyChangeEvent::EventType::MediaTrack: {
bool audioSet = false;
bool videoSet = false;
auto *mediaTrackEvent = dynamic_cast<Ffmpeg::MediaTrackEvent *>(eventPtr.data());
Expand Down Expand Up @@ -265,13 +257,20 @@ void MainWindow::onProcessEvents()
case Ffmpeg::PropertyChangeEvent::EventType::PreviewFramesChanged:
d_ptr->previewWidget->setFrames(d_ptr->transcoder->previewFrames());
break;
case Ffmpeg::PropertyChangeEvent::Error: {
auto *errorEvent = dynamic_cast<Ffmpeg::ErrorEvent *>(eventPtr.data());
case Ffmpeg::PropertyChangeEvent::EventType::AVError: {
auto *errorEvent = dynamic_cast<Ffmpeg::AVErrorEvent *>(eventPtr.data());
const auto text = tr("Error[%1]:%2.")
.arg(QString::number(errorEvent->error().errorCode()),
errorEvent->error().errorString());
qWarning() << text;
}
statusBar()->showMessage(text);
} break;
case Ffmpeg::PropertyChangeEvent::EventType::Error: {
auto *errorEvent = dynamic_cast<Ffmpeg::ErrorEvent *>(eventPtr.data());
const auto text = tr("Error:%1.").arg(errorEvent->error());
qWarning() << text;
statusBar()->showMessage(text);
} break;
default: break;
}
}
Expand Down Expand Up @@ -301,7 +300,7 @@ void MainWindow::setupUI()
layout->addWidget(d_ptr->outPutWidget);
setCentralWidget(widget);

statusBar()->addWidget(d_ptr->statusWidget);
statusBar()->addPermanentWidget(d_ptr->statusWidget);

auto *tempWidget = new QWidget(this);
auto *tempLayout = new QVBoxLayout(tempWidget);
Expand Down
6 changes: 6 additions & 0 deletions examples/transcoder/sourcewidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ void SourceWidget::setFileInfo(const QString &info)
d_ptr->infoLabel->setToolTip(info);
}

auto SourceWidget::range() const -> QPair<qint64, qint64>
{
return {d_ptr->startTimeEdit->time().msecsSinceStartOfDay() * 1000,
d_ptr->endTimeEdit->time().msecsSinceStartOfDay() * 1000};
}

void SourceWidget::onRangeChanged()
{
d_ptr->startTimeEdit->setMaximumTime(d_ptr->endTimeEdit->time());
Expand Down
2 changes: 2 additions & 0 deletions examples/transcoder/sourcewidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SourceWidget : public QWidget

void setFileInfo(const QString &info);

auto range() const -> QPair<qint64, qint64>;

private slots:
void onRangeChanged();

Expand Down
2 changes: 1 addition & 1 deletion examples/transcoder/stautuswidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class StautusWidget : public QWidget
~StautusWidget() override;

void setStatus(const QString &status);
QString status() const;
auto status() const -> QString;

void setProgress(int progress);

Expand Down
Loading

0 comments on commit 9e15a1d

Please sign in to comment.