async def findlargemp4fileffmpeg(starttime, endtime): #print("begin findlargemp4fileffmpeg") mp4list = [] client = AsyncIOMotorClient(ServerParameters.mongodbpath) db = client.jt808 bucket = AsyncIOMotorGridFSBucket(db, "eventuploadvideos") cursor = bucket.find({ "uploadDate": { '$gt': starttime, '$lte': endtime }, "filename": { "$regex": ".mp4$" } }) filelist = await cursor.to_list(100000) ccount = 0 for fi in filelist: if fi["length"] > 1000000: print(fi) if os.path.exists(fi["filename"]): os.remove(fi["filename"]) ds = await bucket.open_download_stream(fi["_id"]) f = open("input" + fi["filename"], 'wb') bbb = await ds.read() f.write(bbb) f.close() ds.close() converttstoh264("input" + fi["filename"], fi["filename"]) if os.path.exists("input" + fi["filename"]): os.remove("input" + fi["filename"]) # 保存到bucket try: if os.path.exists(fi["filename"]): uf = open(fi["filename"], "rb") ubbb = uf.read() uf.close() os.remove(fi["filename"]) bucket.delete(fi["_id"]) uds = bucket.open_upload_stream_with_id( fi["_id"], fi["filename"]) await uds.write(ubbb) uds.close() ccount = ccount + 1 logging.info("convert %s %s", fi["_id"], fi["filename"]) except BaseException as e: logging.error(e) logging.info("end findlargemp4fileffmpeg total %s convert %s", len(filelist), ccount) return
class TestAsyncIOGridFSBucket(AsyncIOTestCase): @asyncio.coroutine def _reset(self): yield from self.db.drop_collection("fs.files") yield from self.db.drop_collection("fs.chunks") yield from self.db.drop_collection("alt.files") yield from self.db.drop_collection("alt.chunks") def setUp(self): super(TestAsyncIOGridFSBucket, self).setUp() self.loop.run_until_complete(self._reset()) self.bucket = AsyncIOMotorGridFSBucket(self.db) def tearDown(self): self.loop.run_until_complete(self._reset()) super(TestAsyncIOGridFSBucket, self).tearDown() @asyncio_test def test_basic(self): oid = yield from self.bucket.upload_from_stream("test_filename", b"hello world") gout = yield from self.bucket.open_download_stream(oid) self.assertEqual(b"hello world", (yield from gout.read())) self.assertEqual(1, (yield from self.db.fs.files.count())) self.assertEqual(1, (yield from self.db.fs.chunks.count())) dst = BytesIO() yield from self.bucket.download_to_stream(gout._id, dst) self.assertEqual(b"hello world", dst.getvalue()) yield from self.bucket.delete(oid) with self.assertRaises(NoFile): yield from self.bucket.open_download_stream(oid) self.assertEqual(0, (yield from self.db.fs.files.count())) self.assertEqual(0, (yield from self.db.fs.chunks.count()))
class TestAsyncIOGridFSBucket(AsyncIOTestCase): @asyncio.coroutine def _reset(self): yield from self.db.drop_collection("fs.files") yield from self.db.drop_collection("fs.chunks") yield from self.db.drop_collection("alt.files") yield from self.db.drop_collection("alt.chunks") def setUp(self): super(TestAsyncIOGridFSBucket, self).setUp() self.loop.run_until_complete(self._reset()) self.bucket = AsyncIOMotorGridFSBucket(self.db) def tearDown(self): self.loop.run_until_complete(self._reset()) super(TestAsyncIOGridFSBucket, self).tearDown() @asyncio_test def test_basic(self): oid = yield from self.bucket.upload_from_stream("test_filename", b"hello world") gout = yield from self.bucket.open_download_stream(oid) self.assertEqual(b"hello world", (yield from gout.read())) self.assertEqual(1, (yield from self.db.fs.files.count_documents({}))) self.assertEqual(1, (yield from self.db.fs.chunks.count_documents({}))) dst = BytesIO() yield from self.bucket.download_to_stream(gout._id, dst) self.assertEqual(b"hello world", dst.getvalue()) yield from self.bucket.delete(oid) with self.assertRaises(NoFile): yield from self.bucket.open_download_stream(oid) self.assertEqual(0, (yield from self.db.fs.files.count_documents({}))) self.assertEqual(0, (yield from self.db.fs.chunks.count_documents({})))
class TestAsyncIOGridFSBucket(AsyncIOTestCase): @asyncio.coroutine def _reset(self): yield from self.db.drop_collection("fs.files") yield from self.db.drop_collection("fs.chunks") yield from self.db.drop_collection("alt.files") yield from self.db.drop_collection("alt.chunks") def setUp(self): super(TestAsyncIOGridFSBucket, self).setUp() self.loop.run_until_complete(self._reset()) self.bucket = AsyncIOMotorGridFSBucket(self.db) def tearDown(self): self.loop.run_until_complete(self._reset()) super(TestAsyncIOGridFSBucket, self).tearDown() @asyncio_test def test_basic(self): oid = yield from self.bucket.upload_from_stream( "test_filename", b"hello world") gout = yield from self.bucket.open_download_stream(oid) self.assertEqual(b"hello world", (yield from gout.read())) self.assertEqual(1, (yield from self.db.fs.files.count_documents({}))) self.assertEqual(1, (yield from self.db.fs.chunks.count_documents({}))) dst = BytesIO() yield from self.bucket.download_to_stream(gout._id, dst) self.assertEqual(b"hello world", dst.getvalue()) yield from self.bucket.delete(oid) with self.assertRaises(NoFile): yield from self.bucket.open_download_stream(oid) self.assertEqual(0, (yield from self.db.fs.files.count_documents({}))) self.assertEqual(0, (yield from self.db.fs.chunks.count_documents({}))) def test_init(self): name = 'bucket' wc = WriteConcern(w='majority', wtimeout=1000) rp = ReadPreference.SECONDARY size = 8 bucket = AsyncIOMotorGridFSBucket(self.db, name, disable_md5=True, chunk_size_bytes=size, write_concern=wc, read_preference=rp) self.assertEqual(name, bucket.collection.name) self.assertEqual(wc, bucket.collection.write_concern) self.assertEqual(rp, bucket.collection.read_preference) self.assertEqual(wc, bucket.delegate._chunks.write_concern) self.assertEqual(rp, bucket.delegate._chunks.read_preference) self.assertEqual(size, bucket.delegate._chunk_size_bytes) @ignore_deprecations def test_collection_param(self): bucket = AsyncIOMotorGridFSBucket(self.db, collection='collection') self.assertEqual('collection', bucket.collection.name)