| 1 |
"""Simplest example of files_dropdown_menu + notification.""" |
| 2 |
|
| 3 |
import tempfile |
| 4 |
from contextlib import asynccontextmanager |
| 5 |
from os import path |
| 6 |
from typing import Annotated |
| 7 |
|
| 8 |
import cv2 |
| 9 |
import imageio |
| 10 |
import numpy |
| 11 |
from fastapi import BackgroundTasks, Depends, FastAPI, responses |
| 12 |
from pygifsicle import optimize |
| 13 |
|
| 14 |
from nc_py_api import FsNode, NextcloudApp |
| 15 |
from nc_py_api.ex_app import AppAPIAuthMiddleware, LogLvl, nc_app, run_app, set_handlers |
| 16 |
from nc_py_api.files import ActionFileInfoEx |
| 17 |
|
| 18 |
|
| 19 |
@asynccontextmanager |
| 20 |
async def lifespan(app: FastAPI): |
| 21 |
set_handlers(app, enabled_handler) |
| 22 |
yield |
| 23 |
|
| 24 |
|
| 25 |
APP = FastAPI(lifespan=lifespan) |
| 26 |
APP.add_middleware(AppAPIAuthMiddleware) |
| 27 |
|
| 28 |
|
| 29 |
def convert_video_to_gif(input_file: FsNode, nc: NextcloudApp): |
| 30 |
save_path = path.splitext(input_file.user_path)[0] + ".gif" |
| 31 |
nc.log(LogLvl.WARNING, f"Processing:{input_file.user_path} -> {save_path}") |
| 32 |
try: |
| 33 |
with tempfile.NamedTemporaryFile(mode="w+b") as tmp_in: |
| 34 |
nc.files.download2stream(input_file, tmp_in) |
| 35 |
nc.log(LogLvl.WARNING, "File downloaded") |
| 36 |
tmp_in.flush() |
| 37 |
cap = cv2.VideoCapture(tmp_in.name) |
| 38 |
with tempfile.NamedTemporaryFile(mode="w+b", suffix=".gif") as tmp_out: |
| 39 |
image_lst = [] |
| 40 |
previous_frame = None |
| 41 |
skip = 0 |
| 42 |
while True: |
| 43 |
skip += 1 |
| 44 |
ret, frame = cap.read() |
| 45 |
if frame is None: |
| 46 |
break |
| 47 |
if skip == 2: |
| 48 |
skip = 0 |
| 49 |
continue |
| 50 |
if previous_frame is not None: |
| 51 |
diff = numpy.mean(previous_frame != frame) |
| 52 |
if diff < 0.91: |
| 53 |
continue |
| 54 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 55 |
image_lst.append(frame_rgb) |
| 56 |
previous_frame = frame |
| 57 |
if len(image_lst) > 60: |
| 58 |
break |
| 59 |
cap.release() |
| 60 |
imageio.mimsave(tmp_out.name, image_lst) |
| 61 |
optimize(tmp_out.name) |
| 62 |
nc.log(LogLvl.WARNING, "GIF is ready") |
| 63 |
nc.files.upload_stream(save_path, tmp_out) |
| 64 |
nc.log(LogLvl.WARNING, "Result uploaded") |
| 65 |
nc.notifications.create(f"{input_file.name} finished!", f"{save_path} is waiting for you!") |
| 66 |
except Exception as e: |
| 67 |
nc.log(LogLvl.ERROR, str(e)) |
| 68 |
nc.notifications.create("Error occurred", "Error information was written to log file") |
| 69 |
|
| 70 |
|
| 71 |
@APP.post("/video_to_gif") |
| 72 |
async def video_to_gif( |
| 73 |
files: ActionFileInfoEx, |
| 74 |
nc: Annotated[NextcloudApp, Depends(nc_app)], |
| 75 |
background_tasks: BackgroundTasks, |
| 76 |
): |
| 77 |
for one_file in files.files: |
| 78 |
background_tasks.add_task(convert_video_to_gif, one_file.to_fs_node(), nc) |
| 79 |
return responses.Response() |
| 80 |
|
| 81 |
|
| 82 |
def enabled_handler(enabled: bool, nc: NextcloudApp) -> str: |
| 83 |
print(f"enabled={enabled}") |
| 84 |
try: |
| 85 |
if enabled: |
| 86 |
nc.ui.files_dropdown_menu.register_ex( |
| 87 |
"to_gif", |
| 88 |
"To GIF", |
| 89 |
"/video_to_gif", |
| 90 |
mime="video", |
| 91 |
icon="img/icon.svg", |
| 92 |
) |
| 93 |
except Exception as e: |
| 94 |
return str(e) |
| 95 |
return "" |
| 96 |
|
| 97 |
|
| 98 |
if __name__ == "__main__": |
| 99 |
run_app( |
| 100 |
"main:APP", |
| 101 |
log_level="trace", |
| 102 |
) |