11package com .codefans .practicetask .file ;
22
3+ import it .sauronsoftware .jave .Encoder ;
4+ import it .sauronsoftware .jave .MultimediaInfo ;
5+
36import java .io .File ;
47import java .util .*;
58
@@ -52,27 +55,28 @@ public static void main(String[] args) {
5255
5356 public void taskExecute () {
5457// List<String> fileList = new ArrayList<>();
55- Map <String , Long > fileSizeMap = new HashMap <>(1024 );
58+ Map <String , FileInfo > fileSizeMap = new HashMap <>(1024 );
5659 this .gather (fileSizeMap );
57- List <Map .Entry <String , Long >> fileSizeList = this .sort (fileSizeMap , null );
60+ List <Map .Entry <String , FileInfo >> fileSizeList = this .sort (fileSizeMap , null );
5861 this .print (fileSizeList );
5962 }
6063
61- public void gather (Map <String , Long > fileSizeMap ) {
64+ public void gather (Map <String , FileInfo > fileSizeMap ) {
6265 String [] dirs = new String []{
6366 "D:\\ Films" ,
6467 "E:\\ Films" ,
6568 "F:\\ Films" ,
6669 "G:\\ Films" ,
70+ "C:\\ Users\\ Adminstrator\\ Downloads\\ Films" ,
6771 };
6872 /**
6973 * 扫描视频
7074 */
71- // String scanType = SCAN_VIDEO;
75+ String scanType = SCAN_VIDEO ;
7276 /**
7377 * 扫描图片
7478 */
75- String scanType = SCAN_IMAGE ;
79+ // String scanType = SCAN_IMAGE;
7680 for (int i = 0 ; i < dirs .length ; i ++) {
7781 this .gather (dirs [i ], scanType , fileSizeMap );
7882 }
@@ -83,7 +87,7 @@ public void gather(Map<String, Long> fileSizeMap) {
8387 * @param dir
8488 * @param fileSizeMap
8589 */
86- public void gather (String dir , String scanType , Map <String , Long > fileSizeMap ) {
90+ public void gather (String dir , String scanType , Map <String , FileInfo > fileSizeMap ) {
8791 File path = new File (dir );
8892 if (path .isDirectory ()) {
8993 File [] files = path .listFiles ();
@@ -100,7 +104,7 @@ public void gather(String dir, String scanType, Map<String, Long> fileSizeMap) {
100104 }
101105 }
102106
103- public void addFile (File file , String scanType , Map <String , Long > fileSizeMap ) {
107+ public void addFile (File file , String scanType , Map <String , FileInfo > fileSizeMap ) {
104108 String name = file .getName ();
105109 String suffix = "" ;
106110 if (name .lastIndexOf ("." ) >= 0 ) {
@@ -117,7 +121,9 @@ public void addFile(File file, String scanType, Map<String, Long> fileSizeMap) {
117121 }
118122 }
119123 if (addThisFile ) {
120- fileSizeMap .put (file .getAbsolutePath (), file .length ());
124+ long size = file .length ();
125+ long duration = this .getDuration (file );
126+ fileSizeMap .put (file .getAbsolutePath (), new FileInfo (size , duration ));
121127 }
122128 }
123129
@@ -127,18 +133,18 @@ public void addFile(File file, String scanType, Map<String, Long> fileSizeMap) {
127133 * @param sortType: desc-降序; asc-升序, 默认值:desc-降序
128134 * @return
129135 */
130- public List <Map .Entry <String , Long >> sort (Map <String , Long > fileSizeMap , String sortType ) {
131- Map <String , Long > sortedMap = new LinkedHashMap <>(fileSizeMap .size ());
136+ public List <Map .Entry <String , FileInfo >> sort (Map <String , FileInfo > fileSizeMap , String sortType ) {
137+ Map <String , FileInfo > sortedMap = new LinkedHashMap <>(fileSizeMap .size ());
132138
133- List <Map .Entry <String , Long >> infoIds = new ArrayList <>(fileSizeMap .entrySet ());
139+ List <Map .Entry <String , FileInfo >> infoIds = new ArrayList <>(fileSizeMap .entrySet ());
134140
135141 //排序方法
136- Collections .sort (infoIds , new Comparator <Map .Entry <String , Long >>() {
137- public int compare (Map .Entry <String , Long > o1 , Map .Entry <String , Long > o2 ) {
142+ Collections .sort (infoIds , new Comparator <Map .Entry <String , FileInfo >>() {
143+ public int compare (Map .Entry <String , FileInfo > o1 , Map .Entry <String , FileInfo > o2 ) {
138144 if ("asc" .equals (sortType )) {
139- return o1 .getValue ().compareTo (o2 .getValue ());
145+ return new Long ( o1 .getValue ().getSize ()). compareTo (new Long ( o2 .getValue (). getSize () ));
140146 } else {
141- return o2 .getValue ().compareTo (o1 .getValue ());
147+ return new Long ( o2 .getValue ().getSize ()). compareTo (new Long ( o1 .getValue (). getSize () ));
142148 }
143149 }
144150 });
@@ -164,17 +170,62 @@ public void print(Map<String, Long> fileSizeMap) {
164170 * 打印
165171 * @param fileSizeMap
166172 */
167- public void print (List <Map .Entry <String , Long >> fileSizeMap ) {
168- Iterator <Map .Entry <String , Long >> iter = fileSizeMap .iterator ();
169- Map .Entry <String , Long > key = null ;
173+ public void print (List <Map .Entry <String , FileInfo >> fileSizeMap ) {
174+ Iterator <Map .Entry <String , FileInfo >> iter = fileSizeMap .iterator ();
175+ Map .Entry <String , FileInfo > key = null ;
170176 int index = 1 ;
171177 long totalSize = 0 ;
178+ FileInfo fileInfo = null ;
172179 while (iter .hasNext ()) {
173180 key = iter .next ();
174- System .out .println ("第[" + (index ++) + "]个文件, size:[" + super .formetFileSize (key .getValue ()) + "], path:[" + key .getKey () + "]" );
175- totalSize += key .getValue ();
181+ fileInfo = key .getValue ();
182+ System .out .println ("第[" + (index ++) + "]个文件, size:[" + super .formetFileSize (fileInfo .getSize ()) + "], duration:[" + (fileInfo .getDuration ()/1000 ) + "]分钟, path:[" + key .getKey () + "]" );
183+ totalSize += fileInfo .getSize ();
176184 }
177185 System .out .println ("[" + (--index ) + "]个文件总大小为:[" + super .formetFileSize (totalSize ) + "]" );
178186 }
179187
188+ /**
189+ * 获取视频时长, 单位秒s
190+ * @return
191+ */
192+ public long getDuration (File source ) {
193+ Encoder encoder = new Encoder ();
194+ long duration = 0 ;
195+ try {
196+ MultimediaInfo m = encoder .getInfo (source );
197+ long ls = m .getDuration ();
198+ duration = ls / 1000 ;
199+ // System.out.println("此视频时长为:" + duration + "秒!");
200+ } catch (Exception e ) {
201+ System .out .printf ("文件:%s, 异常:%s/n" , source .getAbsolutePath (), e );
202+ }
203+ return duration ;
204+ }
205+
206+ class FileInfo {
207+ long size ;
208+ long duration ;
209+
210+ public FileInfo (long size , long duration ) {
211+ this .size = size ;
212+ this .duration = duration ;
213+ }
214+
215+ public long getSize () {
216+ return size ;
217+ }
218+
219+ public void setSize (long size ) {
220+ this .size = size ;
221+ }
222+
223+ public long getDuration () {
224+ return duration ;
225+ }
226+
227+ public void setDuration (long duration ) {
228+ this .duration = duration ;
229+ }
230+ }
180231}
0 commit comments