Browse Source

hype

master
gsd 1 year ago
parent
commit
a1ecf0e6a4
  1. 14
      Dockerfile
  2. 65
      extensions/watermark.py
  3. 3
      test_watermark.bat

14
Dockerfile

@ -4,10 +4,24 @@ RUN apt update && apt install -y ffmpeg
RUN python -m pip install python-telegram-bot RUN python -m pip install python-telegram-bot
#SQUARE FIX
#RUN cd /tmp && \
#wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz && \
#tar -xvf ffmpeg-release-amd64-static.tar.xz && \
#cd ffmpeg* && \
#chmod 777 ./ffmpeg && \
#rm /usr/bin/ffmpeg && \
#mv ffmpeg /usr/bin/ && \
#cd .. && \
#rm -r *
WORKDIR /app WORKDIR /app
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
ARG BUILDDATE
ENV BUILDDATE $BUILDDATE
COPY ./ ./ COPY ./ ./
ENTRYPOINT [ "python", "bot.py" ] ENTRYPOINT [ "python", "bot.py" ]

65
extensions/watermark.py

@ -1,15 +1,40 @@
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler
import os, pathlib, asyncio import os, pathlib, asyncio
from time import time from time import time
async def add_watermark(input_file, watermark, transparency, out_ext): try:
#print(f'ffmpeg -i {input_file} -i {watermark} -filter_complex "[1]lut=a=val*0.08[a];[0][a]overlay=0:0" -c:v libx264 {input_file}.mp4') from telegram import ForceReply, Update
resize_filter = "[logo][0]scale2ref=w=oh*mdar:h=ih*1.0[logo][0];" from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler
insert_filter = "[0][logo]overlay=(W-w)/2:(H-h)/2:format=auto,format=yuv420p" except ModuleNotFoundError:
#insert_filter = "[1][logo]overlay=5:H-h-5" Update = None
filter = f"[1]format=rgba,colorchannelmixer=aa={transparency}[logo];{resize_filter}{insert_filter}" class ct:
cmd = f'ffmpeg -i {input_file} -i {watermark} -filter_complex "{filter}" {"-c:v libx264 -c:a copy" if out_ext == "mp4" else ""} {input_file}.{out_ext}' DEFAULT_TYPE = None
ContextTypes = ct()
print("Cannot import telegram futures")
filter_tables = {
"v1":{#default
"resize_filter":"[logo][0]scale2ref=w=oh*mdar:h=ih*1.0[logo][0];",
"insert_filter":"[0][logo]overlay=(W-w)/2:(H-h)/2:format=auto,format=yuv420p",
"filter":"[1]format=rgba,colorchannelmixer=aa={transparency}[logo];{resize_filter}{insert_filter}"
},
"v2":{#photo
"filter":"[1]format=rgba,colorchannelmixer=aa={transparency}[logo];[logo][0]scale2ref=oh*mdar:ih*0.2[logo][video];[video][logo]overlay=(main_w-overlay_w):(main_h-overlay_h)"
},
"v2.move":{#good
"insert_filter":"[video][logo]overlay=x='if(lt(mod(t\,16)\,8)\,W-w-W*10/100\,W*10/100)':y='if(lt(mod(t+4\,16)\,8)\,H-h-H*5/100\,H*5/100)'",
"filter":"[1]colorchannelmixer=aa={transparency}[logo];[logo][0]scale2ref=oh*mdar:ih*0.2[logo][video];{insert_filter}"
},
"v2.dvd":{
"insert_filter":"[video][logo]overlay=x='if(eq(mod(floor(st(8,t*{speed_x})/(W-w)),2),0),mod(ld(8),W-w),W-w-mod(ld(8),W-w))':y='if(eq(mod(floor(st(9,t*{speed_y})/(H-h)),2),0),mod(ld(9),H-h),H-h-mod(ld(9),H-h))'",
"filter":"[1]colorchannelmixer=aa={transparency}[logo];[logo][0]scale2ref=oh*mdar:ih*0.2[logo][video];{insert_filter}"
}
}
async def add_watermark(input_file, watermark, transparency, out_ext, filter_version = "v1", speed=(160, 70)):
resize_filter = filter_tables[filter_version].get("resize_filter", "")
insert_filter = filter_tables[filter_version].get("insert_filter", "").format(speed_x = speed[0], speed_y = speed[1])
filter = filter_tables[filter_version]["filter"].format(transparency = transparency, resize_filter = resize_filter, insert_filter = insert_filter)
cmd = f'ffmpeg -y -i {input_file} -i {watermark} -filter_complex "{filter}" {"-c:v libx264 -c:a copy" if out_ext == "mp4" else ""} {input_file}.{out_ext}'
print(cmd) print(cmd)
process = await asyncio.create_subprocess_shell(cmd) process = await asyncio.create_subprocess_shell(cmd)
await process.wait() await process.wait()
@ -68,7 +93,7 @@ class Extension:
_id = update.message.photo[-1].file_id _id = update.message.photo[-1].file_id
photo = await context.bot.get_file(_id) photo = await context.bot.get_file(_id)
downloaded_photo = await photo.download_to_drive(f"{self.content_directory}/{time()}") downloaded_photo = await photo.download_to_drive(f"{self.content_directory}/{time()}")
res = await add_watermark(downloaded_photo.absolute(), self.png_path, transparency, "jpg") res = await add_watermark(downloaded_photo.absolute(), self.png_path, transparency, "jpg", "v2")
if res: if res:
await update.message.reply_photo(photo=res) await update.message.reply_photo(photo=res)
os.remove(res) os.remove(res)
@ -87,7 +112,7 @@ class Extension:
video = await update.message.video.get_file() video = await update.message.video.get_file()
downloaded_video = await video.download_to_drive(f"{self.content_directory}/{time()}") downloaded_video = await video.download_to_drive(f"{self.content_directory}/{time()}")
res = await add_watermark(downloaded_video.absolute(), self.png_path, transparency, "mp4") res = await add_watermark(downloaded_video.absolute(), self.png_path, transparency, "mp4", "v2.dvd")
if res: if res:
await update.message.reply_video(video=res) await update.message.reply_video(video=res)
os.remove(res) os.remove(res)
@ -95,4 +120,20 @@ class Extension:
await update.message.reply_text("Ошибка при создании видео") await update.message.reply_text("Ошибка при создании видео")
if os.path.exists(downloaded_video.absolute()): if os.path.exists(downloaded_video.absolute()):
os.remove(downloaded_video.absolute()) os.remove(downloaded_video.absolute())
return return
if __name__ == "__main__":
import asyncio
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("input", type=str)
parser.add_argument("watermark", type=str)
parser.add_argument("--transparency", type=float, default=100.0)
parser.add_argument("--insert-version", type=str, default="v1")
parser.add_argument("--out-ext", type=str, default="mp4")
args = parser.parse_args()
async def run():
await add_watermark(args.input, args.watermark, args.transparency / 100, args.out_ext, args.insert_version)
asyncio.get_event_loop().run_until_complete(run())

3
test_watermark.bat

@ -0,0 +1,3 @@
python "D:\GOVNOKOD\zem.TelegramBot\extensions\watermark.py" "C:\Users\gsd\Downloads\IMG_8442.MP4" "D:\GOVNOKOD\zem.TelegramBot\watermark.png" --insert-version v2.dvd --out-ext mp4
python "D:\GOVNOKOD\zem.TelegramBot\extensions\watermark.py" "C:\Users\gsd\Downloads\2023-10-03_10-35-25.jpg" "D:\GOVNOKOD\zem.TelegramBot\watermark.png" --insert-version v2 --out-ext jpg
pause
Loading…
Cancel
Save