Created
March 31, 2024 21:40
-
-
Save ebb-earl-co/6577ae6360893aaf654e0c7d5389322e to your computer and use it in GitHub Desktop.
Download and Verify Checksum of Windows FFmpeg Build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import http | |
from io import BytesIO | |
from pathlib import Path | |
import shutil | |
import sys | |
import urllib.request | |
from zipfile import ZipFile | |
FFMPEG_BUILD_URL: str = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip" | |
# FFMPEG_BUILD_SHA256_URL: str = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip.sha256" | |
FFMPEG_BUILD_SHA256_SUM: str = "742e32fc9f92681f9f254b925e1b613fdd8074ba40749d4879aefdb009b94cc5" | |
def main(argv=None): | |
argv = sys.argv if argv is None else argv | |
h = hashlib.sha256() | |
print(f"Fetching ZIP archive of FFmpeg 6.1.1 from '{FFMPEG_BUILD_URL}'", file=sys.stderr) | |
with urllib.request.urlopen(FFMPEG_BUILD_URL) as response: | |
if not response.status == 200: | |
print(f"Unable to retrieve data from '{FFMPEG_BUILD_URL}'", file=sys.stderr) | |
return 1 | |
response_bytes = BytesIO(response.read()) | |
for line_of_bytes in iter(lambda: response_bytes.readline(), b""): | |
h.update(line_of_bytes) | |
else: | |
sha256_hash = h.hexdigest() | |
if sha256_hash != FFMPEG_BUILD_SHA256_SUM: | |
print( | |
f"WARNING: the downloaded file's checksum, '{sha256_hash}' " | |
f"does not match the expected checksum, '{FFMPEG_BUILD_SHA256_SUM}'. " | |
"The downloaded file will not be saved.", | |
file=sys.stderr | |
) | |
return 1 | |
response_bytes.seek(0) | |
with ZipFile(response_bytes) as z: | |
if z.testzip() is not None: | |
print( | |
f"WARNING: the downloaded file is not a valid .zip file. " | |
"The downloaded file will not be saved.", | |
file=sys.stderr | |
) | |
return 1 | |
with z.open("ffmpeg-6.1.1-essentials_build/bin/ffmpeg.exe") as ffmpeg_exe: | |
Path("ffmpeg.exe").write_bytes(ffmpeg_exe.read()) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment