fix: 🐛 Fix device support audio and video encoding cannot be parsed

This commit is contained in:
viarotel 2024-11-07 15:14:52 +08:00
parent b5809937d6
commit 9b7f6a98c2
4 changed files with 80 additions and 24 deletions

View File

@ -56,3 +56,73 @@ export function getDisplayOverlay(serial) {
|| '' || ''
return value return value
} }
/**
* Parse scrcpy codec output into structured data
* @param {*} rawText
* @returns
*/
export function parseScrcpyCodecList(rawText) {
try {
const result = {
video: [],
audio: [],
}
// 分行处理
const lines = rawText.split('\n')
// 遍历每一行
for (const line of lines) {
const trimmedLine = line.trim()
// 跳过空行和不包含编码器信息的行
if (!trimmedLine || !trimmedLine.startsWith('--')) {
continue
}
// 提取所有的键值对
const pairs = trimmedLine.match(/--[\w-]+=[\w.-]+/g)
if (!pairs || pairs.length < 2)
continue
// 将键值对转换为对象
const info = pairs.reduce((acc, pair) => {
const [key, value] = pair.substring(2).split('=')
acc[key] = value
return acc
}, {})
// 根据键名判断类型并保存数据
if (info['video-codec'] && info['video-encoder']) {
result.video.push({
type: 'video',
codec: info['video-codec'],
encoder: info['video-encoder'],
})
}
else if (info['audio-codec'] && info['audio-encoder']) {
result.audio.push({
type: 'audio',
codec: info['audio-codec'],
encoder: info['audio-encoder'],
})
}
}
// 验证结果是否为空
if (result.video.length === 0 && result.audio.length === 0) {
throw new Error('No valid codec information found in the log content')
}
return result
}
catch (error) {
console.error('Error parsing codec information:', error)
return {
video: [],
audio: [],
error: error.message,
}
}
}

View File

@ -5,7 +5,7 @@ import appStore from '$electron/helpers/store.js'
import { sleep } from '$renderer/utils/index.js' import { sleep } from '$renderer/utils/index.js'
import commandHelper from '$renderer/utils/command/index.js' import commandHelper from '$renderer/utils/command/index.js'
import { getDisplayOverlay, parseScrcpyAppList } from './helper.js' import { getDisplayOverlay, parseScrcpyAppList, parseScrcpyCodecList } from './helper.js'
let adbkit let adbkit
@ -84,26 +84,12 @@ async function execShell(command) {
async function getEncoders(serial) { async function getEncoders(serial) {
const res = await execShell(`--serial="${serial}" --list-encoders`) const res = await execShell(`--serial="${serial}" --list-encoders`)
const stdout = res.stdout const stdout = res.stdout
// 提取视频编码器列表 const value = parseScrcpyCodecList(stdout)
const videoEncoderRegex
= /--video-codec=([\w-]+)\s+--video-encoder='([^']+)'/g
const videoEncoders = [...stdout.matchAll(videoEncoderRegex)].map(
([, codec, encoder]) => ({ decoder: codec, encoder }),
)
// 提取音频编码器列表 console.log('value', value)
const audioEncoderRegex
= /--audio-codec=([\w-]+)\s+--audio-encoder='([^']+)'/g
const audioEncoders = [...stdout.matchAll(audioEncoderRegex)].map(
([, codec, encoder]) => ({ decoder: codec, encoder }),
)
const value = {
audio: audioEncoders,
video: videoEncoders,
}
return value return value
} }

View File

@ -53,8 +53,8 @@ export default {
}, },
set(value) { set(value) {
this.$emit('update:model-value', value) this.$emit('update:model-value', value)
const [decoder, encoder] = value.split(' & ') const [codec, encoder] = value.split(' & ')
this.preferenceData['--audio-codec'] = decoder this.preferenceData['--audio-codec'] = codec
this.preferenceData['--audio-encoder'] = encoder this.preferenceData['--audio-encoder'] = encoder
}, },
}, },
@ -71,7 +71,7 @@ export default {
const res = await this.$scrcpy.getEncoders(deviceId) const res = await this.$scrcpy.getEncoders(deviceId)
this.deviceOptions = res?.audio?.map((item) => { this.deviceOptions = res?.audio?.map((item) => {
const value = `${item.decoder} & ${item.encoder}` const value = `${item.codec} & ${item.encoder}`
return { return {
label: value, label: value,
value, value,

View File

@ -53,8 +53,8 @@ export default {
}, },
set(value) { set(value) {
this.$emit('update:model-value', value) this.$emit('update:model-value', value)
const [decoder, encoder] = value.split(' & ') const [codec, encoder] = value.split(' & ')
this.preferenceData['--video-codec'] = decoder this.preferenceData['--video-codec'] = codec
this.preferenceData['--video-encoder'] = encoder this.preferenceData['--video-encoder'] = encoder
}, },
}, },
@ -71,7 +71,7 @@ export default {
const res = await this.$scrcpy.getEncoders(deviceId) const res = await this.$scrcpy.getEncoders(deviceId)
this.deviceOptions = res?.video?.map((item) => { this.deviceOptions = res?.video?.map((item) => {
const value = `${item.decoder} & ${item.encoder}` const value = `${item.codec} & ${item.encoder}`
return { return {
label: value, label: value,
value, value,