Skip to content

Instantly share code, notes, and snippets.

@nondanee
Last active May 4, 2023 09:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save nondanee/f157bbbccecfe29e48d87273cd02e213 to your computer and use it in GitHub Desktop.
Save nondanee/f157bbbccecfe29e48d87273cd02e213 to your computer and use it in GitHub Desktop.
script for vscode ffmpeg lib replacement
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author: Nzix
"""
import os, shutil, platform, subprocess
import re, zipfile, json
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
try:
import urllib.request as urllib
except:
import urllib
shell = lambda command, cwd = None: subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, cwd = cwd).stdout.read().decode().strip()
installation = ''
possibilities = []
electron_temp = 'electron.temp.zip'
system = {'Windows': 'win32', 'Linux': 'linux', 'Darwin': 'darwin'}[platform.system()]
cli = {'win32': 'bin', 'linux': 'bin', 'darwin': 'Contents/Resources/app/bin'}
lib = {'win32': 'ffmpeg.dll', 'linux': 'libffmpeg.so', 'darwin': 'Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Libraries/libffmpeg.dylib'}
if system == 'win32':
if 'PROGRAMW6432' in os.environ:
possibilities.append(os.environ['PROGRAMW6432'])
if 'PROGRAMFILES(X86)' in os.environ:
possibilities.append(os.environ['PROGRAMFILES(X86)'])
if 'PROGRAMFILES' in os.environ:
possibilities.append(os.environ['PROGRAMFILES'])
if 'LOCALAPPDATA' in os.environ:
possibilities.append(os.path.join(os.environ['LOCALAPPDATA'], 'Programs'))
possibilities = [os.path.join(path, 'Microsoft VS Code') for path in possibilities]
where_code = shell('where code 2> nul').split('\r\n')[0]
if where_code:
possibilities.append(os.path.abspath(os.path.join(os.path.realpath(where_code), os.path.pardir, os.path.pardir)))
elif system == 'linux':
which_code = shell('which code')
if which_code:
possibilities.append(os.path.abspath(os.path.join(os.path.realpath(which_code), os.path.pardir, os.path.pardir)))
elif system == 'darwin':
application = '/Applications/Visual Studio Code.app'
if os.path.exists(application):
possibilities.append(application)
if not installation:
possibilities = list(set(possibilities))
for path in possibilities:
if os.path.exists(path):
installation = path
break
assert installation
vscode_version = shell(('./' if system != 'win32' else '') + 'code -v --user-data-dir="."', os.path.join(installation, cli[system])).split()
print('vscode {version} {arch}'.format(version = vscode_version[0], arch = vscode_version[-1]))
try:
with open(os.path.join(installation, 'resources', 'app', 'package.json'), 'r') as f: package_json = json.loads(f.read())
electron_version = package_json['devDependencies']['electron']
except:
yarnrc = urllib.urlopen('https://raw.githubusercontent.com/Microsoft/vscode/{version}/.yarnrc'.format(version = vscode_version[0])).read().decode()
electron_version = re.search(r'target "([^"]+)"', yarnrc).group(1)
print('electron {version}'.format(version = electron_version))
urllib.urlretrieve('https://cdn.npmmirror.com/binaries/electron/v{version}/electron-v{version}-{system}-{arch}.zip'.format(version = electron_version, system = system, arch = vscode_version[-1]), electron_temp)
print('download well')
local_lib = os.path.join(installation, lib[system].replace('Electron.app', '.'))
os.remove(local_lib)
try:
with zipfile.ZipFile(electron_temp) as z:
with z.open(lib[system]) as src, open(local_lib, 'wb') as dst:
shutil.copyfileobj(src, dst)
print('replace done')
except Exception as error:
print(error)
finally:
if os.path.exists(electron_temp):
os.remove(electron_temp)
print('remove temp')
@yunyoulu
Copy link

Hello @nondanee, I installed vscode to a non-standard directory so I was unable to use the script.
But I've forked and added some auto-detection that you might want to take a look at.

Thank you very much for your excellent work on the extension. It's life-saving.

@nondanee
Copy link
Author

@yunyoulu
Check
But I find a easier way to get the code.cmd path using command where instead of traversing directories in PATH.

I will update this script soon.
Thank you for your suggestion.

@nondanee
Copy link
Author

@tw7613781
先切到 su 再 run 就行

sudo su
curl https://gist.githubusercontent.com/nondanee/f157bbbccecfe29e48d87273cd02e213/raw | python

@nondanee
Copy link
Author

@ShawLyon
Copy link

mac 没声音

@yingjun2
Copy link

mac 没声音

same here. 可以正常播放,但是没有声音。

@yingjun2
Copy link

mac 没声音

已经在修理啦。你可以在这个issue下跟进。nondanee/vsc-netease-music#110

@nondanee
Copy link
Author

@yingjun2
你是 mac 吗,mac 替换后不是启动就会报错吗?难道你关了 SIP?
1.52+ 能播放但是没声音的问题已经修好了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment