Skip to content

Commit

Permalink
loop fixes for mbtree changes in ffmpeg 7 (#38)
Browse files Browse the repository at this point in the history
* loop fixes for mbtree changes in ffmpeg 7

* readme warning
  • Loading branch information
zfleeman authored Dec 18, 2024
1 parent 4b6bdd4 commit 640c11e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pip install ffmpeg4discord

You must first have FFmpeg installed on your system. `ffmpeg` needs to be registered in your PATH. macOS or Linux users can use their favorite package manager to do this, but this process is a little more tricky for Windows users.

> **NOTE:** This package relies on the external FFmpeg binary, and updates to FFmpeg may cause unexpected errors; please raise an issue if you encounter any problems.
### Help for Windows users

After installing this package, Windows users can run this command in a terminal to download the necessary `ffmpeg` binaries:
Expand Down
26 changes: 20 additions & 6 deletions ffmpeg4discord/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import os
from glob import glob
import webbrowser
from flask import Flask, render_template, url_for, request
Expand All @@ -13,19 +12,29 @@


def twopass_loop(twopass: TwoPass, target_filesize: float, approx: bool = False) -> None:
while twopass.run() >= target_filesize and not approx:
while True:
# clean up before each run
cleanup_files("ffmpeg2pass*")

# run the two-pass encoding
current_filesize = twopass.run()

# check if the filesize is within the target
if current_filesize < target_filesize or approx:
break

print(
f"\nThe output file size ({round(twopass.output_filesize, 2)}MB) is still above the target of {target_filesize}MB.\nRestarting...\n"
)
os.remove(twopass.output_filename)
Path(twopass.output_filename).unlink()

# adjust the class's target file size to set a lower bitrate for the next run
twopass.target_filesize -= 0.2

# clean up
for file in glob("ffmpeg2pass*"):
os.remove(file)
# final cleanup
cleanup_files("ffmpeg2pass*")

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


Expand All @@ -34,6 +43,11 @@ def open_browser(port: int) -> None:
webbrowser.open(f"http://localhost:{port}")


def cleanup_files(pattern: str) -> None:
for file in glob(pattern):
Path(file).unlink()


def main() -> None:
# get args from the command line
args = arguments.get_args()
Expand Down
2 changes: 1 addition & 1 deletion 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.7"
version = "0.1.8"
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 Down

0 comments on commit 640c11e

Please sign in to comment.