fix: fix webm duration via ffmpeg
This commit is contained in:
parent
a6b8c09379
commit
e38090e952
|
@ -40,7 +40,7 @@ def guess_mime(data: bytes) -> str:
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
with tempfile.NamedTemporaryFile(delete=False) as temp:
|
with tempfile.NamedTemporaryFile() as temp:
|
||||||
temp.write(data)
|
temp.write(data)
|
||||||
temp.close()
|
temp.close()
|
||||||
mime, _ = mimetypes.guess_type(temp.name)
|
mime, _ = mimetypes.guess_type(temp.name)
|
||||||
|
@ -48,12 +48,26 @@ def guess_mime(data: bytes) -> str:
|
||||||
|
|
||||||
|
|
||||||
def video_to_gif(data: bytes, mime: str) -> bytes:
|
def video_to_gif(data: bytes, mime: str) -> bytes:
|
||||||
from moviepy.editor import VideoFileClip
|
|
||||||
ext = mimetypes.guess_extension(mime)
|
ext = mimetypes.guess_extension(mime)
|
||||||
|
if mime.startswith("video/"):
|
||||||
|
# run ffmpeg to fix duration
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=ext) as temp:
|
||||||
|
temp.write(data)
|
||||||
|
temp.flush()
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=ext) as temp_fixed:
|
||||||
|
import subprocess
|
||||||
|
print(".", end="", flush=True)
|
||||||
|
result = subprocess.run(["ffmpeg", "-y", "-i", temp.name, "-codec", "copy", temp_fixed.name],
|
||||||
|
capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise RuntimeError(f"Run ffmpeg failed with code {result.returncode}, Error occurred:\n{result.stderr}")
|
||||||
|
temp_fixed.seek(0)
|
||||||
|
data = temp_fixed.read()
|
||||||
with tempfile.NamedTemporaryFile(suffix=ext) as temp:
|
with tempfile.NamedTemporaryFile(suffix=ext) as temp:
|
||||||
temp.write(data)
|
temp.write(data)
|
||||||
temp.flush()
|
temp.flush()
|
||||||
with tempfile.NamedTemporaryFile(suffix=".gif") as gif:
|
with tempfile.NamedTemporaryFile(suffix=".gif") as gif:
|
||||||
|
from moviepy.editor import VideoFileClip
|
||||||
clip = VideoFileClip(temp.name)
|
clip = VideoFileClip(temp.name)
|
||||||
clip.write_gif(gif.name, logger=None)
|
clip.write_gif(gif.name, logger=None)
|
||||||
gif.seek(0)
|
gif.seek(0)
|
||||||
|
|
Loading…
Reference in New Issue