2021-12-14 08:13:49 +13:00
use std ::collections ::{ BTreeMap , BTreeSet , HashMap } ;
2023-05-03 08:37:12 +12:00
use std ::fs ::{ DirEntry , File , Metadata } ;
2023-07-10 18:36:03 +12:00
use std ::io ::{ Write , * } ;
2023-05-03 08:37:12 +12:00
use std ::mem ;
2021-11-23 23:10:24 +13:00
use std ::path ::{ Path , PathBuf } ;
2023-05-08 06:54:05 +12:00
use std ::sync ::atomic ::Ordering ;
2021-11-28 08:49:20 +13:00
use crossbeam_channel ::Receiver ;
use ffmpeg_cmdline_utils ::FfmpegErrorKind ::FfmpegNotFound ;
2023-05-03 08:37:12 +12:00
use futures ::channel ::mpsc ::UnboundedSender ;
2023-07-10 18:36:03 +12:00
use humansize ::{ format_size , BINARY } ;
2023-10-02 02:20:58 +13:00
use log ::{ debug , info } ;
2021-11-28 08:49:20 +13:00
use rayon ::prelude ::* ;
2021-12-06 06:34:12 +13:00
use serde ::{ Deserialize , Serialize } ;
2021-11-23 23:10:24 +13:00
use vid_dup_finder_lib ::HashCreationErrorKind ::DetermineVideo ;
use vid_dup_finder_lib ::{ NormalizedTolerance , VideoHash } ;
2023-07-10 18:36:03 +12:00
use crate ::common ::{ check_folder_children , open_cache_folder , prepare_thread_handler_common , send_info_and_wait_for_ending_all_threads , VIDEO_FILES_EXTENSIONS } ;
2023-05-11 07:27:41 +12:00
use crate ::common_dir_traversal ::{ common_get_entry_data_metadata , common_read_dir , get_lowercase_name , get_modified_time , CheckingMethod , ProgressData , ToolType } ;
2021-11-28 08:49:20 +13:00
use crate ::common_messages ::Messages ;
2023-10-02 09:26:24 +13:00
use crate ::common_tool ::{ CommonData , CommonToolData } ;
2023-05-08 06:54:05 +12:00
use crate ::common_traits ::{ DebugPrint , PrintResults , ResultEntry , SaveResults } ;
2022-01-20 10:35:07 +13:00
use crate ::flc ;
use crate ::localizer_core ::generate_translation_hashmap ;
2021-11-28 08:49:20 +13:00
2021-11-23 23:10:24 +13:00
pub const MAX_TOLERANCE : i32 = 20 ;
2021-12-06 06:34:12 +13:00
#[ derive(Clone, Debug, Serialize, Deserialize) ]
2021-11-23 23:10:24 +13:00
pub struct FileEntry {
pub path : PathBuf ,
pub size : u64 ,
pub modified_date : u64 ,
pub vhash : VideoHash ,
2021-11-30 01:53:09 +13:00
pub error : String ,
2021-11-23 23:10:24 +13:00
}
2023-10-02 09:44:40 +13:00
2023-05-08 06:54:05 +12:00
impl ResultEntry for FileEntry {
fn get_path ( & self ) -> & Path {
& self . path
}
}
2021-11-23 23:10:24 +13:00
/// Distance metric to use with the BK-tree.
struct Hamming ;
impl bk_tree ::Metric < Vec < u8 > > for Hamming {
2023-01-29 06:54:02 +13:00
#[ inline ]
2021-11-23 23:10:24 +13:00
fn distance ( & self , a : & Vec < u8 > , b : & Vec < u8 > ) -> u32 {
hamming ::distance_fast ( a , b ) . unwrap ( ) as u32
}
2023-01-29 06:54:02 +13:00
#[ inline ]
2021-11-23 23:10:24 +13:00
fn threshold_distance ( & self , a : & Vec < u8 > , b : & Vec < u8 > , _threshold : u32 ) -> Option < u32 > {
Some ( self . distance ( a , b ) )
}
}
/// Struct to store most basics info about all folder
pub struct SimilarVideos {
2023-10-02 09:26:24 +13:00
common_data : CommonToolData ,
2021-11-23 23:10:24 +13:00
information : Info ,
similar_vectors : Vec < Vec < FileEntry > > ,
2021-12-24 21:18:55 +13:00
similar_referenced_vectors : Vec < ( FileEntry , Vec < FileEntry > ) > ,
2021-11-23 23:10:24 +13:00
videos_hashes : BTreeMap < Vec < u8 > , Vec < FileEntry > > ,
videos_to_check : BTreeMap < String , FileEntry > ,
tolerance : i32 ,
2021-12-14 08:13:49 +13:00
exclude_videos_with_same_size : bool ,
2023-10-02 09:26:24 +13:00
}
impl CommonData for SimilarVideos {
fn get_cd ( & self ) -> & CommonToolData {
& self . common_data
}
fn get_cd_mut ( & mut self ) -> & mut CommonToolData {
& mut self . common_data
}
2021-11-23 23:10:24 +13:00
}
/// Info struck with helpful information's about results
#[ derive(Default) ]
pub struct Info {
2021-12-24 21:18:55 +13:00
pub number_of_duplicates : usize ,
pub number_of_groups : u64 ,
2021-11-23 23:10:24 +13:00
}
2021-11-28 08:57:10 +13:00
2021-11-23 23:10:24 +13:00
impl Info {
pub fn new ( ) -> Self {
Default ::default ( )
}
}
2023-01-29 06:54:02 +13:00
/// Method implementation for `EmptyFolder`
2021-11-23 23:10:24 +13:00
impl SimilarVideos {
/// New function providing basics values
2023-10-02 09:26:24 +13:00
2021-11-23 23:10:24 +13:00
pub fn new ( ) -> Self {
Self {
2023-10-02 09:26:24 +13:00
common_data : CommonToolData ::new ( ToolType ::SimilarVideos ) ,
2021-11-23 23:10:24 +13:00
information : Default ::default ( ) ,
similar_vectors : vec ! [ ] ,
videos_hashes : Default ::default ( ) ,
videos_to_check : Default ::default ( ) ,
tolerance : 10 ,
2021-12-14 08:13:49 +13:00
exclude_videos_with_same_size : false ,
2021-12-24 21:18:55 +13:00
similar_referenced_vectors : vec ! [ ] ,
2021-11-23 23:10:24 +13:00
}
}
2021-12-14 08:13:49 +13:00
pub fn set_exclude_videos_with_same_size ( & mut self , exclude_videos_with_same_size : bool ) {
self . exclude_videos_with_same_size = exclude_videos_with_same_size ;
}
2021-11-23 23:10:24 +13:00
pub fn set_tolerance ( & mut self , tolerance : i32 ) {
assert! ( ( 0 ..= MAX_TOLERANCE ) . contains ( & tolerance ) ) ;
2023-01-29 06:54:02 +13:00
self . tolerance = tolerance ;
2021-11-23 23:10:24 +13:00
}
pub const fn get_similar_videos ( & self ) -> & Vec < Vec < FileEntry > > {
& self . similar_vectors
}
pub const fn get_information ( & self ) -> & Info {
& self . information
}
2021-12-24 21:18:55 +13:00
pub fn get_similar_videos_referenced ( & self ) -> & Vec < ( FileEntry , Vec < FileEntry > ) > {
& self . similar_referenced_vectors
}
pub fn get_number_of_base_duplicated_files ( & self ) -> usize {
2023-10-02 09:26:24 +13:00
if self . common_data . use_reference_folders {
2021-12-24 21:18:55 +13:00
self . similar_referenced_vectors . len ( )
} else {
self . similar_vectors . len ( )
}
}
pub fn get_use_reference ( & self ) -> bool {
2023-10-02 09:26:24 +13:00
self . common_data . use_reference_folders
2021-12-24 21:18:55 +13:00
}
2021-11-23 23:10:24 +13:00
/// Public function used by CLI to search for empty folders
2023-05-03 08:37:12 +12:00
pub fn find_similar_videos ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & UnboundedSender < ProgressData > > ) {
2023-10-02 02:20:58 +13:00
info! ( " Starting finding similar videos " ) ;
2021-11-23 23:10:24 +13:00
if ! check_if_ffmpeg_is_installed ( ) {
2023-10-02 09:26:24 +13:00
self . common_data . text_messages . errors . push ( flc! ( " core_ffmpeg_not_found " ) ) ;
2022-07-06 05:58:57 +12:00
#[ cfg(target_os = " windows " ) ]
2023-10-02 09:26:24 +13:00
self . common_data . text_messages . errors . push ( flc! ( " core_ffmpeg_not_found_windows " ) ) ;
2022-07-06 05:58:57 +12:00
#[ cfg(target_os = " linux " ) ]
2023-10-02 09:26:24 +13:00
self . common_data . text_messages . errors . push ( flc! (
2022-01-15 09:14:13 +13:00
" core_ffmpeg_missing_in_snap " ,
generate_translation_hashmap ( vec! [ ( " url " , " https://github.com/snapcrafters/ffmpeg/issues/73 " . to_string ( ) ) ] )
) ) ;
2021-11-23 23:10:24 +13:00
} else {
2023-10-02 09:26:24 +13:00
self . optimize_dirs_before_start ( ) ;
self . common_data . use_reference_folders = ! self . common_data . directories . reference_directories . is_empty ( ) ;
2021-11-23 23:10:24 +13:00
if ! self . check_for_similar_videos ( stop_receiver , progress_sender ) {
2023-10-02 09:26:24 +13:00
self . common_data . stopped_search = true ;
2021-11-23 23:10:24 +13:00
return ;
}
if ! self . sort_videos ( stop_receiver , progress_sender ) {
2023-10-02 09:26:24 +13:00
self . common_data . stopped_search = true ;
2021-11-23 23:10:24 +13:00
return ;
}
// if self.delete_folders {
// self.delete_empty_folders();
// }
}
self . debug_print ( ) ;
}
// pub fn set_delete_folder(&mut self, delete_folder: bool) {
// self.delete_folders = delete_folder;
// }
/// Function to check if folder are empty.
2023-01-29 06:54:02 +13:00
/// Parameter `initial_checking` for second check before deleting to be sure that checked folder is still empty
2023-05-03 08:37:12 +12:00
fn check_for_similar_videos ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & UnboundedSender < ProgressData > > ) -> bool {
2023-10-02 02:20:58 +13:00
debug! ( " check_for_similar_videos - start " ) ;
2021-11-23 23:10:24 +13:00
let mut folders_to_check : Vec < PathBuf > = Vec ::with_capacity ( 1024 * 2 ) ; // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector
2023-10-02 09:26:24 +13:00
if ! self . common_data . allowed_extensions . using_custom_extensions ( ) {
self . common_data . allowed_extensions . extend_allowed_extensions ( VIDEO_FILES_EXTENSIONS ) ;
2022-05-24 05:04:28 +12:00
} else {
2023-10-02 09:26:24 +13:00
self . common_data . allowed_extensions . validate_allowed_extensions ( VIDEO_FILES_EXTENSIONS ) ;
if ! self . common_data . allowed_extensions . using_custom_extensions ( ) {
2022-05-24 05:04:28 +12:00
return true ;
}
2022-01-01 10:34:24 +13:00
}
2021-12-18 07:29:37 +13:00
2021-11-23 23:10:24 +13:00
// Add root folders for finding
2023-10-02 09:26:24 +13:00
for id in & self . common_data . directories . included_directories {
2021-11-23 23:10:24 +13:00
folders_to_check . push ( id . clone ( ) ) ;
}
2023-05-11 07:27:41 +12:00
let ( progress_thread_handle , progress_thread_run , atomic_counter , _check_was_stopped ) =
2023-10-02 09:26:24 +13:00
prepare_thread_handler_common ( progress_sender , 0 , 1 , 0 , CheckingMethod ::None , self . common_data . tool_type ) ;
2021-11-23 23:10:24 +13:00
while ! folders_to_check . is_empty ( ) {
if stop_receiver . is_some ( ) & & stop_receiver . unwrap ( ) . try_recv ( ) . is_ok ( ) {
2023-05-03 08:37:12 +12:00
send_info_and_wait_for_ending_all_threads ( & progress_thread_run , progress_thread_handle ) ;
2021-11-23 23:10:24 +13:00
return false ;
}
2021-12-18 07:29:37 +13:00
let segments : Vec < _ > = folders_to_check
. par_iter ( )
. map ( | current_folder | {
let mut dir_result = vec! [ ] ;
let mut warnings = vec! [ ] ;
let mut fe_result = vec! [ ] ;
2023-05-03 08:37:12 +12:00
let Some ( read_dir ) = common_read_dir ( current_folder , & mut warnings ) else {
return ( dir_result , warnings , fe_result ) ;
2021-12-18 07:29:37 +13:00
} ;
// Check every sub folder/file/link etc.
2023-05-03 08:37:12 +12:00
for entry in read_dir {
let Some ( ( entry_data , metadata ) ) = common_get_entry_data_metadata ( & entry , & mut warnings , current_folder ) else {
continue ;
2021-12-18 07:29:37 +13:00
} ;
2023-05-03 08:37:12 +12:00
2021-12-18 07:29:37 +13:00
if metadata . is_dir ( ) {
2023-05-03 08:37:12 +12:00
check_folder_children (
& mut dir_result ,
& mut warnings ,
current_folder ,
entry_data ,
2023-10-02 09:26:24 +13:00
self . common_data . recursive_search ,
& self . common_data . directories ,
& self . common_data . excluded_items ,
2023-05-03 08:37:12 +12:00
) ;
2021-12-18 07:29:37 +13:00
} else if metadata . is_file ( ) {
2023-05-03 08:37:12 +12:00
atomic_counter . fetch_add ( 1 , Ordering ::Relaxed ) ;
self . add_video_file_entry ( & metadata , entry_data , & mut fe_result , & mut warnings , current_folder ) ;
2021-12-18 07:29:37 +13:00
}
2021-11-23 23:10:24 +13:00
}
2021-12-18 07:29:37 +13:00
( dir_result , warnings , fe_result )
} )
. collect ( ) ;
// Advance the frontier
folders_to_check . clear ( ) ;
// Process collected data
for ( segment , warnings , fe_result ) in segments {
folders_to_check . extend ( segment ) ;
2023-10-02 09:26:24 +13:00
self . common_data . text_messages . warnings . extend ( warnings ) ;
2021-12-18 07:29:37 +13:00
for ( name , fe ) in fe_result {
self . videos_to_check . insert ( name , fe ) ;
2021-11-23 23:10:24 +13:00
}
}
}
2021-12-18 07:29:37 +13:00
2023-05-03 08:37:12 +12:00
send_info_and_wait_for_ending_all_threads ( & progress_thread_run , progress_thread_handle ) ;
2023-05-08 06:54:05 +12:00
2023-10-02 02:20:58 +13:00
debug! ( " check_for_similar_videos - end " ) ;
2021-11-23 23:10:24 +13:00
true
}
2023-05-03 08:37:12 +12:00
fn add_video_file_entry ( & self , metadata : & Metadata , entry_data : & DirEntry , fe_result : & mut Vec < ( String , FileEntry ) > , warnings : & mut Vec < String > , current_folder : & Path ) {
2023-09-16 05:04:05 +12:00
let Some ( file_name_lowercase ) = get_lowercase_name ( entry_data , warnings ) else {
2023-05-03 08:37:12 +12:00
return ;
} ;
2021-11-23 23:10:24 +13:00
2023-10-02 09:26:24 +13:00
if ! self . common_data . allowed_extensions . matches_filename ( & file_name_lowercase ) {
2023-05-03 08:37:12 +12:00
return ;
}
// Checking files
2023-10-02 09:26:24 +13:00
if ( self . common_data . minimal_file_size ..= self . common_data . maximal_file_size ) . contains ( & metadata . len ( ) ) {
2023-05-03 08:37:12 +12:00
let current_file_name = current_folder . join ( entry_data . file_name ( ) ) ;
2023-10-02 09:26:24 +13:00
if self . common_data . excluded_items . is_excluded ( & current_file_name ) {
2023-05-03 08:37:12 +12:00
return ;
}
let current_file_name_str = current_file_name . to_string_lossy ( ) . to_string ( ) ;
let fe : FileEntry = FileEntry {
path : current_file_name . clone ( ) ,
size : metadata . len ( ) ,
modified_date : get_modified_time ( metadata , warnings , & current_file_name , false ) ,
vhash : Default ::default ( ) ,
error : String ::new ( ) ,
} ;
2021-11-23 23:10:24 +13:00
2023-05-03 08:37:12 +12:00
fe_result . push ( ( current_file_name_str , fe ) ) ;
}
}
fn load_cache_at_start ( & mut self ) -> ( BTreeMap < String , FileEntry > , BTreeMap < String , FileEntry > , BTreeMap < String , FileEntry > ) {
2023-10-02 09:26:24 +13:00
debug! ( " load_cache_at_start - start, use cache: {} " , self . common_data . use_cache ) ;
2023-05-03 08:37:12 +12:00
let loaded_hash_map ;
2021-11-23 23:10:24 +13:00
let mut records_already_cached : BTreeMap < String , FileEntry > = Default ::default ( ) ;
let mut non_cached_files_to_check : BTreeMap < String , FileEntry > = Default ::default ( ) ;
2023-10-02 09:26:24 +13:00
if self . common_data . use_cache {
loaded_hash_map = match load_hashes_from_file ( & mut self . common_data . text_messages , self . common_data . delete_outdated_cache ) {
2021-11-23 23:10:24 +13:00
Some ( t ) = > t ,
None = > Default ::default ( ) ,
} ;
for ( name , file_entry ) in & self . videos_to_check {
if ! loaded_hash_map . contains_key ( name ) {
// If loaded data doesn't contains current videos info
non_cached_files_to_check . insert ( name . clone ( ) , file_entry . clone ( ) ) ;
} else {
2023-05-08 06:54:05 +12:00
let loaded_item = loaded_hash_map . get ( name ) . unwrap ( ) ;
if file_entry . size ! = loaded_item . size | | file_entry . modified_date ! = loaded_item . modified_date {
// When size or modification date of video changed, then it is clear that is different video
non_cached_files_to_check . insert ( name . clone ( ) , file_entry . clone ( ) ) ;
} else {
// Checking may be omitted when already there is entry with same size and modification date
records_already_cached . insert ( name . clone ( ) , loaded_item . clone ( ) ) ;
}
2021-11-23 23:10:24 +13:00
}
}
} else {
loaded_hash_map = Default ::default ( ) ;
mem ::swap ( & mut self . videos_to_check , & mut non_cached_files_to_check ) ;
}
2023-10-02 02:20:58 +13:00
debug! ( " load_cache_at_start - end " ) ;
2023-05-03 08:37:12 +12:00
( loaded_hash_map , records_already_cached , non_cached_files_to_check )
}
fn sort_videos ( & mut self , stop_receiver : Option < & Receiver < ( ) > > , progress_sender : Option < & UnboundedSender < ProgressData > > ) -> bool {
2023-10-02 02:20:58 +13:00
debug! ( " sort_videos - start " ) ;
2023-05-03 08:37:12 +12:00
let ( loaded_hash_map , records_already_cached , non_cached_files_to_check ) = self . load_cache_at_start ( ) ;
2021-11-23 23:10:24 +13:00
2023-05-08 06:54:05 +12:00
let ( progress_thread_handle , progress_thread_run , atomic_counter , check_was_stopped ) =
2023-10-02 09:26:24 +13:00
prepare_thread_handler_common ( progress_sender , 1 , 1 , non_cached_files_to_check . len ( ) , CheckingMethod ::None , self . common_data . tool_type ) ;
2023-05-03 08:37:12 +12:00
2021-11-30 01:53:09 +13:00
let mut vec_file_entry : Vec < FileEntry > = non_cached_files_to_check
2021-11-23 23:10:24 +13:00
. par_iter ( )
. map ( | file_entry | {
2023-05-03 08:37:12 +12:00
atomic_counter . fetch_add ( 1 , Ordering ::Relaxed ) ;
2021-11-23 23:10:24 +13:00
if stop_receiver . is_some ( ) & & stop_receiver . unwrap ( ) . try_recv ( ) . is_ok ( ) {
2022-05-10 05:40:35 +12:00
check_was_stopped . store ( true , Ordering ::Relaxed ) ;
2021-11-23 23:10:24 +13:00
return None ;
}
let mut file_entry = file_entry . 1. clone ( ) ;
let vhash = match VideoHash ::from_path ( & file_entry . path ) {
Ok ( t ) = > t ,
2021-11-30 01:53:09 +13:00
Err ( e ) = > {
return {
2022-12-21 20:44:26 +13:00
file_entry . error = format! ( " Failed to hash file, reason {e} " ) ;
2021-11-30 01:53:09 +13:00
Some ( file_entry )
2022-06-01 03:52:55 +12:00
} ;
2021-11-30 01:53:09 +13:00
}
2021-11-23 23:10:24 +13:00
} ;
file_entry . vhash = vhash ;
2021-11-30 01:53:09 +13:00
Some ( file_entry )
2021-11-23 23:10:24 +13:00
} )
. while_some ( )
2021-11-30 01:53:09 +13:00
. collect ::< Vec < FileEntry > > ( ) ;
2021-11-23 23:10:24 +13:00
2023-05-03 08:37:12 +12:00
send_info_and_wait_for_ending_all_threads ( & progress_thread_run , progress_thread_handle ) ;
2021-11-23 23:10:24 +13:00
// Just connect loaded results with already calculated hashes
2023-05-08 06:54:05 +12:00
vec_file_entry . extend ( records_already_cached . into_values ( ) ) ;
2021-11-23 23:10:24 +13:00
let mut hashmap_with_file_entries : HashMap < String , FileEntry > = Default ::default ( ) ;
let mut vector_of_hashes : Vec < VideoHash > = Vec ::new ( ) ;
2021-11-30 01:53:09 +13:00
for file_entry in & vec_file_entry {
// 0 means that images was not hashed correctly, e.g. could be improperly
if file_entry . error . is_empty ( ) {
hashmap_with_file_entries . insert ( file_entry . vhash . src_path ( ) . to_string_lossy ( ) . to_string ( ) , file_entry . clone ( ) ) ;
vector_of_hashes . push ( file_entry . vhash . clone ( ) ) ;
} else {
2023-10-02 09:26:24 +13:00
self . common_data . text_messages . warnings . push ( file_entry . error . clone ( ) ) ;
2021-11-30 01:53:09 +13:00
}
2021-11-23 23:10:24 +13:00
}
2023-10-02 02:20:58 +13:00
self . save_cache ( vec_file_entry , loaded_hash_map ) ;
2021-11-23 23:10:24 +13:00
2022-05-10 05:40:35 +12:00
// Break if stop was clicked after saving to cache
if check_was_stopped . load ( Ordering ::Relaxed ) {
return false ;
}
2023-05-03 08:37:12 +12:00
self . match_groups_of_videos ( vector_of_hashes , & hashmap_with_file_entries ) ;
self . remove_from_reference_folders ( ) ;
2023-10-02 09:26:24 +13:00
if self . common_data . use_reference_folders {
2023-05-03 08:37:12 +12:00
for ( _fe , vector ) in & self . similar_referenced_vectors {
self . information . number_of_duplicates + = vector . len ( ) ;
self . information . number_of_groups + = 1 ;
}
} else {
for vector in & self . similar_vectors {
self . information . number_of_duplicates + = vector . len ( ) - 1 ;
self . information . number_of_groups + = 1 ;
}
}
2021-11-23 23:10:24 +13:00
2023-05-03 08:37:12 +12:00
// Clean unused data
self . videos_hashes = Default ::default ( ) ;
self . videos_to_check = Default ::default ( ) ;
2023-10-02 02:20:58 +13:00
debug! ( " sort_videos - end " ) ;
2023-05-03 08:37:12 +12:00
true
}
2023-10-02 02:20:58 +13:00
fn save_cache ( & mut self , vec_file_entry : Vec < FileEntry > , loaded_hash_map : BTreeMap < String , FileEntry > ) {
2023-10-02 09:26:24 +13:00
debug! ( " save_cache - start, use cache: {} " , self . common_data . use_cache ) ;
if self . common_data . use_cache {
2023-10-02 02:20:58 +13:00
// Must save all results to file, old loaded from file with all currently counted results
let mut all_results : BTreeMap < String , FileEntry > = loaded_hash_map ;
for file_entry in vec_file_entry {
all_results . insert ( file_entry . path . to_string_lossy ( ) . to_string ( ) , file_entry ) ;
}
2023-10-02 09:26:24 +13:00
save_hashes_to_file ( & all_results , & mut self . common_data . text_messages , self . common_data . save_also_as_json ) ;
2023-10-02 02:20:58 +13:00
}
debug! ( " save_cache - end " ) ;
}
2023-05-03 08:37:12 +12:00
fn match_groups_of_videos ( & mut self , vector_of_hashes : Vec < VideoHash > , hashmap_with_file_entries : & HashMap < String , FileEntry > ) {
2023-10-02 02:20:58 +13:00
debug! ( " match_groups_of_videos - start " ) ;
2023-05-03 08:37:12 +12:00
let match_group = vid_dup_finder_lib ::search ( vector_of_hashes , NormalizedTolerance ::new ( self . tolerance as f64 / 100.0 f64 ) ) ;
2021-11-23 23:10:24 +13:00
let mut collected_similar_videos : Vec < Vec < FileEntry > > = Default ::default ( ) ;
for i in match_group {
let mut temp_vector : Vec < FileEntry > = Vec ::new ( ) ;
2021-12-14 08:13:49 +13:00
let mut bt_size : BTreeSet < u64 > = Default ::default ( ) ;
2021-11-23 23:10:24 +13:00
for j in i . duplicates ( ) {
2021-12-14 08:13:49 +13:00
let file_entry = hashmap_with_file_entries . get ( & j . to_string_lossy ( ) . to_string ( ) ) . unwrap ( ) ;
if self . exclude_videos_with_same_size {
if ! bt_size . contains ( & file_entry . size ) {
bt_size . insert ( file_entry . size ) ;
temp_vector . push ( file_entry . clone ( ) ) ;
}
} else {
temp_vector . push ( file_entry . clone ( ) ) ;
}
}
if temp_vector . len ( ) > 1 {
collected_similar_videos . push ( temp_vector ) ;
2021-11-23 23:10:24 +13:00
}
}
self . similar_vectors = collected_similar_videos ;
2023-10-02 02:20:58 +13:00
debug! ( " match_groups_of_videos - end " ) ;
2023-05-03 08:37:12 +12:00
}
2021-11-23 23:10:24 +13:00
2023-05-03 08:37:12 +12:00
fn remove_from_reference_folders ( & mut self ) {
2023-10-02 09:26:24 +13:00
debug! ( " remove_from_reference_folders - start, use reference folders: {} " , self . common_data . use_reference_folders ) ;
if self . common_data . use_reference_folders {
2023-05-08 06:54:05 +12:00
self . similar_referenced_vectors = mem ::take ( & mut self . similar_vectors )
2021-12-24 21:18:55 +13:00
. into_iter ( )
. filter_map ( | vec_file_entry | {
2023-10-02 09:26:24 +13:00
let ( mut files_from_referenced_folders , normal_files ) : ( Vec < _ > , Vec < _ > ) = vec_file_entry
. into_iter ( )
. partition ( | e | self . common_data . directories . is_in_referenced_directory ( e . get_path ( ) ) ) ;
2021-12-24 21:18:55 +13:00
if files_from_referenced_folders . is_empty ( ) | | normal_files . is_empty ( ) {
None
} else {
Some ( ( files_from_referenced_folders . pop ( ) . unwrap ( ) , normal_files ) )
}
} )
. collect ::< Vec < ( FileEntry , Vec < FileEntry > ) > > ( ) ;
}
2023-10-02 02:20:58 +13:00
debug! ( " remove_from_reference_folders - end " ) ;
2021-11-23 23:10:24 +13:00
}
}
2021-11-28 08:57:10 +13:00
2021-11-23 23:10:24 +13:00
impl Default for SimilarVideos {
fn default ( ) -> Self {
Self ::new ( )
}
}
impl DebugPrint for SimilarVideos {
#[ allow(dead_code) ]
#[ allow(unreachable_code) ]
fn debug_print ( & self ) {
#[ cfg(not(debug_assertions)) ]
{
return ;
}
println! ( " ---------------DEBUG PRINT--------------- " ) ;
2023-10-02 09:26:24 +13:00
println! ( " Included directories - {:?} " , self . common_data . directories . included_directories ) ;
2023-10-02 09:44:40 +13:00
self . debug_print_common ( ) ;
2021-11-23 23:10:24 +13:00
println! ( " ----------------------------------------- " ) ;
}
}
2021-11-28 08:57:10 +13:00
2021-11-23 23:10:24 +13:00
impl SaveResults for SimilarVideos {
fn save_results_to_file ( & mut self , file_name : & str ) -> bool {
let file_name : String = match file_name {
" " = > " results.txt " . to_string ( ) ,
k = > k . to_string ( ) ,
} ;
let file_handler = match File ::create ( & file_name ) {
Ok ( t ) = > t ,
Err ( e ) = > {
2023-10-02 09:26:24 +13:00
self . common_data . text_messages . errors . push ( format! ( " Failed to create file {file_name} , reason {e} " ) ) ;
2021-11-23 23:10:24 +13:00
return false ;
}
} ;
let mut writer = BufWriter ::new ( file_handler ) ;
if let Err ( e ) = writeln! (
writer ,
" Results of searching {:?} with excluded directories {:?} and excluded items {:?} " ,
2023-10-02 09:26:24 +13:00
self . common_data . directories . included_directories , self . common_data . directories . excluded_directories , self . common_data . excluded_items . items
2021-11-23 23:10:24 +13:00
) {
2023-10-02 09:26:24 +13:00
self . common_data
. text_messages
. errors
. push ( format! ( " Failed to save results to file {file_name} , reason {e} " ) ) ;
2021-11-23 23:10:24 +13:00
return false ;
}
if ! self . similar_vectors . is_empty ( ) {
write! ( writer , " {} videos which have similar friends \n \n " , self . similar_vectors . len ( ) ) . unwrap ( ) ;
2023-01-29 06:54:02 +13:00
for struct_similar in & self . similar_vectors {
2023-07-10 18:08:07 +12:00
writeln! ( writer , " Found {} videos which have similar friends " , struct_similar . len ( ) ) . unwrap ( ) ;
2021-11-23 23:10:24 +13:00
for file_entry in struct_similar {
2022-09-14 08:18:07 +12:00
writeln! ( writer , " {} - {} " , file_entry . path . display ( ) , format_size ( file_entry . size , BINARY ) ) . unwrap ( ) ;
2021-11-23 23:10:24 +13:00
}
writeln! ( writer ) . unwrap ( ) ;
}
} else {
write! ( writer , " Not found any similar videos. " ) . unwrap ( ) ;
}
true
}
}
2021-11-28 08:57:10 +13:00
2021-11-23 23:10:24 +13:00
impl PrintResults for SimilarVideos {
fn print_results ( & self ) {
if ! self . similar_vectors . is_empty ( ) {
println! ( " Found {} videos which have similar friends " , self . similar_vectors . len ( ) ) ;
for vec_file_entry in & self . similar_vectors {
for file_entry in vec_file_entry {
2022-09-14 08:18:07 +12:00
println! ( " {} - {} " , file_entry . path . display ( ) , format_size ( file_entry . size , BINARY ) ) ;
2021-11-23 23:10:24 +13:00
}
println! ( ) ;
}
}
}
}
2022-01-06 10:47:27 +13:00
pub fn save_hashes_to_file ( hashmap : & BTreeMap < String , FileEntry > , text_messages : & mut Messages , save_also_as_json : bool ) {
if let Some ( ( ( file_handler , cache_file ) , ( file_handler_json , cache_file_json ) ) ) = open_cache_folder ( & get_cache_file ( ) , true , save_also_as_json , & mut text_messages . warnings ) {
{
let writer = BufWriter ::new ( file_handler . unwrap ( ) ) ; // Unwrap because cannot fail here
if let Err ( e ) = bincode ::serialize_into ( writer , hashmap ) {
2021-12-22 06:44:20 +13:00
text_messages
2022-01-06 10:47:27 +13:00
. warnings
. push ( format! ( " Cannot write data to cache file {} , reason {} " , cache_file . display ( ) , e ) ) ;
2021-11-23 23:10:24 +13:00
return ;
}
}
2022-01-06 10:47:27 +13:00
if save_also_as_json {
if let Some ( file_handler_json ) = file_handler_json {
let writer = BufWriter ::new ( file_handler_json ) ;
if let Err ( e ) = serde_json ::to_writer ( writer , hashmap ) {
text_messages
. warnings
. push ( format! ( " Cannot write data to cache file {} , reason {} " , cache_file_json . display ( ) , e ) ) ;
return ;
}
}
2021-12-06 09:01:16 +13:00
}
text_messages . messages . push ( format! ( " Properly saved to file {} cache entries. " , hashmap . len ( ) ) ) ;
2021-11-23 23:10:24 +13:00
}
}
2021-11-28 08:57:10 +13:00
2021-12-01 00:45:09 +13:00
pub fn load_hashes_from_file ( text_messages : & mut Messages , delete_outdated_cache : bool ) -> Option < BTreeMap < String , FileEntry > > {
2022-01-06 10:47:27 +13:00
if let Some ( ( ( file_handler , cache_file ) , ( file_handler_json , cache_file_json ) ) ) = open_cache_folder ( & get_cache_file ( ) , false , true , & mut text_messages . warnings ) {
let mut hashmap_loaded_entries : BTreeMap < String , FileEntry > ;
if let Some ( file_handler ) = file_handler {
let reader = BufReader ::new ( file_handler ) ;
hashmap_loaded_entries = match bincode ::deserialize_from ( reader ) {
Ok ( t ) = > t ,
Err ( e ) = > {
text_messages
. warnings
. push ( format! ( " Failed to load data from cache file {} , reason {} " , cache_file . display ( ) , e ) ) ;
return None ;
}
} ;
} else {
let reader = BufReader ::new ( file_handler_json . unwrap ( ) ) ; // Unwrap cannot fail, because at least one file must be valid
hashmap_loaded_entries = match serde_json ::from_reader ( reader ) {
Ok ( t ) = > t ,
Err ( e ) = > {
text_messages
. warnings
. push ( format! ( " Failed to load data from cache file {} , reason {} " , cache_file_json . display ( ) , e ) ) ;
return None ;
}
} ;
}
2021-12-06 06:34:12 +13:00
// Don't load cache data if destination file not exists
2021-12-06 09:01:16 +13:00
if delete_outdated_cache {
hashmap_loaded_entries . retain ( | src_path , _file_entry | Path ::new ( src_path ) . exists ( ) ) ;
}
text_messages . messages . push ( format! ( " Properly loaded {} cache entries. " , hashmap_loaded_entries . len ( ) ) ) ;
2021-11-23 23:10:24 +13:00
return Some ( hashmap_loaded_entries ) ;
}
None
}
2021-12-06 09:01:16 +13:00
fn get_cache_file ( ) -> String {
2022-07-06 05:58:57 +12:00
" cache_similar_videos.bin " . to_string ( )
2021-12-06 09:01:16 +13:00
}
2021-11-23 23:10:24 +13:00
pub fn check_if_ffmpeg_is_installed ( ) -> bool {
2022-12-23 07:59:50 +13:00
let vid = " 9999czekoczekoczekolada999.txt " ;
2021-12-22 06:44:20 +13:00
if let Err ( DetermineVideo {
src_path : _a ,
error : FfmpegNotFound ,
2022-11-24 08:23:17 +13:00
} ) = VideoHash ::from_path ( vid )
2021-12-22 06:44:20 +13:00
{
2021-11-23 23:10:24 +13:00
return false ;
}
true
}