-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pathlib import Path | ||
import ffmpeg | ||
|
||
|
||
def sticker_from_video(mp4_path: Path): | ||
''' | ||
Converts the given video to a proper format that can be uploaded as a sticker. | ||
Telegram accepts WEBM VP9 with a maximum size of 512x512 pixels with maximum 3 seconds duration. | ||
''' | ||
probe = ffmpeg.probe(mp4_path) | ||
video_stream = next( | ||
(stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None | ||
) | ||
width = int(video_stream['width']) | ||
height = int(video_stream['height']) | ||
duration = float(probe['format']['duration']) | ||
|
||
if duration > 3.0: | ||
raise ValueError('Video duration exceeds limits') | ||
|
||
(width, height) = estimate_video_sticker_size(width, height) | ||
vid_output_path = mp4_path.with_suffix('.webm') | ||
( | ||
ffmpeg.input(mp4_path) | ||
.video.filter('scale', width, height) | ||
.output(filename=vid_output_path.as_posix(), format='webm', vcodec='libvpx-vp9') | ||
.run(overwrite_output=True) | ||
) | ||
return vid_output_path | ||
|
||
|
||
def estimate_video_sticker_size(width: int, height: int) -> tuple[int, int]: | ||
''' | ||
Estimates the frame size to fit Telegram restrictions (maximum size of 512x512 pixels), keeping its aspect ratio. | ||
''' | ||
if width >= height: | ||
ratio = 512 / width | ||
return (512, int(ratio * height)) | ||
else: | ||
ratio = 512 / height | ||
return (int(ratio * width), 512) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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