我在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
问题:
当我使用5 ≤ T ≤ 1000
and
(100 ≤ T ≤ 200) or (1100 ≤ T ≤ 1300)
时如何获取屏幕截图?