Mercurial > code > home > repos > video
annotate thumbnail.py @ 49:1bd17c2e5517 default tip
video.py must sign video urls for serve-files.js to serve them
author | drewp@bigasterisk.com |
---|---|
date | Fri, 06 Dec 2024 17:13:51 -0800 |
parents | 7cacfae58430 |
children |
rev | line source |
---|---|
37
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
1 import asyncio |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
2 from tempfile import NamedTemporaryFile |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
3 import pymongo.collection |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
4 import logging |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
5 log = logging.getLogger('thumb') |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
6 |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
7 async def getThumbnailData(coll: pymongo.collection.Collection, diskPath: str) -> bytes: |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
8 doc=coll.find_one({'diskPath':diskPath}) |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
9 if doc is None: |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
10 raise Exception(f"no thumb found for {diskPath}") |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
11 return doc['thumbData'] |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
12 |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
13 async def createThumbnail(coll: pymongo.collection.Collection, diskPath: str): |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
14 if coll.find_one({'diskPath':diskPath}): |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
15 return |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
16 coll.delete_one({'diskPath':diskPath}) |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
17 |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
18 # diskPath could be a YT sidecar file? |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
20 thumbFile = NamedTemporaryFile(suffix='.jpg') |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
21 log.info(f'createThumbnail: {diskPath=} to {thumbFile.name=}') |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
22 proc = await asyncio.create_subprocess_exec('ffmpegthumbnailer', |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
23 '-s', '250', '-i', |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
24 diskPath, '-o', thumbFile.name) |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
25 await proc.wait() |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
26 if proc.returncode != 0: |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
27 log.error(f'createThumbnail: {proc.returncode=}') |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
28 return |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
29 thumbFile.seek(0) |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
30 imgData = thumbFile.read() |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
31 log.info(f'createThumbnail: {len(imgData)=}') |
7cacfae58430
thumbnails rewrite - store in db; don't use YT-provided pics for now
drewp@bigasterisk.com
parents:
diff
changeset
|
32 coll.insert_one({'diskPath':diskPath,'thumbData':imgData}) |