Solve 2 transcription problems: Video input and big input

オープン
#472 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@danafon がすでに取り組んでいます。

2026年12月1日 から。

  • #473 @danafon による — オープン

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
php

調査の方向性

Start with OpenAiAPIService::transcribeFile() and the existing size check in AudioToTextSubtitlesProvider; compare the movie preview approach in lib/private/Preview/Movie.php. The issue proposes a new lib/Service/AudioTranscodingService.php, admin settings, and tests for transcoding decisions, ffmpeg handling, and output formats. First clarify the open questions about target format and segmentation; done means video and oversized audio are handled safely, with the 25 MB limit and subtitle timing behavior covered.

索引モデルが issue の本文から書いたものです。

説明

bug enhancement priority: high

TL;DR: We could try to use ffmpeg to extract the audio track of a video input. We could also transcode the audio to something light like 32 Kbps MP3 which is enough for voice content and safe in term of compatibility with the services we can connect to.
We could just fail if ffmpeg is not found.

Optional but quite necessary in the long run: If the transcoded audio is still > 25 MB, we could cut it into chunks, make one request per chunk, and reassemble the chunk transcriptions. For core:audio2text:subtitles, this is a little bit tricky to reassemble subtitle files but doable: we need to accurately shift the timestamps.

The following text was generated:

Audio extraction & transcoding for the transcription providers

Problem

The task-processing providers for speech-to-text (AudioToTextProvider, AudioToTextSubtitlesProvider, AudioToAudioTranslateProvider, AudioToAudioChatProvider) all converge on OpenAiAPIService::transcribeFile(), which sends the file content to the API as-is. Two consequences:

  1. Video files are sent untouched. OpenAI's Whisper API accepts mp4/webm containers, but many self-hosted or OpenAI-compatible backends only accept real audio formats and reject them.
  2. Large files fail. The API limit is 25 MB. An hour of compressed stereo video is easily 1 to 2 GB, and even a long audio recording often exceeds 25 MB. Today users just get a hard error (see the size check in AudioToTextSubtitlesProvider).

Whisper itself always downsamples to 16 kHz mono, so everything above that is wasted bytes for our use case. Re-encoding to 16 kHz mono before upload loses nothing for transcription quality.

Proposed solution

Extract the audio stream from video files and transcode audio to a compact format before uploading, using ffmpeg on the Nextcloud server. This is the same approach core uses for movie previews (lib/private/Preview/Movie.php).

Typical results (16 kHz mono):

Input Output format Size
1 h 1080p H.264 video (~1.5 GB) mp3 48 kbps ~21 MB
1 h stereo 256 kbps mp3 (~115 MB) mp3 48 kbps ~21 MB
1 h 1080p video opus 24 kbps ~11 MB
1 h WAV (~300 MB) flac ~30-40 MB

1. Detecting when to transcode

In the pre-processing step (below), decide based on the node's mime type and size:

  • video/* → extract audio (-vn) and compress → always transcode.
  • audio/* under 25 MB in a format the API accepts (see the allowlist in AudioToTextSubtitlesProvider) → send as-is (current fast path, no CPU cost).
  • audio/* over 25 MB, or in an exotic container → transcode.
  • Anything else (mime not audio/ or video/) → current validation error.

FileFormatDetector::getAudioExtension() (magic-byte based) can detect the actual format of the source bytes when the stored mime type is unreliable.

2. ffmpeg commands

Extraction and compression happen in one pass, no intermediate file needed:

# default target: mp3, safe for every Whisper-compatible API
ffmpeg -y -i input.mp4 -vn -ac 1 -ar 16000 -c:a libmp3lame -b:a 48k output.mp3

# smaller option (verify the backend accepts ogg/oga first)
ffmpeg -y -i input -vn -ac 1 -ar 16000 -c:a libopus -b:a 24k output.ogg

# lossless option (no quality debate, ~50-60% of WAV)
ffmpeg -y -i input -vn -ac 1 -ar 16000 -c:a flac output.flac
  • -vn drops the video stream (this alone handles the video case).
  • -ac 1 -ar 16000 downmixes to mono / 16 kHz, exactly what Whisper does internally, so no information relevant to transcription is lost.
  • Target format and bitrate should be a config choice; mp3 48k is the recommended default.

3. Implementation

New service: lib/Service/AudioTranscodingService.php

Responsibilities:

  • Server::get(IBinaryFinder::class)->findBinaryPath('ffmpeg') to locate the binary (result is cached by core). Distinguish "not installed" from "failed to run" in logs.
  • Write the source to a temp file via Server::get(ITempManager::class)->getTemporaryFile(); run proc_open with an array of arguments (never a shell string, so no escaping issues); read stderr before stdout to avoid the 64 KB pipe-buffer deadlock core already documented in Movie.php; always clean up temp files in finally.
  • Enforce a timeout (e.g. 10 min, configurable) and kill the process if exceeded.
  • Return the transcoded content plus the detected extension (reuse FileFormatDetector::getAudioExtension() on the output bytes).
Hook point: OpenAiAPIService::transcribeFile()

transcribeFile() is the single funnel for all four providers, so the logic belongs there (or in a method it delegates to), not in the providers:

File → mime/size decision → [transcode if needed] → transcribe(content)
  • transcribeBase64Mp3() (audio-recorder input) stays untouched, since it is already a small mp3.
  • The quota accounting in transcribe() is duration-based (parsed from response timestamps), so transcoding does not affect it.
Filename detail

request() currently hardcodes the multipart filename file.mp3 for the audio part (around line 1470 of OpenAiAPIService.php). That happens to match the default target format, but it should derive from the actual bytes/extension so non-mp3 targets don't get mislabeled.

Admin configuration

Settings in the existing admin section (lib/Settings/Admin.php / OpenAiSettingsService):

  • Transcoding mode: auto (default, transcode only when needed per the rules above) / always (helpful to shrink uploads even when under the limit) / off.
  • ffmpeg binary path: optional override; falls back to IBinaryFinder.
  • Target format & bitrate: default mp3 / 48k.

4. Edge cases and fallbacks

  • ffmpeg not installed: degrade gracefully. Audio input under the limit works exactly as today; video input or oversized audio gets a clear user-facing error ("ffmpeg is required for video transcription / files over 25 MB"). Log a one-time warning at the admin level.
  • Output still > 25 MB (very long recordings): fall back to time-based segmentation (-f segment -segment_time 1200), transcribe chunks sequentially, concatenate text. For subtitles, timestamps must be offset-corrected per segment. This is the most complex part and could be phase 2; for phase 1, a clear error is acceptable.
  • Encrypted files: fine, because File::getContent() returns decrypted content. The temp-file route works (unlike core's direct-download optimization).
  • Quota check order: the existing quota check in transcribe() happens before any upload; transcoding costs CPU locally, so a cheap quota-rejected task should not transcode first. Keep the decision order: quota → transcode → upload.

5. Security considerations

  • proc_open with array args only; no user-controlled strings reach the command line (paths are temp files we created).
  • Restrict the input to audio/* / video/* before touching ffmpeg.
  • Timeouts and temp-file cleanup mandatory; never let a hung ffmpeg pin a task-processing worker.
  • Admin-provided binary path: validate it points to an executable; IBinaryFinder already restricts to standard paths.

6. Testing

  • Unit tests (tests/): mock IBinaryFinder and the temp manager; cover the mime/size decision matrix, command construction, "ffmpeg missing" fallback, extension detection of the output bytes. When adding the new PHP file, run build/autoloaderchecker.sh.
  • Live validation: transcribe an .mp4 from a back end that rejects video containers, and a >25 MB audio file, with and without ffmpeg installed; confirm the 25 MB error path, quota bookkeeping, and subtitle timestamps for a video source.

Open questions

  • Target format default: mp3 (safe) vs opus (smallest, backend-dependent) vs flac (lossless)?
  • Should segmentation + timestamp offsetting be phase 2?
  • Should AudioToAudioTranslateProvider also use transcoded audio as its translation input (it's speech-to-text based) while keeping the output audio path untouched? Presumably yes.

Assisted by OpenCode:GLM-5.3-Flash

主要言語
PHP
スター
78
フォーク
42
平均マージ
2日 21時間
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nextcloud/integration_openai のほかの issue

nextcloud/integration_openai の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。