{"record":{"id":"8e7016eea2efe915","repo":"harry0703/MoneyPrinterTurbo","slug":"background-music-file-exceeds-the-30-mb-limit","errorCode":null,"errorMessage":"background music file exceeds the 30 MB limit","messagePattern":"background music file exceeds the 30 MB limit","errorType":"validation","errorClass":"BgmUploadError","httpStatus":400,"severity":"error","filePath":"app/services/bgm.py","lineNumber":207,"sourceCode":"            raise BgmUploadError(\"background music upload is not seekable\") from exc\n\n        # 保留原始扩展名便于 FFmpeg 针对无容器头的 AAC 等格式选择正确的\n        # demuxer；临时文件仍放在目标目录，以保证最终 os.replace 是原子操作。\n        descriptor, temp_path = tempfile.mkstemp(\n            prefix=_INTERNAL_UPLOAD_PREFIX,\n            suffix=Path(safe_name).suffix.lower(),\n            dir=target_dir,\n        )\n        with os.fdopen(descriptor, \"wb\") as output:\n            while True:\n                chunk = source.read(_COPY_CHUNK_BYTES)\n                if not chunk:\n                    break\n                if not isinstance(chunk, (bytes, bytearray, memoryview)):\n                    raise BgmUploadError(\"background music upload must be binary\")\n                total_bytes += len(chunk)\n                if total_bytes > MAX_BGM_UPLOAD_BYTES:\n                    raise BgmUploadError(\"background music file exceeds the 30 MB limit\")\n                output.write(chunk)\n            output.flush()\n            os.fsync(output.fileno())\n\n        if total_bytes == 0:\n            raise BgmUploadError(\"background music file is empty\")\n        return safe_name, temp_path, total_bytes\n    except Exception as exc:\n        _remove_staged_file(temp_path)\n        if isinstance(exc, BgmUploadError):\n            raise\n        if isinstance(exc, OSError):\n            raise BgmServiceError(\"failed to stage background music upload\") from exc\n        raise\n    finally:\n        # Streamlit 还需要使用同一个 UploadedFile 做浏览器试听；恢复文件指针可\n        # 避免校验后播放器或最终保存读取到空内容。\n        try:","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/services/bgm.py#L189-L225","documentation":"Raised during staged copying of a background music upload when the cumulative byte count exceeds MAX_BGM_UPLOAD_BYTES (30 MB, app/services/bgm.py:16). The check happens per 1 MB chunk before writing, so at most one chunk over the limit hits disk; the temp file is then removed via _remove_staged_file and a BgmUploadError propagates. This is a server-side cap so oversized files never fully hit disk and compete with video task I/O.","triggerScenarios":"Uploading any BGM file larger than 30*1024*1024 bytes through the WebUI or API staging path (e.g. a 40 MB WAV or FLAC). The error fires as soon as total_bytes crosses the limit mid-stream, not after the full write.","commonSituations":"Uncompressed WAV/FLAC rips easily exceed 30 MB; users re-uploading a podcast or full album rip as BGM; proxy frameworks that buffer the whole body before the size check.","solutions":["Compress or trim the audio to under 30 MB (convert WAV/FLAC to MP3/AAC/OGG, or cut the clip to the video length).","Enforce the same limit client-side (Streamlit file_uploader type + a size check) so users get feedback before the upload starts.","If 30 MB is genuinely too small for your content, re-encode to a lossy format first rather than raising MAX_BGM_UPLOAD_BYTES, since the cap also protects disk and I/O."],"exampleFix":"// before (client sends 45 MB WAV)\n\n// after: re-encode before upload\nffmpeg -i input.wav -b:a 192k output.mp3  # ~13 MB for 10 min","handlingStrategy":"validation","validationCode":"MAX = 30 * 1024 * 1024\nsize = os.fseek(fh, 0, os.SEEK_END) and os.fstat(fh.fileno()).st_size or fh.seek(0)\nif os.fstat(fh.fileno()).st_size > MAX:\n    raise ClientError('file too large; compress to under 30 MB')","typeGuard":null,"tryCatchPattern":"try:\n    stage_bgm_upload(fh, filename)\nexcept BgmUploadError as e:\n    if 'exceeds the 30 MB' in str(e):\n        show_user('Compress the audio (e.g. convert WAV to MP3) and retry')","preventionTips":["Advertise the 30 MB limit in the upload UI and check Content-Length before streaming.","Prefer lossy formats (MP3/AAC/OGG) for BGM uploads — lossless rips blow the cap.","Keep the client-side cap in sync with MAX_BGM_UPLOAD_BYTES since it is the single source of truth."],"tags":["bgm","upload","size-limit","validation"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}