python - 使用`django-storages`上传时如何获取mp4的缩略图?
问题描述
我的工作是local.py
我可以获得instance.video.path
并且能够获得thumbnail
. 这是我的函数、测试用例和模型
utils.py
def save_screen_shot(instance: Video) -> Video:
try:
filename = instance.video.path
except ValueError as err:
logger.info(f"The 'video' attribute has no file associated with it.")
else:
video_length = clean_duration(get_length(filename))
instance.video_length = video_length
img_output_path = f"/tmp/{str(uuid.uuid4())}.jpg"
subprocess.call(['ffmpeg', '-i', filename, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])
# save screen_shot
with open(img_output_path, 'rb') as ss_file:
instance.screen_shot.save(str(uuid.uuid4()) + '.jpg', ss_file)
instance.save()
finally:
instance.refresh_from_db()
return instance
tests.py
def test_screen_shot(self):
client = APIClient()
client.force_authenticate(user=self.user_a)
with open('media/SampleVideo_1280x720_1mb.mp4', 'rb') as mp4_file:
data = {
'text': "Big Bug Bunny",
'multipart_tags': 'Tri Uncle featuring',
'video': mp4_file,
}
url = reverse('api:tweet-list')
res = client.post(url, data=data, format='multipart')
video = Video.objects.first()
mp4_file.seek(0) # set head to first position before do an `assertion`
assert status.HTTP_201_CREATED == res.status_code
assert mp4_file.read() == video.video.read()
assert video.screen_shot # Not None
assert 1 == Video.objects.count()
models.py
class Video(models.Model):
video = models.FileField(upload_to='./videos/', null=True, blank=True)
我把调试,发现在我的生产INSTALLED_APPS
中有'storages'
它。它让我犯了错误
ipdb> instance.video.path
*** NotImplementedError: This backend doesn't support absolute paths.
ipdb> dir(instance.video)
['DEFAULT_CHUNK_SIZE', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_committed', '_del_file', '_file', '_get_file', '_require_file', '_set_file', 'chunks', 'close', 'closed', 'delete', 'encoding', 'field', 'file', 'fileno', 'flush', 'instance', 'isatty', 'multiple_chunks', 'name', 'newlines', 'open', 'path', 'read', 'readable', 'readinto', 'readline', 'readlines', 'save', 'seek', 'seekable', 'size', 'storage', 'tell', 'truncate', 'url', 'writable', 'write', 'writelines']
尝试
因为我的最终目标是
video_length
screenshot
我尝试直接访问file
,但它提高了我TypeError
ipdb> instance.video.file
<S3Boto3StorageFile: videos/Marschiert_in_Feindesland_.flv_360p_j5GJ19m.mp4>
ipdb> video_length = clean_duration(get_length(instance.video.file))
*** TypeError: expected str, bytes or os.PathLike object, not S3Boto3StorageFile
问题:
我在使用时如何获取屏幕截图django-storage
?
解决方案
推荐阅读
- swift - 将 iPhone XS SceneKit PixelFormat 设置为 MTKView 格式 MTLPixelFormatRGB10A8_2P_XR10_sRGB
- javascript - V-validate not working with interpolated attribute
- python - ImportError:无法导入名称图。在 UBUNTU 和 WINDOWS 中
- java - labelFor 属性不适用于 Text 标签
- python - Connecting django-mssql to mssql server (azure)
- sql - Bigquery: Append to a nested record
- r - 创建以参数为列名的函数
- python - matplotlib stack bar grouped by date (month and year)
- javascript - google auth 成功后将用户重定向到 Angular 路由
- firebase - Golang Firebase 管理员 SDK