Skip to content

Commit

Permalink
happi :)
Browse files Browse the repository at this point in the history
  • Loading branch information
AWeirdDev committed Aug 22, 2023
0 parents commit 5948ca1
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
replit_ffmpeg.egg-info/
start.bat
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 JC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# replit-ffmpeg

A simple tool for installing ffmpeg, libopus (opus) and youtube dl (optional) for you on Replit.

```bash
$ pip install replit-ffmpeg
```

To use it, simply run this in your terminal:

```bash
$ replit-ffmpeg
```

That's about it. Enjoy your "FFmpeg." Yup, it sounds weird.
Binary file added dist/replit-ffmpeg-0.1.tar.gz
Binary file not shown.
31 changes: 31 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[build-system]
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"

[project]
name = "replit-ffmpeg"
version = "0.1"
description = "Installs FFmpeg for you on Replit."
keywords = ["replit", "ffmpeg", "opus", "youtube-dl"]
authors = [
{ name = "AWeirdDev", email = "[email protected]" },
]
license = { file = "LICENSE" }
readme = "README.md"
classifiers = [
"Programming Language :: Python :: 3.9",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
requires-python = ">=3.9"
dependencies = [
"rich",
"requests"
]

[project.scripts]
replit-ffmpeg = "replit_ffmpeg.installer:main"

[project.urls]
"Homepage" = "https://github.com/AWeirdScratcher/replit-ffmpeg"
"Bug Tracker" = "https://github.com/AWeirdScratcher/replit-ffmpeg/issues"
5 changes: 5 additions & 0 deletions replit_ffmpeg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .installer import main

__all__ = (
'main',
)
3 changes: 3 additions & 0 deletions replit_ffmpeg/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from replit_ffmpeg.installer import main

main()
190 changes: 190 additions & 0 deletions replit_ffmpeg/installer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import os
import shutil
import time
import requests

from rich.console import Console
from rich.markdown import Markdown

def main():
console = Console()

console.print()
console.print(
" I will install [blue]ffmpeg, opus and youtube dl (opt)[/blue] for you."
)
console.print(
" Make sure this REPL is clear so that nothing will be overwritten."
)
console.print()
_next: str = console.input(
" [green]?[/green] Continue? [blue](Yn)[/blue] "
)
console.print()

if _next.lower() not in ["y", "yes"]:
exit(1)

_plain_ans_with_ytdl = console.input(
" [green]?[/green] Would you also like me to install "
"[red]youtube dl[/red]? [blue](Yn)[/blue] "
)
console.print()

with_ytdl: bool = _plain_ans_with_ytdl.lower() in ["y", "yes"]

r = None

headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 OPR/100.0.0.0 (Edition GX-CN)"
}

def download_ffmpeg():
global r
r = requests.get("https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz", headers=headers)

console.print(
"[blue]install[/blue] ffmpeg static "
"[d white](2x attempts)[/d white]"
)

START = time.time()

for _ in range(2):
try:
download_ffmpeg()
break
except requests.exceptions.SSLError:
console.print(
"[d red]error [/d red][d] SSL error: retrying[/d]"
)

console.print(
"[blue]unpack [/blue] ffmpeg static "
"[d white]@ ffmpeg.tar.xz[/d white]"
)
with open('ffmpeg.tar.xz', 'wb') as f:
f.write(r.content)

os.system('tar -xf ffmpeg.tar.xz')

console.print(
"[blue]moving [/blue] ffmpeg, ffprobe"
)
os.system('mv ffmpeg-6.0-amd64-static/ffmpeg ffmpeg')
os.system('mv ffmpeg-6.0-amd64-static/ffprobe ffprobe')

console.print("[red]remove [/red] ffmpeg.tar.xz")
os.remove('ffmpeg.tar.xz')

# https://www.linuxfromscratch.org/blfs/view/svn/multimedia/opus.html

console.print(
"[blue]install[/blue] opus archive"
)

opus_r = requests.get("https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz", headers=headers)

with open("opus-1.3.1.tar.gz", "wb") as f:
f.write(opus_r.content)

console.print(
"[blue]unpack [/blue] opus archive "
"[d white]@ opus-1.3.1.tar.gz[/d white]"
)

os.system('tar -xf opus-1.3.1.tar.gz')
os.remove("opus-1.3.1.tar.gz")

console.print(
"[blue]command[/blue] 'configure' & 'make'"
)
console.print()

os.system("""cd opus-1.3.1 && ./configure --prefix=/usr \
--disable-static \
--docdir=/usr/share/doc/opus-1.3.1 && make""")

console.print()
console.print(
"[green]success[/green] built opus"
)
os.system("""mkdir opus && mv opus-1.3.1/.libs/* opus/""")
console.print(
"[blue]moving [/blue] 'libopus.so', 'libopus.so.0', and many other."
)

console.print()

console.print(Markdown(
"""
# It's DONE!
Congratulations! We've installed ffmpeg and opus for your REPL!
In constrast, I've:
- Installed `ffmpeg` and `ffprobe` in this directory
- Installed `opus`
- Created a dir named `opus` that contains `libopus.so` files
...that's pretty much it!
If you're making a Discord bot, whether it's `py-cord` or `discord.py`, use:
```python
import discord
discord.opus.load_opus("opus/libopus.so")
```
That's it, and have fun hacking! <3
""",
"one-dark"
))

console.print()

if with_ytdl:
console.print(
"[d blue]extra [/d blue][d] downloading yt-dlp[/d]..."
)
os.system('pip install yt-dlp --quiet')
console.print(
"[d blue]extra [/d blue][d] installed yt-dlp. "
"Use `import yt_dlp` instead of `import youtube_dl`.[/d]"
)
console.print()

console.print(
f"[blue]took {(time.time() - START):.2f}s[/blue]"
)
console.print()
console.print(
" If you want to save some space for your REPL, this might help:"
)
console.print()
console.input(
" [green]?[/green] Cleanup installed contents? [blue](Yn)[/blue] "
)

def rmdir(folder: str):
# https://stackoverflow.com/questions/185936/how-to-delete-the-contents-of-a-folder
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
console.print(
'[red]failed [/red] '
'cannot delete %s. Reason: %s' % (file_path, e)
)
os.rmdir(folder)

rmdir('opus-1.3.1')
rmdir('ffmpeg-6.0-amd64-static')

console.print("[blue]all done![/blue]")

2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description_file = README.md
28 changes: 28 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from setuptools import setup

with open("README.md", "r", encoding="utf-8") as f:
readme = f.read()

setup(
name="replit-ffmpeg",
author="AWeirdDev",
version="0.1",
license="MIT License",
description="Installs FFmpeg for you on Replit.",
long_description=readme,
long_description_content_type="text/markdown",
author_email="[email protected]",
packages=['replit_ffmpeg'],
classifiers=[
"Programming Language :: Python :: 3.9",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Environment :: Console",
],
keywords=["replit", "ffmpeg", "opus", "youtube-dl"],
entry_points={
"console_scripts": [
"replit-ffmpeg=replit_ffmpeg.installer:main"
]
}
)

0 comments on commit 5948ca1

Please sign in to comment.