Skip to content

Commit

Permalink
Post Release Improvements (#22)
Browse files Browse the repository at this point in the history
* port improvments after user feedback

* adding port to docs

* light structure changes

* WindowsApps warning

* upversion and webpage message fix
  • Loading branch information
zfleeman authored Feb 26, 2024
1 parent a8ac53d commit 0118113
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The `TwoPass()` Class showcases a 2-pass encoding methodology for the `ffmpeg-py

## Installation

This package works with Python >= 3.8. For Windows users, ensure that Python is added to your system's PATH.
This package works with Python >= 3.8. For Windows users, ensure that Python is added to your system's PATH. I highly recommend that you use the [python.org](https://python.org) Python installation.

Install this package with the following command.

Expand Down Expand Up @@ -65,7 +65,10 @@ You can edit the name of your video file if you need to trim it to a specific se
- From the top-left of your video, this example goes 255 pixels to the right, 0 pixels down, and it carves out a 1410x1080 section of the video.
- [FFmpeg crop documentation](https://ffmpeg.org/ffmpeg-filters.html#Examples-61)
- `--web`
- A Boolean flag. No value is needed after the flag. See [Web UI](#web-ui) for more information on the Web UI.
- Launch the Web UI for this job. A Boolean flag. No value is needed after the flag. See [Web UI](#web-ui) for more information on the Web UI.
- `-p`, `--port`
- Example: `5333`
- Run the Web UI on a specifc port.
- `--config`
- Example: `custom_run_config.json`
- Path to a json file containing the configuration for the above parameters. This config file takes precedence over all of the other flags.
Expand Down
40 changes: 20 additions & 20 deletions ffmpeg4discord/app.py → ffmpeg4discord/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,8 @@
from ffmpeg4discord import arguments
from ffmpeg4discord.twopass import TwoPass

# get args from the command line
args = arguments.get_args()
web = args.pop("web")
port = args.pop("port")
path = Path(args["filename"]).resolve()
args["filename"] = path

# instantiate the TwoPass class
twopass = TwoPass(**args)


def twopass_loop(target_filesize: float) -> str:
def twopass_loop(twopass: TwoPass, target_filesize: float) -> None:
while twopass.run() >= target_filesize:
print(
f"\nThe output file size ({round(twopass.output_filesize, 2)}MB) is still above the target of {target_filesize}MB.\nRestarting...\n"
Expand All @@ -32,10 +22,7 @@ def twopass_loop(target_filesize: float) -> str:
# adjust the class's target file size to set a lower bitrate for the next run
twopass.target_filesize -= 0.2

success_message = f"Your compressed video file ({round(twopass.output_filesize, 2)}MB) is located at {Path(twopass.output_filename).resolve()}"
print(success_message)

return success_message
twopass.message = f"Your compressed video file ({round(twopass.output_filesize, 2)}MB) is located at {Path(twopass.output_filename).resolve()}"


def seconds_to_timestamp(seconds: int) -> str:
Expand All @@ -48,12 +35,25 @@ def seconds_to_timestamp(seconds: int) -> str:
return timestamp


def open_browser() -> None:
def open_browser(port: int) -> None:
time.sleep(0.5)
webbrowser.open(f"http://localhost:{port}")


def main() -> None:
# get args from the command line
args = arguments.get_args()
web = args.pop("web")

if web:
port = args.pop("port")

path = Path(args["filename"]).resolve()
args["filename"] = path

# instantiate the TwoPass class
twopass = TwoPass(**args)

if web:
app = Flask(__name__, static_folder=path.parent)

Expand All @@ -79,7 +79,7 @@ def form_twopass():
twopass.crop = request.form.get("crop")
twopass.output_dir = request.form.get("output_dir")

success_message = twopass_loop(target_filesize)
twopass_loop(twopass=twopass, target_filesize=target_filesize)

for file in glob("ffmpeg2pass*"):
os.remove(file)
Expand All @@ -88,13 +88,13 @@ def form_twopass():
"web.html",
file_url=url_for("static", filename=path.name),
twopass=twopass,
alert=success_message,
)

threading.Thread(target=open_browser, name="Open Browser").start()
threading.Thread(target=open_browser, args=[port], name="Open Browser").start()
app.run("0.0.0.0", port=port)
else:
twopass_loop(twopass.target_filesize)
twopass_loop(twopass=twopass, target_filesize=twopass.target_filesize)
print(twopass.message)


if __name__ == "__main__":
Expand Down
30 changes: 28 additions & 2 deletions ffmpeg4discord/arguments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from argparse import ArgumentParser, Namespace, BooleanOptionalAction
import socket
import logging
from random import randint


def is_port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0


def get_args() -> Namespace:
Expand Down Expand Up @@ -32,6 +40,24 @@ def get_args() -> Namespace:

# web
parser.add_argument("--web", action=BooleanOptionalAction, help="Launch ffmpeg4discord's Web UI in your browser.")
parser.add_argument("-p", "--port", type=int, default=5333, help="Local port for the Flask application.")
parser.add_argument("-p", "--port", type=int, help="Local port for the Flask application.")

args = vars(parser.parse_args())

# do some work regarding the port
if args["web"]:
port = args.pop("port")
port = port if port else randint(5000, 6000)

while True:
if not is_port_in_use(port):
break
logging.warning(f"Port {port} is already in use. Retrying with a new port.")
port = randint(5000, 6000)

args["port"] = port

else:
del args["port"]

return vars(parser.parse_args())
return args
4 changes: 2 additions & 2 deletions ffmpeg4discord/templates/web.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<div class="container">
<div class="row mt-3">
<div class="alert alert-light" role="alert" {{ "hidden" if alert_hidden else "" }}>
<p>{{ alert }}</p>
<span class="text-danger">Remember to terminate this server before starting another job.</span>
<p>{{ twopass.message }}</p>
<span class="text-danger">The server is still running. Do not forget to terminate it.</span>
</div>
</div>
<div class="row mt-3">
Expand Down
3 changes: 1 addition & 2 deletions ffmpeg4discord/twopass.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def __init__(
information to further diagnose the problem:
- Your video is {self.duration / 60} minutes long
- You want to START clipping at {self.times["ss"]}
- You want to STOP clipping at {self.times["to"]}
- Your clipping times are {self.times}
"""
)

Expand Down
6 changes: 4 additions & 2 deletions ffmpeg4discord/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
python_install = Path(sys.executable)

if "WindowsApps" in str(python_install):
print("Microsoft Store Python installation detected.")
print(
"Microsoft Store Python installation detected. If you run into problems, consider using the https://python.org installation instead!"
)
target = Path(sys.executable).parent.parent
else:
print("https://python.org Python installation detected.")
print("python.org Python installation detected.")
target = Path(sys.executable).parent / "Scripts"

target.mkdir(parents=True, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "ffmpeg4discord"
version = "0.1.1"
version = "0.1.2"
description = "A tool to help convert a video file to a target file size."
readme = {file = "README.md", content-type = "text/markdown"}
authors = [{ name = "Zach Fleeman", email = "zfleeman@gmail.com" }]
Expand All @@ -22,7 +22,7 @@ Homepage = "https://github.com/zfleeman/ffmpeg4discord"
Issues = "https://github.com/zfleeman/ffmpeg4discord/issues"

[project.scripts]
ff4d = "ffmpeg4discord.app:main"
ff4d = "ffmpeg4discord.__main__:main"
install-ffmpeg-windows = "ffmpeg4discord.windows:install"

[tool.setuptools.package-data]
Expand Down

0 comments on commit 0118113

Please sign in to comment.