Ejemplo n.º 1
0
class SimpleAudioRecorder(QAudioRecorder):
    def __init__(self, data: ConverterData, transcription: Transcription,
                 app_settings: AppSettings) -> None:
        super().__init__()
        self.app_settings = app_settings
        self.temp = data.get_temp_file()
        self.transcription = transcription
        self.file_path = os.path.join(self.temp,
                                      f'{self.transcription.id}.wav')
        self.settings = QAudioEncoderSettings()

    def start_recording(self) -> None:
        LOG_RECORDER.info("Audio recording started.")
        self.settings.setCodec('audio/pcm')
        self.settings.setChannelCount(1)
        self.settings.setBitRate(96000)
        self.settings.setSampleRate(44100)
        self.settings.setQuality(self.app_settings.audio_quality)
        self.settings.setEncodingMode(QMultimedia.ConstantQualityEncoding)
        container = 'audio/x-wav'
        self.setEncodingSettings(self.settings, QVideoEncoderSettings(),
                                 container)
        self.setOutputLocation(QUrl.fromLocalFile(self.file_path))
        self.record()

    def stop_recording(self) -> Union[str, None]:
        LOG_RECORDER.info("Audio recording finished.")
        LOG_RECORDER.info(f"Audio file: {self.file_path}")
        self.stop()
        return self.file_path
Ejemplo n.º 2
0
    def startVid(self):
        self.cam.load()
        # self.camvfind.show()
        self.cam.setViewfinder(self.camvfind)
        self.cam.setCaptureMode(QCamera.CaptureVideo)
        self.cam.start()

        audio = QAudioEncoderSettings()
        audio.setCodec("audio/amr")
        audio.setQuality(QtMultimedia.QMultimedia.NormalQuality)
        video = QVideoEncoderSettings()
        # video.setCodec("video/mp4")
        video.setQuality(QtMultimedia.QMultimedia.NormalQuality)
        video.setResolution(1920, 1080)
        video.setFrameRate(30.0)
        # self.recorder.setAudioSettings(audio)
        self.recorder.setVideoSettings(video)
        self.recorder.setContainerFormat("mp4")
Ejemplo n.º 3
0
    def __setRecordParams(self):  ##设置音频输入参数
        selectedFile = self.ui.editOutputFile.text().strip()
        if (selectedFile == ""):
            QMessageBox.critical(self, "错误", "请先设置录音输出文件")
            return False

        if os.path.exists(selectedFile):
            os.remove(selectedFile)  #删除已有文件

    ##         QMessageBox.critical(self,"错误","录音输出文件被占用,无法删除")
    ##         return False

        recordFile = QUrl.fromLocalFile(selectedFile)
        self.recorder.setOutputLocation(recordFile)  #设置输出文件

        recordDevice = self.ui.comboDevices.currentText()
        self.recorder.setAudioInput(recordDevice)  #设置录入设备

        settings = QAudioEncoderSettings()  #音频编码设置
        settings.setCodec(self.ui.comboCodec.currentText())  #编码

        sampRate = int(self.ui.comboSampleRate.currentText())
        settings.setSampleRate(sampRate)  #采样率

        bitRate = int(self.ui.comboBitrate.currentText())
        settings.setBitRate(bitRate)  #比特率

        channelCount = int(self.ui.comboChannels.currentText())
        settings.setChannelCount(channelCount)  #通道数

        ##      quality=self.ui.sliderQuality.value()   #也可以
        quality = QMultimedia.EncodingQuality(self.ui.sliderQuality.value())
        settings.setQuality(quality)  #品质

        if self.ui.radioQuality.isChecked():  #编码模式为固定品质,自动决定采样率,采样点大小
            settings.setEncodingMode(QMultimedia.ConstantQualityEncoding)
        else:
            settings.setEncodingMode(
                QMultimedia.ConstantBitRateEncoding)  #固定比特率

        self.recorder.setAudioSettings(settings)  #音频设置
        return True
Ejemplo n.º 4
0
    def setupWindow(self):
        """Set up widgets in the main window and the QAudioRecorder instance."""
        # Set up two push buttons (the app's first "screen")
        self.select_path_button = QPushButton("Select Audio Path")
        self.select_path_button.setObjectName("SelectFile")
        self.select_path_button.setFixedWidth(140)
        self.select_path_button.clicked.connect(self.selectAudioPath)

        self.start_button = QPushButton()
        self.start_button.setObjectName("StartButton")
        self.start_button.setEnabled(False)
        self.start_button.setFixedSize(105, 105)
        self.start_button.clicked.connect(self.startRecording)

        # Set up the labels and stop button (the app's second "screen")
        self.recording_label = QLabel("Recording...")
        self.recording_label.setFont(QFont("Helvetica [Cronyx]", 32))
        self.recording_label.setVisible(False)
        self.recording_label.setAlignment(Qt.AlignHCenter)
        self.time_label = QLabel("00:00")
        self.time_label.setFont(QFont("Helvetica [Cronyx]", 18))
        self.time_label.setObjectName("Time")
        self.time_label.setVisible(False)
        self.time_label.setAlignment(Qt.AlignHCenter)

        self.stop_button = QPushButton()
        self.stop_button.setObjectName("StopButton")
        self.stop_button.setFixedSize(65, 65)
        self.stop_button.setVisible(False)
        self.stop_button.clicked.connect(self.stopRecording)

        # Set up the main layout
        self.main_v_box = QVBoxLayout()
        self.main_v_box.setAlignment(Qt.AlignHCenter)
        self.main_v_box.addWidget(self.select_path_button)
        # Force select_path_button to be centered in the window
        self.main_v_box.setAlignment(self.select_path_button, Qt.AlignCenter)
        self.main_v_box.addStretch(3)
        self.main_v_box.addWidget(self.start_button)
        self.main_v_box.setAlignment(self.start_button, Qt.AlignCenter)
        self.main_v_box.addWidget(self.recording_label)
        self.main_v_box.addWidget(self.time_label)
        self.main_v_box.addStretch(3)
        self.main_v_box.addWidget(self.stop_button)
        self.main_v_box.setAlignment(self.stop_button, Qt.AlignCenter)
        self.main_v_box.addStretch(1)

        self.setLayout(self.main_v_box)  # Set the beginning layout

        # Specify audio encoder settings
        audio_settings = QAudioEncoderSettings()
        # Depending upon your platform or the codecs that you have available, you
        # will need to change the codec. For Linux users if you are having issues
        # use "audio/x-vorbis", and then select the .ogg extension when saving
        # the file
        audio_settings.setCodec("audio/wav")
        audio_settings.setQuality(QMultimedia.HighQuality)

        # Create instance of QAudioRecorder for recording audio
        self.audio_recorder = QAudioRecorder()
        # Uncomment to discover possible codecs supported on your platform
        #print(self.audio_recorder.supportedAudioCodecs())
        self.audio_recorder.setEncodingSettings(audio_settings)
        self.audio_recorder.durationChanged.connect(self.displayTime)