Skip to content

Instantly share code, notes, and snippets.

@yunyoulu
Forked from nondanee/helper.py
Last active May 4, 2023 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yunyoulu/5f6eb8959eb269cedd54152a9e79e5ea to your computer and use it in GitHub Desktop.
Save yunyoulu/5f6eb8959eb269cedd54152a9e79e5ea 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
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':
cwd = os.getcwd()
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.append(cwd)
possibilities = [os.path.join(path, 'Microsoft VS Code') for path in possibilities]
if os.path.exists(os.path.join(cwd, 'ffmpeg.dll')): # when run in 'Microsoft VS Code' directory
possibilities.append(cwd)
# Search the %PATH% environment variable for VS Code
for path in os.environ['PATH'].split(';'):
# The first item in %PATH% that contain 'code.cmd' should be 'Microsoft VS Code\bin'
if os.path.exists(os.path.join(path, 'code.cmd')):
possibilities.append(os.path.normpath(
os.path.join(path, os.path.pardir)
))
break
elif system == 'linux':
which_code = shell('which code')
if which_code:
possibilities.append(os.path.abspath(os.path.join(os.path.realpath(which_code), '../../')))
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
ch = input(f'Microsoft VS Code installation directory detected at {installation}. Confirm?[Y/n]')
if ch not in {'Y', 'y', ''}:
print('Aborted')
exit()
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]))
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))
print('downloading electron')
# For overseas users, choose GitHub source manually below:
urllib.urlretrieve('https://npm.taobao.org/mirrors/electron/{version}/electron-v{version}-{system}-{arch}.zip'.format(version = electron_version, system = system, arch = vscode_version[-1]), electron_temp)
#urllib.urlretrieve('https://github.com/electron/electron/releases/download/v{version}/electron-v{version}-{system}-{arch}.zip'.format(version = electron_version, system = system, arch = vscode_version[-1]), electron_temp)
print('electron download')
try:
with zipfile.ZipFile(electron_temp) as z:
with z.open(lib[system]) as src, open(os.path.join(installation, lib[system].replace('Electron.app', '.')), '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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment