Skip to content

Commit

Permalink
fix: 🐛 Fix device support audio and video encoding cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Nov 7, 2024
1 parent b580993 commit 9b7f6a9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
70 changes: 70 additions & 0 deletions electron/exposes/scrcpy/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,73 @@ export function getDisplayOverlay(serial) {
|| ''
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,
}
}
}
22 changes: 4 additions & 18 deletions electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import appStore from '$electron/helpers/store.js'
import { sleep } from '$renderer/utils/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

Expand Down Expand Up @@ -84,26 +84,12 @@ async function execShell(command) {

async function getEncoders(serial) {
const res = await execShell(`--serial="${serial}" --list-encoders`)

const stdout = res.stdout

// 提取视频编码器列表
const videoEncoderRegex
= /--video-codec=([\w-]+)\s+--video-encoder='([^']+)'/g
const videoEncoders = [...stdout.matchAll(videoEncoderRegex)].map(
([, codec, encoder]) => ({ decoder: codec, encoder }),
)
const value = parseScrcpyCodecList(stdout)

// 提取音频编码器列表
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,
}
console.log('value', value)

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

0 comments on commit 9b7f6a9

Please sign in to comment.