diff --git a/README.md b/README.md index 1168e5c..f84380b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A simple webcam recorder for Omarchy. Choose your camera and microphone once, then press **Space** to record. Press again to pause or resume the same take. Stop the take and it opens right away in a built-in editor: trim either end, cut ranges out of the middle, then **Save**. - Remembers camera and microphone by device ID; missing inputs never silently switch. -- Automatically selects the camera's maximum advertised video resolution, preferring 30 fps at that resolution. Preview preserves the whole frame. +- Automatically selects the camera's maximum advertised video resolution, preferring 30 fps at that resolution, and lists every advertised format in a dropdown so a different resolution, frame rate, or pixel format stays selected across sessions. Preview preserves the whole frame. - Live microphone meter with peak hold and clipping indication, including while paused. No microphone playback through your speakers. - Explicit No audio option for silent recordings. - Live Omarchy accent syncing, including theme symlink switches. Controls and dialogs follow Hyprland's active corner rounding, including personal overrides. Dark chrome and a yellow fallback follow Omacut; button foregrounds adapt for contrast. diff --git a/src/Main.qml b/src/Main.qml index 972c8b5..a0af4c0 100644 --- a/src/Main.qml +++ b/src/Main.qml @@ -121,7 +121,7 @@ ApplicationWindow { readonly property bool controlFocused: activeFocusItem && (activeFocusItem instanceof AbstractButton || activeFocusItem instanceof ComboBox || activeFocusItem instanceof Slider || activeFocusItem instanceof TextInput) Shortcut { sequence: "Space"; context: Qt.WindowShortcut; autoRepeat: false - enabled: !win.overlayOpen && !cameraChoice.popup.visible && !microphoneChoice.popup.visible && !win.controlFocused && !win.busy + enabled: !win.overlayOpen && !cameraChoice.popup.visible && !microphoneChoice.popup.visible && !formatChoice.popup.visible && !win.controlFocused && !win.busy onActivated: win.space() } // Stopping ends the take and opens it for editing. @@ -130,7 +130,7 @@ ApplicationWindow { Shortcut { sequence: "Ctrl+S"; context: Qt.WindowShortcut; autoRepeat: false; enabled: win.finished && !win.busy && !win.overlayOpen; onActivated: { player.pause(); backend.save() } } Shortcut { sequence: "Escape"; context: Qt.WindowShortcut; autoRepeat: false - enabled: (backend.takeActive || win.finished) && !win.busy && !win.overlayOpen && !cameraChoice.popup.visible && !microphoneChoice.popup.visible + enabled: (backend.takeActive || win.finished) && !win.busy && !win.overlayOpen && !cameraChoice.popup.visible && !microphoneChoice.popup.visible && !formatChoice.popup.visible onActivated: { player.pause(); restartDialog.open() } } // Editing the finished clip. @@ -246,6 +246,8 @@ ApplicationWindow { id: choice property string iconPath property string detail: "" + // Shows this fixed text instead of the selected row's label. + property string textOverride: "" property real maximumTextWidth: 260 textRole: "label" focusPolicy: Qt.TabFocus @@ -257,10 +259,10 @@ ApplicationWindow { background: Item { } contentItem: RowLayout { spacing: 8 - Icon { path: choice.iconPath; color: win.dimColor } + Icon { visible: choice.iconPath !== ""; path: choice.iconPath; color: win.dimColor } Label { Layout.maximumWidth: choice.maximumTextWidth; elide: Text.ElideRight; font: choice.font - text: choice.displayText + text: choice.textOverride !== "" ? choice.textOverride : choice.displayText color: choice.activeFocus ? win.accent : choice.enabled ? (choice.hovered ? "#ffffff" : win.textColor) : win.dimColor } Icon { visible: choice.enabled; path: win.icons.chevron; implicitWidth: 12; implicitHeight: 12; color: win.dimColor } @@ -274,6 +276,42 @@ ApplicationWindow { } HoverHandler { cursorShape: choice.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor } } + // A tight popup row for long lists like camera formats. + component CompactRow: ItemDelegate { + id: row + required property int index + required property var modelData + readonly property bool selected: formatChoice.currentIndex === index + width: ListView.view.width + height: 24 + highlighted: formatChoice.highlightedIndex === index + contentItem: RowLayout { + spacing: 8 + Label { + id: rowLabel + Layout.fillWidth: true + text: modelData.label + font.pixelSize: 11 + color: row.highlighted ? win.accent : win.textColor + verticalAlignment: Text.AlignVCenter + elide: Text.ElideRight + } + Icon { + visible: row.selected; color: win.accent + implicitWidth: 12; implicitHeight: 12 + path: "M4 12.5l5 5 11-13" + } + } + background: Rectangle { + radius: Math.min(4, win.cornerRadius) + color: row.highlighted ? "#2c2c2f" : "transparent" + } + ToolTip { + visible: row.hovered && rowLabel.truncated + text: modelData.label + background: Rectangle { color: "#303037"; radius: win.cornerRadius } + } + } component ShortcutList: GridLayout { id: list property string heading @@ -410,13 +448,31 @@ ApplicationWindow { id: sources; visible: !win.finished; spacing: 2 anchors.left: parent.left; anchors.verticalCenter: parent.verticalCenter width: parent.width - SourceChoice { - id: cameraChoice; iconPath: win.icons.camera; detail: backend.formatLabel - maximumTextWidth: Math.min(260, sources.width - 44) - model: backend.cameras; currentIndex: backend.cameraIndex - enabled: !backend.takeActive && !win.busy - onActivated: index => { backend.selectCamera(index); liveVideo.forceActiveFocus() } - Accessible.name: "Camera" + RowLayout { + Layout.fillWidth: true; spacing: 8 + SourceChoice { + id: cameraChoice; iconPath: win.icons.camera; detail: backend.formatLabel + Layout.fillWidth: true + maximumTextWidth: Math.min(260, sources.width - 44) + model: backend.cameras; currentIndex: backend.cameraIndex + enabled: !backend.takeActive && !win.busy + onActivated: index => { backend.selectCamera(index); liveVideo.forceActiveFocus() } + Accessible.name: "Camera" + } + SourceChoice { + id: formatChoice + font.pixelSize: 9 + Layout.maximumWidth: 90 + maximumTextWidth: 70 + model: backend.cameraFormats; currentIndex: backend.cameraFormatIndex + enabled: !backend.takeActive && !win.busy && backend.cameraFormats.length > 0 + delegate: CompactRow { } + // The dropdown rewrites currentIndex itself; re-bind it to the backend's + // chosen format whenever the list or the selection changes. + onActivated: index => { backend.selectCameraFormat(index); formatChoice.currentIndex = Qt.binding(() => backend.cameraFormatIndex); liveVideo.forceActiveFocus() } + onModelChanged: formatChoice.currentIndex = Qt.binding(() => backend.cameraFormatIndex) + Accessible.name: "Camera format" + } } SourceChoice { id: microphoneChoice; iconPath: win.icons.microphone diff --git a/src/backend.cpp b/src/backend.cpp index 9c50603..2d2e2c3 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -117,18 +117,35 @@ void Backend::activateSources() { if(m_writer || m_state=="saving" || m_probing) return; if(!m_activateHardware) { m_state="unavailable"; emit changed(); return; } releaseSources(); - m_state="starting"; m_message.clear(); m_formatLabel.clear(); m_activatedAt=now(); + m_state="starting"; m_message.clear(); m_formatLabel.clear(); m_cameraFormats.clear(); m_cameraFormatIndex=-1; m_activatedAt=now(); QCameraDevice selectedCamera; for(const auto &d:QMediaDevices::videoInputs()) if(deviceId(d.id())==m_cameraId) selectedCamera=d; QAudioDevice selectedAudio; for(const auto &d:QMediaDevices::audioInputs()) if(deviceId(d.id())==m_audioId) selectedAudio=d; if(selectedCamera.isNull()) { m_state="unavailable"; m_message="Connect a camera or choose an available video source."; } else { - m_cameraFormat=media::bestCameraFormat(selectedCamera); - if(m_cameraFormat.isNull()) { m_state="unavailable"; m_message="This camera advertises no supported video formats."; } + m_formatChoices=media::sortedFormats(selectedCamera); + QStringList keys; + for(const auto &f:m_formatChoices) { + keys.append(media::formatKey(f.resolution(),f.minFrameRate(),f.maxFrameRate(),int(f.pixelFormat()))); + m_cameraFormats.append(QVariantMap{{"label",media::formatLabel(f.resolution(),f.minFrameRate(),f.maxFrameRate(),int(f.pixelFormat()))}}); + } + // bestFormat ranks the device's own order, so match its pick by identity + // instead of reusing the index against the sorted list. + const auto advertised=selectedCamera.videoFormats(); + QList choices; + for(const auto &f:advertised) choices.append({f.resolution(),f.minFrameRate(),f.maxFrameRate(),int(f.pixelFormat())}); + const int best=media::bestFormat(choices); + if(best<0) { m_state="unavailable"; m_message="This camera advertises no supported video formats."; } else { + // A format picked for this camera before wins, so a deliberate choice survives + // restarts; if the camera no longer offers it, fall back to the default pick. + const auto bestKey=media::formatKey(advertised[best].resolution(),advertised[best].minFrameRate(),advertised[best].maxFrameRate(),int(advertised[best].pixelFormat())); + const int chosen=media::chooseFormat(keys,m_settings.value("cameraFormat/"+m_cameraId).toString(),bestKey); + m_cameraFormatIndex=chosen; + m_cameraFormat=m_formatChoices[chosen]; m_fps=media::targetFps(m_cameraFormat.minFrameRate(),m_cameraFormat.maxFrameRate()); - m_formatLabel=QString("%1 × %2 · up to %3 fps").arg(m_cameraFormat.resolution().width()).arg(m_cameraFormat.resolution().height()).arg(m_fps,0,'f',m_fps==int(m_fps)?0:2); + m_formatLabel=media::formatLabel(m_cameraFormat.resolution(),m_cameraFormat.minFrameRate(),m_cameraFormat.maxFrameRate(),int(m_cameraFormat.pixelFormat())); m_camera=new QCamera(selectedCamera,this); m_camera->setCameraFormat(m_cameraFormat); m_capture.setCamera(m_camera); @@ -149,6 +166,7 @@ void Backend::activateSources() { } else { m_settings.setValue("microphone/id","none"); m_settings.setValue("microphone/label","No audio"); } if(QStandardPaths::findExecutable("ffprobe").isEmpty() || QStandardPaths::findExecutable("ffmpeg").isEmpty()) { m_state="unavailable"; m_message="Install ffmpeg (including ffprobe) to record and inspect clips."; } else if(!Writer::supported(audioEnabled())) { m_state="unavailable"; m_message="This Qt multimedia backend cannot encode H.264 MP4 with the selected audio mode."; } + emit devicesChanged(); emit changed(); } void Backend::updateReady() { @@ -160,7 +178,7 @@ void Backend::receiveVideo(const QVideoFrame &frame) { if(!m_camera || !frame.isValid()) return; m_lastVideoAt=now(); if(frame.size()!=m_cameraFormat.resolution()) { - sourceFailed("The camera did not provide its maximum resolution. Choose another source or Retry."); return; + sourceFailed("The camera did not provide the selected resolution. Choose another format or Retry."); return; } m_lastFrame=frame; // Once stopped, the preview holds the take's final frame while it finalizes. @@ -230,6 +248,12 @@ void Backend::selectMicrophone(int index) { if(m_writer || index<0 || index>=m_microphones.size() || m_state=="saving" || m_probing) return; m_audioId=m_microphones[index].toMap()["id"].toString(); activateSources(); } +void Backend::selectCameraFormat(int index) { + if(m_writer || index<0 || index>=m_formatChoices.size() || m_state=="saving" || m_probing) return; + const auto &f=m_formatChoices[index]; + m_settings.setValue("cameraFormat/"+m_cameraId,media::formatKey(f.resolution(),f.minFrameRate(),f.maxFrameRate(),int(f.pixelFormat()))); + activateSources(); +} void Backend::retry() { if(!m_writer && m_state!="finished") activateSources(); } void Backend::toggleRecording() { if(m_dialogOpen) return; diff --git a/src/backend.h b/src/backend.h index e8eaceb..9458fce 100644 --- a/src/backend.h +++ b/src/backend.h @@ -27,6 +27,8 @@ class Backend : public QObject { Q_PROPERTY(QVariantList microphones READ microphones NOTIFY devicesChanged) Q_PROPERTY(int cameraIndex READ cameraIndex NOTIFY devicesChanged) Q_PROPERTY(int microphoneIndex READ microphoneIndex NOTIFY devicesChanged) + Q_PROPERTY(QVariantList cameraFormats READ cameraFormats NOTIFY devicesChanged) + Q_PROPERTY(int cameraFormatIndex READ cameraFormatIndex NOTIFY devicesChanged) Q_PROPERTY(bool ready READ ready NOTIFY changed) Q_PROPERTY(bool audioEnabled READ audioEnabled NOTIFY changed) Q_PROPERTY(bool takeActive READ takeActive NOTIFY changed) @@ -55,8 +57,10 @@ class Backend : public QObject { QString formatLabel() const { return m_formatLabel; } QVariantList cameras() const { return m_cameras; } QVariantList microphones() const { return m_microphones; } + QVariantList cameraFormats() const { return m_cameraFormats; } int cameraIndex() const; int microphoneIndex() const; + int cameraFormatIndex() const { return m_cameraFormatIndex; } bool ready() const; bool audioEnabled() const { return m_audioId != "none"; } bool takeActive() const { return m_writer != nullptr; } @@ -79,6 +83,7 @@ class Backend : public QObject { Q_INVOKABLE void setPreview(QObject *sink); Q_INVOKABLE void selectCamera(int index); Q_INVOKABLE void selectMicrophone(int index); + Q_INVOKABLE void selectCameraFormat(int index); Q_INVOKABLE void retry(); Q_INVOKABLE void toggleRecording(); Q_INVOKABLE void finish(); @@ -142,6 +147,7 @@ class Backend : public QObject { QAudioFormat m_audioFormat; QVideoFrame m_lastFrame; QCameraFormat m_cameraFormat; + QList m_formatChoices; QElapsedTimer m_wall; QTimer m_tick; QTimer m_finishTimeout; @@ -150,8 +156,9 @@ class Backend : public QObject { bool m_activateHardware; QString m_pendingDestination; Writer *m_writer = nullptr; - QVariantList m_cameras, m_microphones; + QVariantList m_cameras, m_microphones, m_cameraFormats; QString m_cameraId, m_audioId, m_state = "starting", m_message, m_formatLabel; + int m_cameraFormatIndex = -1; QString m_root, m_takeId, m_clipPath, m_clipFileName; QString m_interruption; // Held while a take is in use, so another Monologue never mistakes it for an interrupted one. diff --git a/src/mediautils.cpp b/src/mediautils.cpp index 190212f..86ed951 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include double media::targetFps(double minimum, double maximum) { return std::clamp(30.0, minimum, maximum); } int media::bestFormat(const QList &formats) { @@ -28,12 +31,55 @@ int media::bestFormat(const QList &formats) { } return best; } -QCameraFormat media::bestCameraFormat(const QCameraDevice &device) { - const auto formats = device.videoFormats(); - QList choices; - for (const auto &f : formats) choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())}); - const int index = bestFormat(choices); - return index < 0 ? QCameraFormat() : formats[index]; +static QString pixelName(int format) { + switch (format) { + case int(QVideoFrameFormat::Format_Jpeg): return "MJPEG"; + case int(QVideoFrameFormat::Format_YUYV): return "YUYV"; + case int(QVideoFrameFormat::Format_NV12): return "NV12"; + default: { + const auto meta = QMetaEnum::fromType(); + const char *key = meta.valueToKey(format); + QString name = key ? QString::fromLatin1(key) : QString::number(format); + if (name.startsWith("Format_")) name.remove(0, 7); + return name; + }} +} +static QString fpsLabel(double minimum, double maximum) { + // The app requests 30 fps clamped to what the format supports, so show that + // rate rather than the camera's full advertised range. + const double rate = media::targetFps(minimum, maximum); + return QString("%1 fps").arg(rate, 0, 'f', rate == int(rate) ? 0 : 1); +} +QString media::formatKey(const QSize &size, double minFps, double maxFps, int pixelFormat) { + return QString("%1x%2:%3-%4:%5") + .arg(size.width()).arg(size.height()) + .arg(minFps, 0, 'f', 2).arg(maxFps, 0, 'f', 2) + .arg(pixelFormat); +} +QString media::formatLabel(const QSize &size, double minFps, double maxFps, int pixelFormat) { + return QString("%1 × %2 · %3 · %4") + .arg(size.width()).arg(size.height()) + .arg(pixelName(pixelFormat)).arg(fpsLabel(minFps, maxFps)); +} +int media::chooseFormat(const QStringList &keys, const QString &saved, const QString &best) { + for (const auto &wanted : {saved, best}) { + if (wanted.isEmpty()) continue; + const int index = keys.indexOf(wanted); + if (index >= 0) return index; + } + return keys.isEmpty() ? -1 : 0; +} +QList media::sortedFormats(const QCameraDevice &device) { + auto formats = device.videoFormats(); + std::sort(formats.begin(), formats.end(), [](const QCameraFormat &a, const QCameraFormat &b) { + const qint64 areaA = qint64(a.resolution().width()) * a.resolution().height(); + const qint64 areaB = qint64(b.resolution().width()) * b.resolution().height(); + if (areaA != areaB) return areaA > areaB; + if (a.maxFrameRate() != b.maxFrameRate()) return a.maxFrameRate() > b.maxFrameRate(); + if (a.minFrameRate() != b.minFrameRate()) return a.minFrameRate() > b.minFrameRate(); + return int(a.pixelFormat()) < int(b.pixelFormat()); + }); + return formats; } double media::peak(const QByteArray &data, const QAudioFormat &format) { double result = 0; diff --git a/src/mediautils.h b/src/mediautils.h index 17645d3..7ac5568 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -10,8 +10,15 @@ namespace media { struct Format { QSize size; double minFps; double maxFps; int pixelFormat; }; int bestFormat(const QList &formats); -QCameraFormat bestCameraFormat(const QCameraDevice &device); double targetFps(double minimum, double maximum); +// Stable identity and display text for one advertised camera format. +QString formatKey(const QSize &size, double minFps, double maxFps, int pixelFormat); +QString formatLabel(const QSize &size, double minFps, double maxFps, int pixelFormat); +// The device's formats, biggest first, so the picker reads naturally. +QList sortedFormats(const QCameraDevice &device); +// Index into keys of the format to open: the one saved for this camera if it is +// still offered, otherwise the default pick, otherwise the first. -1 if there are none. +int chooseFormat(const QStringList &keys, const QString &saved, const QString &best); double peak(const QByteArray &data, const QAudioFormat &format); QString copyAtomically(const QString &source, const QString &destination); QString normalizeMp4(const QString &path, double fps); diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 19745fe..39eb4b2 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "mediautils.h" #include "theme.h" @@ -43,6 +44,21 @@ private slots: QCOMPARE(media::bestFormat({}),-1); QCOMPARE(media::targetFps(15,15),15.0); } + void formatKeys() { + QCOMPARE(media::formatLabel({1920,1080},30,30,int(QVideoFrameFormat::Format_Jpeg)),QString("1920 × 1080 · MJPEG · 30 fps")); + QCOMPARE(media::formatLabel({640,480},15,30,int(QVideoFrameFormat::Format_YUYV)),QString("640 × 480 · YUYV · 30 fps")); + QCOMPARE(media::formatLabel({2560,1472},2,2,int(QVideoFrameFormat::Format_YUYV)),QString("2560 × 1472 · YUYV · 2 fps")); + QVERIFY(media::formatKey({1920,1080},30,30,1)!=media::formatKey({1920,1080},30,60,1)); + QVERIFY(media::formatKey({1920,1080},30,30,1)!=media::formatKey({1920,1080},30,30,2)); + QCOMPARE(media::formatKey({1920,1080},30,30,1),media::formatKey({1920,1080},30,30,1)); } + void formatChoice() { + const QStringList keys{"a","b","c"}; + QCOMPARE(media::chooseFormat(keys,"c","a"),2); // a saved choice wins + QCOMPARE(media::chooseFormat(keys,"","b"),1); // nothing saved: the default + QCOMPARE(media::chooseFormat(keys,"gone","b"),1);// saved format no longer offered: the default, not the first + QCOMPARE(media::chooseFormat(keys,"gone","also gone"),0); + QCOMPARE(media::chooseFormat({},"a","b"),-1); + } void clockExcludesPauses() { TakeClock c; c.start(1000000); QVERIFY(!c.position(999999)); diff --git a/tests/ui_tests.cpp b/tests/ui_tests.cpp index 85371be..e590afa 100644 --- a/tests/ui_tests.cpp +++ b/tests/ui_tests.cpp @@ -26,6 +26,8 @@ QtObject { property string message: "" property string formatLabel: "3840 × 2160 · 30 fps recording" property var cameras: [{label: "Test camera"}] + property var cameraFormats: [{label: "1920 × 1080 · MJPEG · 30 fps"}] + property int cameraFormatIndex: 0 property var microphones: [{label: "Test microphone"}] property int cameraIndex: 0 property int microphoneIndex: 0 @@ -63,6 +65,7 @@ QtObject { function toggleRecording() { toggles++; state = state === "ready" || state === "paused" ? "recording" : "paused"; changed() } function finish() { state = "finished"; changed() } function selectCamera(i) {} + function selectCameraFormat(i) {} function selectMicrophone(i) {} function newRecording() { newRecordings++; state = "ready"; changed() } function closeTake() {}