Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
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. Preview preserves the whole frame. A resolution menu next to the camera lets slower machines choose a smaller size, which is remembered.
- MJPEG cameras are decoded off the GUI thread and encoded as ordinary 4:2:0 H.264, so high resolutions stay playable and affordable on modest CPUs.
- 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.
Expand Down
9 changes: 9 additions & 0 deletions src/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ ApplicationWindow {
readonly property var icons: ({
camera: "M4.5 6h9a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2z M15.5 10.5l6-3.5v10l-6-3.5z",
microphone: "M12 3a3 3 0 0 1 3 3v5a3 3 0 0 1-6 0V6a3 3 0 0 1 3-3z M5.5 11a6.5 6.5 0 0 0 13 0 M12 17.5V21",
resolution: "M4 9V5h4 M20 9V5h-4 M4 15v4h4 M20 15v4h-4",
chevron: "M6 9l6 6 6-6",
back: "M4 12a8 8 0 1 0 2.4-5.7 M4 4v4.5h4.5",
play: "M8 4.5v15l12.5-7.5z",
Expand Down Expand Up @@ -418,6 +419,14 @@ ApplicationWindow {
onActivated: index => { backend.selectCamera(index); liveVideo.forceActiveFocus() }
Accessible.name: "Camera"
}
SourceChoice {
id: resolutionChoice; iconPath: win.icons.resolution
maximumTextWidth: Math.min(260, sources.width - 44)
model: backend.resolutions; currentIndex: backend.resolutionIndex
enabled: !backend.takeActive && !win.busy && backend.resolutions.length > 1
onActivated: index => { backend.selectResolution(index); liveVideo.forceActiveFocus() }
Accessible.name: "Resolution"
}
SourceChoice {
id: microphoneChoice; iconPath: win.icons.microphone
maximumTextWidth: Math.min(260, sources.width - 44)
Expand Down
29 changes: 28 additions & 1 deletion src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Backend::Backend(FilePicker *picker, bool activateHardware, QObject *parent)
m_root = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/recordings";
m_cameraId=m_settings.value("camera/id").toString();
m_audioId=m_settings.value("microphone/id").toString();
{
const auto parts=m_settings.value("camera/resolution").toString().split('x');
if(parts.size()==2) m_chosenResolution=QSize(parts[0].toInt(),parts[1].toInt());
}
m_capture.setVideoSink(&m_sink);
connect(&m_sink,&QVideoSink::videoFrameChanged,this,&Backend::receiveVideo);
if(m_activateHardware) {
Expand Down Expand Up @@ -54,6 +58,10 @@ Backend::~Backend() {
if(m_export) { m_export->disconnect(this); m_export->kill(); m_export->waitForFinished(); QFile::remove(m_exportPartial); }
}
int Backend::cameraIndex() const { return indexOf(m_cameras,m_cameraId); }
int Backend::resolutionIndex() const {
for(int i=0;i<m_resolutions.size();++i) if(m_resolutions[i].toMap()["selected"].toBool()) return i;
return 0;
}
int Backend::microphoneIndex() const { return indexOf(m_microphones,m_audioId); }
bool Backend::ready() const { return m_state=="ready" && m_cameraHealthy && (!audioEnabled() || m_audioHealthy); }
QString Backend::meterText() const {
Expand Down Expand Up @@ -124,7 +132,17 @@ void Backend::activateSources() {
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);
const auto sizes=media::cameraResolutions(selectedCamera);
const bool chosenAvailable=m_chosenResolution.isValid() && sizes.contains(m_chosenResolution) && m_chosenResolution!=sizes.value(0);
m_resolutions.clear();
for(int i=0;i<sizes.size();++i) {
const auto &size=sizes[i];
const bool selected=chosenAvailable ? size==m_chosenResolution : i==0;
m_resolutions.append(QVariantMap{{"width",size.width()},{"height",size.height()},{"selected",selected},
{"label",QString("%1 × %2%3").arg(size.width()).arg(size.height()).arg(i==0?" (maximum)":"")}});
}
emit resolutionsChanged();
m_cameraFormat=media::bestCameraFormat(selectedCamera,chosenAvailable ? m_chosenResolution : QSize());
if(m_cameraFormat.isNull()) { m_state="unavailable"; m_message="This camera advertises no supported video formats."; }
else {
m_fps=media::targetFps(m_cameraFormat.minFrameRate(),m_cameraFormat.maxFrameRate());
Expand Down Expand Up @@ -226,6 +244,15 @@ void Backend::selectCamera(int index) {
if(m_writer || index<0 || index>=m_cameras.size() || m_state=="saving" || m_probing) return;
m_cameraId=m_cameras[index].toMap()["id"].toString(); activateSources();
}
void Backend::selectResolution(int index) {
if(m_writer || index<0 || index>=m_resolutions.size() || m_state=="saving" || m_probing) return;
const auto choice=m_resolutions[index].toMap();
// The first entry is the maximum, which is stored as "no preference".
m_chosenResolution=index==0 ? QSize() : QSize(choice["width"].toInt(),choice["height"].toInt());
if(index==0) m_settings.remove("camera/resolution");
else m_settings.setValue("camera/resolution",QString("%1x%2").arg(m_chosenResolution.width()).arg(m_chosenResolution.height()));
activateSources();
}
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();
Expand Down
10 changes: 9 additions & 1 deletion src/backend.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <QSize>
#include <QObject>
#include <QMediaDevices>
#include <QMediaCaptureSession>
Expand Down Expand Up @@ -26,6 +27,8 @@ class Backend : public QObject {
Q_PROPERTY(QVariantList cameras READ cameras NOTIFY devicesChanged)
Q_PROPERTY(QVariantList microphones READ microphones NOTIFY devicesChanged)
Q_PROPERTY(int cameraIndex READ cameraIndex NOTIFY devicesChanged)
Q_PROPERTY(QVariantList resolutions READ resolutions NOTIFY resolutionsChanged)
Q_PROPERTY(int resolutionIndex READ resolutionIndex NOTIFY resolutionsChanged)
Q_PROPERTY(int microphoneIndex READ microphoneIndex NOTIFY devicesChanged)
Q_PROPERTY(bool ready READ ready NOTIFY changed)
Q_PROPERTY(bool audioEnabled READ audioEnabled NOTIFY changed)
Expand Down Expand Up @@ -54,6 +57,8 @@ class Backend : public QObject {
QString message() const { return m_message; }
QString formatLabel() const { return m_formatLabel; }
QVariantList cameras() const { return m_cameras; }
QVariantList resolutions() const { return m_resolutions; }
int resolutionIndex() const;
QVariantList microphones() const { return m_microphones; }
int cameraIndex() const;
int microphoneIndex() const;
Expand All @@ -78,6 +83,7 @@ class Backend : public QObject {
edit::Clips currentEdit() const { return m_edit; }
Q_INVOKABLE void setPreview(QObject *sink);
Q_INVOKABLE void selectCamera(int index);
Q_INVOKABLE void selectResolution(int index);
Q_INVOKABLE void selectMicrophone(int index);
Q_INVOKABLE void retry();
Q_INVOKABLE void toggleRecording();
Expand All @@ -102,6 +108,7 @@ class Backend : public QObject {
signals:
void changed();
void devicesChanged();
void resolutionsChanged();
void meterChanged();
void editChanged();
void safeToClose();
Expand Down Expand Up @@ -150,7 +157,8 @@ 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_resolutions;
QSize m_chosenResolution; // empty means the camera's maximum
QString m_cameraId, m_audioId, m_state = "starting", m_message, m_formatLabel;
QString m_root, m_takeId, m_clipPath, m_clipFileName;
QString m_interruption;
Expand Down
2 changes: 1 addition & 1 deletion src/common.pri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CONFIG += link_pkgconfig
PKGCONFIG += libpulse
PKGCONFIG += libpulse libswscale libavcodec libavutil
HEADERS += $$PWD/theme.h $$PWD/mediautils.h $$PWD/writer.h $$PWD/backend.h \
$$PWD/filepicker.h $$PWD/portalfilepicker.h $$PWD/audiocapture.h \
$$PWD/edit.h $$PWD/thumbnails.h
Expand Down
24 changes: 21 additions & 3 deletions src/mediautils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QProcess>
#include <QJsonDocument>
#include <QJsonArray>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <limits>
Expand All @@ -28,12 +29,29 @@ int media::bestFormat(const QList<Format> &formats) {
}
return best;
}
QCameraFormat media::bestCameraFormat(const QCameraDevice &device) {
QCameraFormat media::bestCameraFormat(const QCameraDevice &device, const QSize &preferred) {
const auto formats = device.videoFormats();
QList<Format> choices;
for (const auto &f : formats) choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())});
QList<int> origin;
for (int i = 0; i < formats.size(); ++i) {
const auto &f = formats[i];
if (!preferred.isEmpty() && f.resolution() != preferred) continue;
choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())});
origin.append(i);
}
// An explicit resolution that this camera lacks falls back to the maximum.
if (choices.isEmpty() && !preferred.isEmpty()) return bestCameraFormat(device);
const int index = bestFormat(choices);
return index < 0 ? QCameraFormat() : formats[index];
return index < 0 ? QCameraFormat() : formats[origin[index]];
}
QList<QSize> media::cameraResolutions(const QCameraDevice &device) {
QList<QSize> sizes;
for (const auto &f : device.videoFormats())
if (!f.resolution().isEmpty() && f.maxFrameRate() > 0 && !sizes.contains(f.resolution())) sizes.append(f.resolution());
std::sort(sizes.begin(), sizes.end(), [](const QSize &a, const QSize &b) {
return qint64(a.width()) * a.height() > qint64(b.width()) * b.height();
});
return sizes;
}
double media::peak(const QByteArray &data, const QAudioFormat &format) {
double result = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/mediautils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
namespace media {
struct Format { QSize size; double minFps; double maxFps; int pixelFormat; };
int bestFormat(const QList<Format> &formats);
QCameraFormat bestCameraFormat(const QCameraDevice &device);
QCameraFormat bestCameraFormat(const QCameraDevice &device, const QSize &preferred = {});
QList<QSize> cameraResolutions(const QCameraDevice &device);
double targetFps(double minimum, double maximum);
double peak(const QByteArray &data, const QAudioFormat &format);
QString copyAtomically(const QString &source, const QString &destination);
Expand Down
103 changes: 94 additions & 9 deletions src/writer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include "writer.h"
#include <QMetaObject>
#include <cstring>
#include <QVideoFrameFormat>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/pixfmt.h>
}
#include <QMediaFormat>
#include <QUrl>
#include <QAbstractVideoBuffer>
Expand Down Expand Up @@ -30,6 +38,54 @@ class FrameBuffer : public QAbstractVideoBuffer {
QVideoFrame m_source;
bool m_mapped=false;
};

// Webcams deliver MJPEG, which Qt's encoder decodes to 4:4:4 that hardware H.264
// encoders reject and software x264 struggles with at 1080p. Hand the encoder
// plain 8-bit 4:2:0 NV12 instead, which also plays everywhere. Runs on a worker.
QVideoFrame jpegToNv12(const QByteArray &jpeg, const QSize &size) {
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
if (!codec) return {};
AVCodecContext *decoder = avcodec_alloc_context3(codec);
AVPacket *packet = av_packet_alloc();
AVFrame *decoded = av_frame_alloc();
QVideoFrame output;
if (decoder && packet && decoded && avcodec_open2(decoder, codec, nullptr) == 0
&& av_new_packet(packet, jpeg.size()) == 0) {
memcpy(packet->data, jpeg.constData(), jpeg.size());
if (avcodec_send_packet(decoder, packet) == 0 && avcodec_receive_frame(decoder, decoded) == 0
&& decoded->width == size.width() && decoded->height == size.height()) {
QVideoFrameFormat format(size, QVideoFrameFormat::Format_NV12);
format.setColorSpace(QVideoFrameFormat::ColorSpace_BT601);
format.setColorRange(QVideoFrameFormat::ColorRange_Video);
QVideoFrame frame(format);
if (frame.map(QVideoFrame::WriteOnly)) {
thread_local SwsContext *context = nullptr;
const bool fullRange = decoded->color_range == AVCOL_RANGE_JPEG || decoded->format == AV_PIX_FMT_YUVJ420P
|| decoded->format == AV_PIX_FMT_YUVJ422P || decoded->format == AV_PIX_FMT_YUVJ444P;
AVPixelFormat source = AVPixelFormat(decoded->format);
if (source == AV_PIX_FMT_YUVJ420P) source = AV_PIX_FMT_YUV420P;
else if (source == AV_PIX_FMT_YUVJ422P) source = AV_PIX_FMT_YUV422P;
else if (source == AV_PIX_FMT_YUVJ444P) source = AV_PIX_FMT_YUV444P;
context = sws_getCachedContext(context, size.width(), size.height(), source,
size.width(), size.height(), AV_PIX_FMT_NV12, SWS_BILINEAR, nullptr, nullptr, nullptr);
uint8_t *planes[2] = {frame.bits(0), frame.bits(1)};
const int strides[2] = {frame.bytesPerLine(0), frame.bytesPerLine(1)};
int rows = 0;
if (context) {
const int *coefficients = sws_getCoefficients(SWS_CS_ITU601);
sws_setColorspaceDetails(context, coefficients, fullRange ? 1 : 0, coefficients, 0, 0, 1 << 16, 1 << 16);
rows = sws_scale(context, decoded->data, decoded->linesize, 0, size.height(), planes, strides);
}
frame.unmap();
if (rows == size.height()) output = frame;
}
}
}
av_frame_free(&decoded);
av_packet_free(&packet);
avcodec_free_context(&decoder);
return output;
}
}

void TakeClock::start(qint64 now) {
Expand Down Expand Up @@ -75,6 +131,7 @@ QList<qint64> TakeClock::joins() const {
}

Writer::Writer(QObject *parent) : QObject(parent) {
m_convertPool.setMaxThreadCount(3);
m_session.setRecorder(&m_recorder);
m_flushTimeout.setSingleShot(true);
m_flushTimeout.setInterval(10000);
Expand All @@ -93,6 +150,7 @@ static QMediaFormat recordingFormat(bool audio) {
if (audio) f.setAudioCodec(QMediaFormat::AudioCodec::AAC);
return f;
}
Writer::~Writer() { m_convertPool.waitForDone(); }
bool Writer::supported(bool audio) { return recordingFormat(audio).isSupported(QMediaFormat::Encode); }
bool Writer::start(const QString &path, const QVideoFrameFormat &format,
double fps, const QAudioFormat &audioFormat, qint64 now) {
Expand Down Expand Up @@ -137,16 +195,32 @@ void Writer::videoAt(QVideoFrame frame, qint64 position) {
const qint64 frameIndex = qRound64(position * m_fps / 1000000.0);
const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps);
if (timestamp <= m_lastVideo) return;
m_tailFrame = frame;
frame = QVideoFrame(std::make_unique<FrameBuffer>(frame));
frame.setStartTime(timestamp);
frame.setEndTime(qRound64((frameIndex + 1) * 1000000.0 / m_fps));
frame.setStreamFrameRate(m_fps);
m_lastVideo = timestamp;
// Bounded native-frame references; never build an unbounded 4K frame queue.
if (m_frames.size() >= 6) { fail("Video encoding cannot keep up at this camera's maximum resolution, so the take was stopped."); return; }
m_frames.enqueue(frame);
drain();
m_tailFrame = frame;
m_lastVideo = timestamp;
auto job = std::make_shared<Job>();
bool queued = false;
if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg) {
QByteArray jpeg;
if (frame.map(QVideoFrame::ReadOnly)) {
jpeg = QByteArray(reinterpret_cast<const char *>(frame.bits(0)), frame.mappedBytes(0));
frame.unmap();
}
if (jpeg.isEmpty()) return;
const QSize size = frame.size();
queued = true;
m_convertPool.start([this, job, jpeg, size] {
job->frame = jpegToNv12(jpeg, size);
job->ready.store(true, std::memory_order_release);
QMetaObject::invokeMethod(this, &Writer::drain, Qt::QueuedConnection);
});
} else {
job->frame = QVideoFrame(std::make_unique<FrameBuffer>(frame));
job->ready.store(true, std::memory_order_release);
}
m_frames.enqueue({job, timestamp, qRound64((frameIndex + 1) * 1000000.0 / m_fps), {}});
if (!queued) drain();
}
void Writer::audio(QByteArray data, qint64 capturedAt) {
if (m_stopping || m_failed || !m_audio || data.isEmpty()) return;
Expand Down Expand Up @@ -194,7 +268,18 @@ void Writer::appendAudio(const QByteArray &data) {
void Writer::drain() {
if (m_draining || m_failed) return;
m_draining = true;
while (!m_frames.isEmpty() && m_video->sendVideoFrame(m_frames.head())) m_frames.dequeue();
while (!m_frames.isEmpty() && m_frames.head().job->ready.load(std::memory_order_acquire)) {
auto &head = m_frames.head();
if (!head.job->frame.isValid()) { m_frames.dequeue(); continue; } // undecodable camera frame
if (!head.frame.isValid()) {
head.frame = QVideoFrame(std::make_unique<FrameBuffer>(head.job->frame));
head.frame.setStartTime(head.start);
head.frame.setEndTime(head.end);
head.frame.setStreamFrameRate(m_fps);
}
if (!m_video->sendVideoFrame(head.frame)) break;
m_frames.dequeue();
}
while (!m_buffers.isEmpty() && m_audio->sendAudioBuffer(m_buffers.head())) {
m_queuedAudioUs -= m_buffers.head().duration(); m_buffers.dequeue();
}
Expand Down
10 changes: 9 additions & 1 deletion src/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <QMediaRecorder>
#include <QVideoFrame>
#include <QQueue>
#include <QThreadPool>
#include <atomic>
#include <memory>
#include <QTimer>
#include <optional>

Expand Down Expand Up @@ -34,6 +37,7 @@ class Writer : public QObject {
Q_OBJECT
public:
explicit Writer(QObject *parent = nullptr);
~Writer() override;
bool start(const QString &path, const QVideoFrameFormat &videoFormat,
double fps, const QAudioFormat &audioFormat, qint64 now);
void video(QVideoFrame frame, qint64 capturedAt);
Expand All @@ -58,7 +62,11 @@ class Writer : public QObject {
QVideoFrameInput *m_video = nullptr;
QAudioBufferInput *m_audio = nullptr;
QAudioFormat m_audioFormat;
QQueue<QVideoFrame> m_frames;
// A video frame waiting for its (possibly off-thread) conversion to finish.
struct Job { std::atomic<bool> ready{false}; QVideoFrame frame; };
struct Pending { std::shared_ptr<Job> job; qint64 start = 0, end = 0; QVideoFrame frame; };
QQueue<Pending> m_frames;
QThreadPool m_convertPool;
QVideoFrame m_tailFrame;
QQueue<QAudioBuffer> m_buffers;
QTimer m_flushTimeout;
Expand Down
Loading