FFmpegNormalAudio

This commit is contained in:
AF 2022-04-17 15:49:01 +03:00
parent 8ccbed24bd
commit 2a74b0972f
2 changed files with 23 additions and 12 deletions

View File

@ -1,12 +1,20 @@
import shlex import shlex
import subprocess import subprocess
import time
import discord import discord
class FFmpegTorAudio(discord.FFmpegAudio): class FFmpegNormalAudio(discord.FFmpegAudio):
def __init__(self, source, *, executable='ffmpeg', pipe=False, stderr=None, before_options=None, options=None): def __init__(
args = [executable] self, source, *, executable='ffmpeg', pipe=False, stderr=None, before_options=None, options=None,
tor: bool
):
args = []
if tor:
args.append(executable)
executable = 'torify'
subprocess_kwargs = {'stdin': source if pipe else subprocess.DEVNULL, 'stderr': stderr} subprocess_kwargs = {'stdin': source if pipe else subprocess.DEVNULL, 'stderr': stderr}
if isinstance(before_options, str): if isinstance(before_options, str):
@ -21,11 +29,13 @@ class FFmpegTorAudio(discord.FFmpegAudio):
args.append('pipe:1') args.append('pipe:1')
super().__init__(source, executable='torify', args=args, **subprocess_kwargs) super().__init__(source, executable=executable, args=args, **subprocess_kwargs)
def read(self): def read(self):
ret = self._stdout.read(discord.opus.Encoder.FRAME_SIZE) ret = self._stdout.read(discord.opus.Encoder.FRAME_SIZE)
if len(ret) != discord.opus.Encoder.FRAME_SIZE: if len(ret) != discord.opus.Encoder.FRAME_SIZE:
if self._process.poll() is None:
time.sleep(.5)
return b'' return b''
return ret return ret

View File

@ -23,7 +23,7 @@ from v6d2ctx.lock_for import lock_for
from v6d2ctx.serve import serve from v6d2ctx.serve import serve
import v6d3music.extract import v6d3music.extract
import v6d3music.ffmpegtoraudio import v6d3music.ffmpegnormalaudio
from v6d3music.config import prefix, myroot from v6d3music.config import prefix, myroot
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
@ -146,13 +146,11 @@ class YTAudio(discord.AudioSource):
def set_source(self): def set_source(self):
self.schedule_duration_update() self.schedule_duration_update()
audio_class = discord.FFmpegPCMAudio self.source = v6d3music.ffmpegnormalaudio.FFmpegNormalAudio(
if self.tor:
audio_class = v6d3music.ffmpegtoraudio.FFmpegTorAudio
self.source = audio_class(
self.url, self.url,
options=self.options, options=self.options,
before_options=self.before_options() before_options=self.before_options(),
tor=self.tor
) )
def set_already_read(self, already_read: int): def set_already_read(self, already_read: int):
@ -456,6 +454,7 @@ async def cache_url(hurl: str, rurl: str, override: bool, tor: bool) -> None:
if cachable: if cachable:
print('caching', hurl) print('caching', hurl)
path = cache_root / f'{hurl}.opus' path = cache_root / f'{hurl}.opus'
tmp_path = cache_root / f'{hurl}.tmp.opus'
args = [] args = []
if tor: if tor:
args.append('torify') args.append('torify')
@ -464,7 +463,7 @@ async def cache_url(hurl: str, rurl: str, override: bool, tor: bool) -> None:
'ffmpeg', '-hide_banner', '-loglevel', 'warning', 'ffmpeg', '-hide_banner', '-loglevel', 'warning',
'-reconnect', '1', '-reconnect_at_eof', '0', '-reconnect', '1', '-reconnect_at_eof', '0',
'-reconnect_streamed', '1', '-reconnect_delay_max', '10', '-copy_unknown', '-reconnect_streamed', '1', '-reconnect_delay_max', '10', '-copy_unknown',
'-y', '-i', rurl, str(path) '-y', '-i', rurl, str(tmp_path)
] ]
) )
p = subprocess.Popen( p = subprocess.Popen(
@ -474,6 +473,7 @@ async def cache_url(hurl: str, rurl: str, override: bool, tor: bool) -> None:
code = await loop.run_in_executor(None, p.wait) code = await loop.run_in_executor(None, p.wait)
if code: if code:
raise RuntimeError(code) raise RuntimeError(code)
await loop.run_in_executor(None, tmp_path.rename, path)
await cache_db.set(f'url:{hurl}', str(path)) await cache_db.set(f'url:{hurl}', str(path))
print('cached', hurl) print('cached', hurl)
# await cache_db.set(f'cachable:{hurl}', False) # await cache_db.set(f'cachable:{hurl}', False)
@ -578,11 +578,12 @@ presets: dict[str, str] = {
'bassboost': 'bass=g=10', 'bassboost': 'bass=g=10',
'bassbooboost': 'bass=g=30', 'bassbooboost': 'bass=g=30',
'nightcore': 'asetrate=67882', 'nightcore': 'asetrate=67882',
'daycore': 'atempo=.9,aecho=1.0:0.5:20:0.5',
'пришествие анимешне': 'bass=g=15,asetrate=67882,bass=g=15', 'пришествие анимешне': 'bass=g=15,asetrate=67882,bass=g=15',
'difference': 'aeval=val(0)-val(1)|val(1)-val(0)', 'difference': 'aeval=val(0)-val(1)|val(1)-val(0)',
'mono': 'aeval=.5*val(0)+.5*val(1)|.5*val(1)+.5*val(0)', 'mono': 'aeval=.5*val(0)+.5*val(1)|.5*val(1)+.5*val(0)',
} }
allowed_presets = ['bassboost', 'bassbooboost', 'nightcore', 'mono'] allowed_presets = ['bassboost', 'bassbooboost', 'nightcore', 'daycore', 'mono']
allowed_effects = {'', *(presets[key] for key in allowed_presets)} allowed_effects = {'', *(presets[key] for key in allowed_presets)}