telegram gif fix 2
This commit is contained in:
1 parent
4ca7e13246
commit
4358df8da9
1 file changed
+22
-7
+22
-7
@@ -133,17 +133,32 @@ class TelegramForwarder:
|
|||||||
# convert mp4 animation to true gif so discord auto-loops it
|
# convert mp4 animation to true gif so discord auto-loops it
|
||||||
try:
|
try:
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_mp4:
|
||||||
|
temp_mp4.write(file_data)
|
||||||
|
temp_mp4_path = temp_mp4.name
|
||||||
|
|
||||||
|
temp_gif_path = temp_mp4_path + '.gif'
|
||||||
|
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
'ffmpeg', '-i', 'pipe:0', '-f', 'gif', '-loop', '0', 'pipe:1',
|
'ffmpeg', '-y', '-i', temp_mp4_path, '-f', 'gif', '-loop', '0', temp_gif_path,
|
||||||
stdin=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.DEVNULL,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
|
||||||
stderr=asyncio.subprocess.DEVNULL
|
stderr=asyncio.subprocess.DEVNULL
|
||||||
)
|
)
|
||||||
stdout_data, _ = await process.communicate(input=file_data)
|
await process.communicate()
|
||||||
if process.returncode == 0 and stdout_data:
|
|
||||||
file_data = stdout_data
|
if process.returncode == 0 and os.path.exists(temp_gif_path):
|
||||||
# replace extension with .gif
|
with open(temp_gif_path, 'rb') as f:
|
||||||
|
file_data = f.read()
|
||||||
filename = filename.rsplit('.', 1)[0] + '.gif'
|
filename = filename.rsplit('.', 1)[0] + '.gif'
|
||||||
|
|
||||||
|
try:
|
||||||
|
if os.path.exists(temp_mp4_path): os.remove(temp_mp4_path)
|
||||||
|
if os.path.exists(temp_gif_path): os.remove(temp_gif_path)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'ffmpeg gif conversion failed (is ffmpeg installed?): {e}')
|
print(f'ffmpeg gif conversion failed (is ffmpeg installed?): {e}')
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user