Add customizzable mpv parameters

This commit is contained in:
jeffvli
2022-11-20 04:46:25 -08:00
parent 01d327f9f1
commit 1c130e6c58
2 changed files with 87 additions and 6 deletions
+27 -2
View File
@@ -1,4 +1,5 @@
import { ipcMain } from 'electron'; import { ipcMain } from 'electron';
import uniq from 'lodash/uniq';
import MpvAPI from 'node-mpv'; import MpvAPI from 'node-mpv';
import { PlayerData } from '../../../../renderer/store'; import { PlayerData } from '../../../../renderer/store';
import { getMainWindow } from '../../../main'; import { getMainWindow } from '../../../main';
@@ -7,7 +8,29 @@ import './media-keys';
declare module 'node-mpv'; declare module 'node-mpv';
const BINARY_PATH = store.get('mpv_path') as string; const BINARY_PATH = store.get('mpv_path') as string | undefined;
const MPV_PARAMETERS = store.get('mpv_parameters') as Array<string> | undefined;
const DEFAULT_MPV_PARAMETERS = () => {
const parameters = [];
if (
!MPV_PARAMETERS?.includes('--gapless-audio=weak') ||
!MPV_PARAMETERS?.includes('--gapless-audio=no') ||
!MPV_PARAMETERS?.includes('--gapless-audio=yes') ||
!MPV_PARAMETERS?.includes('--gapless-audio')
) {
parameters.push('--gapless-audio=yes');
}
if (
!MPV_PARAMETERS?.includes('--prefetch-playlist=no') ||
!MPV_PARAMETERS?.includes('--prefetch-playlist=yes') ||
!MPV_PARAMETERS?.includes('--prefetch-playlist')
) {
parameters.push('--prefetch-playlist=yes');
}
return parameters;
};
const mpv = new MpvAPI( const mpv = new MpvAPI(
{ {
@@ -16,7 +39,9 @@ const mpv = new MpvAPI(
binary: BINARY_PATH || '', binary: BINARY_PATH || '',
time_update: 1, time_update: 1,
}, },
['--gapless-audio=yes', '--prefetch-playlist'] MPV_PARAMETERS
? uniq([...DEFAULT_MPV_PARAMETERS(), ...MPV_PARAMETERS])
: DEFAULT_MPV_PARAMETERS()
); );
mpv.start().catch((error: any) => { mpv.start().catch((error: any) => {
@@ -8,6 +8,8 @@ import {
Select, Select,
Slider, Slider,
Switch, Switch,
Text,
Textarea,
toast, toast,
Tooltip, Tooltip,
} from '@/renderer/components'; } from '@/renderer/components';
@@ -46,6 +48,7 @@ export const PlaybackTab = () => {
const status = usePlayerStore((state) => state.current.status); const status = usePlayerStore((state) => state.current.status);
const [audioDevices, setAudioDevices] = useState<SelectItem[]>([]); const [audioDevices, setAudioDevices] = useState<SelectItem[]>([]);
const [mpvPath, setMpvPath] = useState(''); const [mpvPath, setMpvPath] = useState('');
const [mpvParameters, setMpvParameters] = useState('');
const handleSetMpvPath = (e: File) => { const handleSetMpvPath = (e: File) => {
setLocalSetting('mpv_path', e.path); setLocalSetting('mpv_path', e.path);
@@ -58,7 +61,15 @@ export const PlaybackTab = () => {
return setMpvPath(mpvPath); return setMpvPath(mpvPath);
}; };
const getMpvParameters = async () => {
if (!isElectron()) return setMpvPath('');
const mpvParametersFromSettings = await getLocalSetting('mpv_parameters');
const mpvParameters = mpvParametersFromSettings?.join('\n');
return setMpvParameters(mpvParameters);
};
getMpvPath(); getMpvPath();
getMpvParameters();
}, []); }, []);
useEffect(() => { useEffect(() => {
@@ -82,7 +93,7 @@ export const PlaybackTab = () => {
data={[ data={[
{ {
disabled: !isElectron(), disabled: !isElectron(),
label: 'MPV', label: 'Mpv',
value: PlaybackType.LOCAL, value: PlaybackType.LOCAL,
}, },
{ label: 'Web', value: PlaybackType.WEB }, { label: 'Web', value: PlaybackType.WEB },
@@ -98,7 +109,7 @@ export const PlaybackTab = () => {
}} }}
/> />
), ),
description: 'The audio player to use for playback (desktop only)', description: 'The audio player to use for playback',
isHidden: !isElectron(), isHidden: !isElectron(),
note: note:
status === PlayerStatus.PLAYING ? 'Player must be paused' : undefined, status === PlayerStatus.PLAYING ? 'Player must be paused' : undefined,
@@ -113,10 +124,55 @@ export const PlaybackTab = () => {
onChange={handleSetMpvPath} onChange={handleSetMpvPath}
/> />
), ),
description: 'The location of your MPV executable', description: 'The location of your mpv executable',
isHidden: settings.type !== PlaybackType.LOCAL, isHidden: settings.type !== PlaybackType.LOCAL,
note: 'Restart required', note: 'Restart required',
title: 'MPV executable path', title: 'Mpv executable path',
},
{
control: (
<Stack spacing="xs">
<Textarea
autosize
defaultValue={mpvParameters}
minRows={4}
placeholder={`--gapless-playback=yes\n--prefetch-playlist=yes`}
width={225}
onBlur={(e) => {
update({
player: {
...settings,
mpv: {
...settings.mpv,
parameters: e.currentTarget.value,
},
},
});
if (isElectron()) {
setLocalSetting(
'mpv_parameters',
e.currentTarget.value.split('\n')
);
}
}}
/>
</Stack>
),
description: (
<Text $noSelect $secondary size="sm">
Options to pass to the player{' '}
<a
href="https://mpv.io/manual/stable/#audio"
rel="noreferrer"
target="_blank"
>
https://mpv.io/manual/stable/#audio
</a>
</Text>
),
isHidden: settings.type !== PlaybackType.LOCAL,
note: 'Restart required',
title: 'Mpv parameters',
}, },
{ {
control: ( control: (