aiohttp
免费Use when building Python async HTTP services or clients with aiohttp - web server routing, middleware, WebSocket, SSE, streaming, client sessions, pytest-aiohttp testing, or troubleshooting SSL and timeout issues
中文简介正在准备中。当前显示原文说明。
Use when adding multimedia to PyQt/PySide6 apps - audio and video playback with QMediaPlayer, camera capture with QCamera and QMediaCaptureSession, audio/video recording with QMediaRecorder, GStreamer backend setup, or codec and platform compatibility issues
中文简介正在准备中。当前显示原文说明。
查看安装方法安装前可以查看交给 AI 代理的指令内容。
Audio and video playback, camera capture, and media recording in PyQt6/PySide6.
Qt Multimedia provides classes for audio, video, and camera functionality:
Backend by platform:
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget, QSlider, QLabel
from PyQt6.QtCore import Qt, QUrl
import sys
class AudioPlayer(QWidget):
def __init__(self):
super().__init__()
self.player = QMediaPlayer()
self.audio_output = QAudioOutput()
self.player.setAudioOutput(self.audio_output)
self.play_btn = QPushButton("Play")
self.volume_slider = QSlider(Qt.Orientation.Horizontal)
self.volume_slider.setRange(0, 100)
layout = QVBoxLayout()
layout.addWidget(self.play_btn)
layout.addWidget(self.volume_slider)
self.setLayout(layout)
self.play_btn.clicked.connect(self.player.play)
self.volume_slider.valueChanged.connect(
lambda v: self.audio_output.setVolume(v / 100)
)
def load_file(self, filepath):
self.player.setSource(QUrl.fromLocalFile(filepath))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = AudioPlayer()
window.show()
sys.exit(app.exec())
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QSlider, QLabel
)
from PyQt6.QtCore import Qt, QUrl
class VideoPlayer(QWidget):
def __init__(self):
super().__init__()
self.player = QMediaPlayer()
self.audio_output = QAudioOutput()
self.player.setAudioOutput(self.audio_output)
self.video_widget = QVideoWidget()
self.player.setVideoOutput(self.video_widget)
self.play_btn = QPushButton("Play")
self.position_slider = QSlider(Qt.Orientation.Horizontal)
self.time_label = QLabel("00:00 / 00:00")
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
layout.addWidget(self.play_btn)
layout.addWidget(self.position_slider)
layout.addWidget(self.time_label)
self.setLayout(layout)
self.play_btn.clicked.connect(self.player.play)
self.player.positionChanged.connect(self.position_changed)
self.player.durationChanged.connect(self.duration_changed)
def load_video(self, filepath):
self.player.setSource(QUrl.fromLocalFile(filepath))
def position_changed(self, position):
self.position_slider.setValue(position)
def duration_changed(self, duration):
self.position_slider.setRange(0, duration)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = VideoPlayer()
window.show()
sys.exit(app.exec())
Key methods:
play(), pause(), stop() - Playback controlsetPosition(ms), seek(seconds) - Position controlplaybackState() - Returns PlayingState, PausedState, or StoppedStatefrom PyQt6.QtMultimedia import QCamera, QMediaDevices
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
class CameraViewer(QWidget):
def __init__(self):
super().__init__()
cameras = QMediaDevices.videoInputs()
if not cameras:
print("No cameras available")
return
self.camera = QCamera(cameras[0])
self.video_widget = QVideoWidget()
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
self.setLayout(layout)
self.camera.setVideoOutput(self.video_widget)
self.camera.start()
def closeEvent(self, event):
self.camera.stop()
super().closeEvent(event)
from PyQt6.QtMultimedia import QCamera, QMediaCaptureSession, QImageCapture
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton
class CameraCapture(QWidget):
def __init__(self):
super().__init__()
self.camera = QCamera()
self.capture_session = QMediaCaptureSession()
self.capture_session.setCamera(self.camera)
self.video_widget = QVideoWidget()
self.capture_session.setVideoOutput(self.video_widget)
self.image_capture = QImageCapture()
self.capture_session.setImageCapture(self.image_capture)
self.capture_btn = QPushButton("Capture Photo")
self.capture_btn.clicked.connect(self.capture_photo)
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
layout.addWidget(self.capture_btn)
self.setLayout(layout)
self.camera.start()
def capture_photo(self):
self.image_capture.captureToFile()
from PyQt6.QtMultimedia import QCamera, QMediaCaptureSession, QMediaRecorder
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel
from PyQt6.QtCore import QUrl
class MediaRecorder(QWidget):
def __init__(self):
super().__init__()
self.camera = QCamera()
self.capture_session = QMediaCaptureSession()
self.capture_session.setCamera(self.camera)
self.video_widget = QVideoWidget()
self.capture_session.setVideoOutput(self.video_widget)
self.recorder = QMediaRecorder()
self.capture_session.setRecorder(self.recorder)
self.record_btn = QPushButton("Start Recording")
self.status_label = QLabel("Ready")
layout = QVBoxLayout()
layout.addWidget(self.video_widget)
layout.addWidget(self.record_btn)
layout.addWidget(self.status_label)
self.setLayout(layout)
self.record_btn.clicked.connect(self.toggle_recording)
self.recorder.recorderStateChanged.connect(self.update_status)
def toggle_recording(self):
if self.recorder.recorderState() == QMediaRecorder.RecorderState.RecordingState:
self.recorder.stop()
else:
self.recorder.setOutputLocation(QUrl.fromLocalFile("output.mp4"))
self.recorder.record()
def update_status(self, state):
states = {
QMediaRecorder.RecorderState.StoppedState: "Stopped",
QMediaRecorder.RecorderState.RecordingState: "Recording",
QMediaRecorder.RecorderState.PausedState: "Paused"
}
self.status_label.setText(states.get(state, "Unknown"))
# Ubuntu/Debian - core plugins
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly
# Additional codec support
sudo apt-get install gstreamer1.0-libav
| Issue | Solution |
|---|---|
| No audio/video output | Check QMediaDevices.audioOutputs() and videoOutputs() |
| No cameras found | Check QMediaDevices.videoInputs() |
| Format not supported | Install codec pack (Windows) or GStreamer plugins (Linux) |
| Playback fails | Check player.error() for specific error code |
# ✅ Check device availability first
from PyQt6.QtMultimedia import QMediaDevices
if not QMediaDevices.audioInputs():
print("No microphone available")
# ✅ Set output location before recording
recorder.setOutputLocation(QUrl.fromLocalFile(path))
recorder.record()
# ✅ Clean up on close
def closeEvent(self, event):
self.camera.stop()
self.recorder.stop()
super().closeEvent(event)
For detailed implementations, see the reference files:
references/media-pipeline.md) - QMediaPlayer + QVideoWidget, QCamera + QMediaCaptureSession, QMediaRecorder setupreferences/codecs-platforms.md) - Platform-specific codec support, installation guides, format compatibility还没有评价。欢迎分享你的使用感受。
无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。
还没有评论。来写第一条吧。
Use when building Python async HTTP services or clients with aiohttp - web server routing, middleware, WebSocket, SSE, streaming, client sessions, pytest-aiohttp testing, or troubleshooting SSL and timeout issues
中文简介正在准备中。当前显示原文说明。
Use when doing structural code search and rewriting - ast-grep linting, refactoring, multi-language patterns
中文简介正在准备中。当前显示原文说明。
Use when building GBA games with the BPCore Lua engine - entity, sprite and tilemap functions, SRAM save and load, link cable multiplayer protocol, camera and scrolling, or optimization patterns
中文简介正在准备中。当前显示原文说明。
Use when running background tasks with Celery - worker and broker configuration (Redis, RabbitMQ), task routing by name vs queue, chains/groups/chords, retry patterns (autoretry_for, retry_backoff), acks_late semantics, failure detection, and monitoring with Flower
中文简介正在准备中。当前显示原文说明。
Use when building Django applications - security hardening, authentication and permissions, ORM optimization, PostgreSQL features, Django 6.0, migrations, testing, and ecosystem libraries
中文简介正在准备中。当前显示原文说明。
Use when customizing Django Admin - save_formset, get_search_results, formsets, queryset optimization, db_index, custom URLs
中文简介正在准备中。当前显示原文说明。