@@ -120,10 +120,10 @@ def process_video_hash(algo: str, hash_size: int, file_info: dict, data_dir: str
120120 full_path = get_file_full_path (data_dir , file_info ['storage' ], file_info ['path' ])
121121 if not full_path :
122122 break
123- video_info = ffprobe_get_video_info (full_path )
123+ video_info = ffprobe_get_video_info (full_path , None )
124124 if not video_info :
125125 break
126- if not do_hash_video (algo , hash_size , video_info , file_info , full_path ):
126+ if not do_hash_video (algo , hash_size , video_info , file_info , full_path , None ):
127127 raise InvalidVideo
128128 return
129129 if file_info ['size' ] > remote_filesize_limit :
@@ -133,10 +133,10 @@ def process_video_hash(algo: str, hash_size: int, file_info: dict, data_dir: str
133133 data = request_file_from_php (file_info )
134134 if len (data ) == 0 :
135135 return
136- video_info = ffprobe_get_video_info (data )
136+ video_info = ffprobe_get_video_info (None , data )
137137 if not video_info .get ('fast_start' , False ):
138138 raise InvalidVideo
139- if not do_hash_video (algo , hash_size , video_info , file_info , data ):
139+ if not do_hash_video (algo , hash_size , video_info , file_info , None , data ):
140140 raise InvalidVideo
141141 except Exception as exception_info :
142142 store_err_video_hash (file_info ['fileid' ], video_info .get ('duration' , 0 ),
@@ -146,15 +146,16 @@ def process_video_hash(algo: str, hash_size: int, file_info: dict, data_dir: str
146146 print (f"Exception({ exception_name } ): `{ file_info ['path' ]} `:\n `{ str (traceback .format_exc ())} `" )
147147
148148
149- def do_hash_video (algo : str , hash_size : int , video_info : dict , file_info : dict , path_or_data ) -> bool :
149+ def do_hash_video (algo : str , hash_size : int , video_info : dict , file_info : dict , path , data ) -> bool :
150+ """Accepts path(bytes/str) or data for processing in memory."""
150151 if video_info ['duration' ] < MinVideoDuration_ms :
151152 return False
152- first_timestamp = get_first_timestamp (video_info , path_or_data )
153+ first_timestamp = get_first_timestamp (video_info , path , data )
153154 if first_timestamp == - 1 :
154155 return False
155156 frames_timestamps = build_times_for_hashes (video_info ['duration' ], first_timestamp )
156157 ex = ()
157- res = get_frames (frames_timestamps , path_or_data , * ex )
158+ res = get_frames (frames_timestamps , path , data , * ex )
158159 if not res [0 ]:
159160 return False
160161 if any (len (x ) == 0 for x in res [1 :]):
@@ -192,19 +193,22 @@ def get_max_first_frame_time(duration_ms) -> int:
192193 return max_timestamp
193194
194195
195- def get_first_timestamp (video_info : dict , data_or_filepath ) -> int :
196+ def get_first_timestamp (video_info : dict , path , data ) -> int :
197+ """Accepts path(bytes/str) or data for processing in memory."""
196198 max_timestamp = get_max_first_frame_time (video_info ['duration' ])
197199 ffmpeg_input_params = ['-hide_banner' , '-loglevel' , 'fatal' , '-an' , '-sn' , '-dn' , '-to' , f'{ max_timestamp } ms' ]
198- if isinstance ( data_or_filepath , str ) :
199- result , err = stub_call_ff ('ffmpeg' , * ffmpeg_input_params , '-i' , data_or_filepath ,
200+ if path is not None :
201+ result , err = stub_call_ff ('ffmpeg' , * ffmpeg_input_params , '-i' , path ,
200202 '-f' , 'rawvideo' , '-s' , f'{ FirstFrameResolution } x{ FirstFrameResolution } ' ,
201203 '-pix_fmt' , 'rgb24' , 'pipe:'
202204 )
203- else :
205+ elif data is not None :
204206 result , err = stub_call_ff ('ffmpeg' , * ffmpeg_input_params , '-i' , 'pipe:0' ,
205207 '-f' , 'rawvideo' , '-s' , f'{ FirstFrameResolution } x{ FirstFrameResolution } ' ,
206208 '-pix_fmt' , 'rgb24' , 'pipe:1' ,
207- stdin_data = data_or_filepath )
209+ stdin_data = data )
210+ else :
211+ raise ValueError ("`path` or `data` argument must be specified." )
208212 if err :
209213 print ('DEBUG:' , err )
210214 return - 1
@@ -223,22 +227,25 @@ def get_first_timestamp(video_info: dict, data_or_filepath) -> int:
223227 return 0
224228
225229
226- def get_frames (timestamps : list , data_or_filepath , * ffmpeg_out_params ) -> list :
230+ def get_frames (timestamps : list , path , data , * ffmpeg_out_params ) -> list :
231+ """Accepts path(bytes/str) or data for processing in memory."""
227232 ret = [False ]
228233 for _ in range (len (timestamps )):
229234 ret .append (b'' )
235+ if path is None and data is None :
236+ raise ValueError ("`path` or `data` argument must be specified." )
230237 ffmpeg_input_params = ['-hide_banner' , '-loglevel' , 'fatal' , '-an' , '-sn' , '-dn' ]
231238 for index , timestamp in enumerate (timestamps ):
232- if isinstance ( data_or_filepath , str ) :
239+ if path is not None :
233240 result , err = stub_call_ff ('ffmpeg' , * ffmpeg_input_params ,
234- '-ss' , f'{ timestamp } ms' , '-i' , data_or_filepath ,
241+ '-ss' , f'{ timestamp } ms' , '-i' , path ,
235242 '-f' , 'image2' , '-c:v' , 'bmp' , '-frames' , '1' , * ffmpeg_out_params , 'pipe:'
236243 )
237244 else :
238245 result , err = stub_call_ff ('ffmpeg' , * ffmpeg_input_params ,
239246 '-ss' , f'{ timestamp } ms' , '-i' , 'pipe:0' ,
240247 '-f' , 'image2' , '-c:v' , 'bmp' , '-frames' , '1' , * ffmpeg_out_params , 'pipe:1' ,
241- stdin_data = data_or_filepath )
248+ stdin_data = data )
242249 if err :
243250 print ('DEBUG:' , err )
244251 return ret
0 commit comments