@@ -32,31 +32,35 @@ def get_sample_url(self, realm):
3232 sample_path = f'{ self .id } /{ self .get_sample_path (sample_start )} '
3333 if not realm .sample_storage .exists (sample_path ):
3434 suffix = Path (sample_path ).suffix
35- with (realm .original_storage .open (self .file_path ) as handle , NamedTemporaryFile (suffix = suffix ) as tempfile ):
36- in_stream = ffmpeg .input ('pipe:0' )
37- video_stream = (
38- in_stream .video
39- .filter ('crop' , 'min(iw,ih)' , 'min(iw,ih)' )
40- .filter ('scale' , self .thumbnail_size , - 1 )
41- )
42- process = (
43- ffmpeg .concat (video_stream , in_stream .audio , v = 1 , a = 1 )
44- .output (
45- tempfile .name ,
46- ss = sample_start ,
47- t = sample_duration ,
35+ try :
36+ with NamedTemporaryFile (suffix = suffix ) as tempfile :
37+ in_stream = ffmpeg .input ('pipe:0' )
38+ video_stream = (
39+ in_stream .video
40+ .filter ('crop' , 'min(iw,ih)' , 'min(iw,ih)' )
41+ .filter ('scale' , self .thumbnail_size , - 1 )
4842 )
49- .run_async (pipe_stdin = True , overwrite_output = True , quiet = True )
50- )
51- for chunk in handle .chunks ():
52- try :
53- process .stdin .write (chunk )
54- except BrokenPipeError :
55- break # sample frame found
56- process .stdin .close ()
57- process .wait ()
58- tempfile .flush ()
59- realm .sample_storage .save (sample_path , tempfile )
43+ process = (
44+ ffmpeg .concat (video_stream , in_stream .audio , v = 1 , a = 1 )
45+ .output (
46+ tempfile .name ,
47+ ss = sample_start ,
48+ t = sample_duration ,
49+ )
50+ .run_async (pipe_stdin = True , overwrite_output = True , quiet = True )
51+ )
52+ with realm .original_storage .open (self .file_path ) as handle :
53+ for chunk in handle .chunks ():
54+ try :
55+ process .stdin .write (chunk )
56+ except BrokenPipeError :
57+ break # sample frame found
58+ process .stdin .close ()
59+ process .wait ()
60+ tempfile .flush ()
61+ realm .sample_storage .save (sample_path , tempfile )
62+ except Exception :
63+ return
6064 return realm .sample_storage .url (sample_path )
6165
6266 def get_thumbnail_url (self , realm ):
@@ -66,23 +70,27 @@ def get_thumbnail_url(self, realm):
6670 suffix = '.jpg'
6771 poster_path = f'{ self .id } /{ self .get_sample_path (sample_start , suffix = suffix )} '
6872 if not realm .sample_storage .exists (poster_path ):
69- with realm .original_storage .open (self .file_path ) as handle , NamedTemporaryFile (suffix = suffix ) as tempfile :
70- process = (
71- ffmpeg .input ('pipe:0' ).video
72- .filter ('crop' , 'min(iw,ih)' , 'min(iw,ih)' )
73- .filter ('scale' , self .thumbnail_size , - 1 )
74- .output (tempfile .name , ss = sample_start , vframes = 1 )
75- .run_async (pipe_stdin = True , overwrite_output = True , quiet = False )
76- )
77- for chunk in handle .chunks ():
78- try :
79- process .stdin .write (chunk )
80- except BrokenPipeError :
81- break # sample frame found
82- process .stdin .close ()
83- process .wait ()
84- tempfile .flush ()
85- realm .sample_storage .save (poster_path , tempfile )
73+ try :
74+ with NamedTemporaryFile (suffix = suffix ) as tempfile :
75+ process = (
76+ ffmpeg .input ('pipe:0' ).video
77+ .filter ('crop' , 'min(iw,ih)' , 'min(iw,ih)' )
78+ .filter ('scale' , self .thumbnail_size , - 1 )
79+ .output (tempfile .name , ss = sample_start , vframes = 1 )
80+ .run_async (pipe_stdin = True , overwrite_output = True , quiet = True )
81+ )
82+ with realm .original_storage .open (self .file_path ) as handle :
83+ for chunk in handle .chunks ():
84+ try :
85+ process .stdin .write (chunk )
86+ except BrokenPipeError :
87+ break # sample frame found
88+ process .stdin .close ()
89+ process .wait ()
90+ tempfile .flush ()
91+ realm .sample_storage .save (poster_path , tempfile )
92+ except Exception :
93+ return self .fallback_thumbnail_url
8694 return realm .sample_storage .url (poster_path )
8795
8896 def get_sample_path (self , sample_start , suffix = None ):
0 commit comments