@@ -10,7 +10,7 @@ package main
1010////////////////////////////////////////////////////////////////////////////
1111// Porgram: FfCvt
1212// Purpose: ffmpeg convert wrapper tool
13- // Authors: Tong Sun (c) 2015-2022 , All rights reserved
13+ // Authors: Tong Sun (c) 2015-2023 , All rights reserved
1414////////////////////////////////////////////////////////////////////////////
1515
1616//go:generate sh -x ffcvt_cli.sh
@@ -37,17 +37,30 @@ import (
3737
3838const _encodedExt = "_.mkv"
3939
40+ // video encompass data around a single video
41+ type video struct {
42+ name string
43+ size , sum int64
44+ pct int
45+ }
46+
47+ // videoCol is the collection for the given set of videos
48+ type videoCol struct {
49+ videos []video
50+ sum int64
51+ }
52+
4053////////////////////////////////////////////////////////////////////////////
4154// Global variables definitions
4255
4356var (
44- version = "1.8.1 "
45- date = "2022-12-18 "
57+ version = "1.9.0 "
58+ date = "2023-03-16 "
4659
4760 encodedExt string = _encodedExt
4861 totalOrg int64 = 1
4962 totalNew int64 = 1
50- videos [] string
63+ vidCol videoCol
5164 workDirs []string
5265 cutOps string = ""
5366)
@@ -180,6 +193,15 @@ cut_ok:
180193 transcodeFile (Opts .File )
181194 } else if Opts .Directory != "" {
182195 filepath .Walk (Opts .Directory , visit )
196+ // calculate (finished) percentage for each video
197+ n := len (vidCol .videos )
198+ if n > 0 {
199+ totalSize := vidCol .videos [n - 1 ].sum
200+ for i , _ := range vidCol .videos {
201+ vidCol .videos [i ].pct = int (vidCol .videos [i ].sum * 100 / totalSize )
202+ }
203+ }
204+ //fmt.Printf("%v", vidCol)
183205 transcodeVideos (startTime )
184206 }
185207 // par2 creating
@@ -196,23 +218,13 @@ cut_ok:
196218}
197219
198220////////////////////////////////////////////////////////////////////////////
199- // Function definitions
200-
201- //==========================================================================
202- // Directory & files handling
203-
204- func visit (path string , f os.FileInfo , err error ) error {
205- if f .IsDir () {
206- return nil
207- }
208-
209- appendVideo (Opts .Directory + string (os .PathSeparator ) + path )
210- return nil
211- }
221+ // Methods definitions
212222
213223// Append the video file to the list, unless it's encoded already
214224// and duplicate none-video files to dest dir as well
215- func appendVideo (fname string ) {
225+ func (vidCol * videoCol ) append (path string , size int64 ) {
226+ fname := Opts .Directory + string (os .PathSeparator ) + path
227+
216228 if Opts .WDirectory == "" && fname [len (fname )- 5 :] == encodedExt {
217229 debug ("Already-encoded file ignored: " + fname , 1 )
218230 return
@@ -243,9 +255,29 @@ func appendVideo(fname string) {
243255 return
244256 }
245257
246- videos = append (videos , fname )
258+ vidCol .sum += size
259+ vidCol .videos = append (vidCol .videos ,
260+ video {name : fname , size : size , sum : vidCol .sum })
261+
262+ }
263+
264+ ////////////////////////////////////////////////////////////////////////////
265+ // Function definitions
266+
267+ //==========================================================================
268+ // Directory & files handling
269+
270+ // visit will visit the source directory and queue videos found there to vidCol
271+ func visit (path string , f os.FileInfo , err error ) error {
272+ if f .IsDir () {
273+ return nil
274+ }
275+
276+ vidCol .append (path , f .Size ())
277+ return nil
247278}
248279
280+ // visitWDir will visit the work dir and add all directories there to workDirs
249281func visitWDir (path string , f os.FileInfo , err error ) error {
250282 if ! f .IsDir () {
251283 return nil
@@ -256,6 +288,7 @@ func visitWDir(path string, f os.FileInfo, err error) error {
256288 return nil
257289}
258290
291+ // createPar2s will create par2s files for each dir in workDirs
259292func createPar2s (workDirs []string ) {
260293 fmt .Printf ("\n == Creating par2 files\n \n " )
261294 for ii , dir := range workDirs {
@@ -283,23 +316,24 @@ func createPar2s(workDirs []string) {
283316
284317// Transcode videos in the global videos array
285318func transcodeVideos (startTime time.Time ) {
286- videosTotal := len (videos )
287- for i , inputName := range videos {
319+ videosTotal := len (vidCol .videos )
320+ for i , v := range vidCol .videos {
321+ inputName := v .name
288322 videoNdx := i + 1
289323
290324 if Opts .NoClobber && fileExist (getOutputName (inputName )) {
291325 debug ("Encoded file exist for: " + inputName , 1 )
292326 continue
293327 }
294328
295- fmt .Printf ("\n == Transcoding [%d/%d]: '%s'\n under %s\n " ,
296- videoNdx , videosTotal , filepath .Base (inputName ), filepath .Dir (inputName ))
329+ fmt .Printf ("\n == Transcoding [%d/%d] (%d%%) : '%s'\n under %s\n " ,
330+ videoNdx , videosTotal , v . pct , filepath .Base (inputName ), filepath .Dir (inputName ))
297331 transcodeFile (inputName )
298332 fmt .Printf ("Time taken so far %s\n " , time .Since (startTime ))
299333 fmt .Printf ("Finishing the remaining %d%% in %s\n " ,
300- ( videosTotal - videoNdx ) * 100 / videosTotal ,
334+ 100 - v . pct ,
301335 time .Duration (int64 (float32 (time .Since (startTime ))*
302- float32 (videosTotal - videoNdx )/ float32 (videoNdx ))))
336+ float32 (100 - v . pct )/ float32 (v . pct ))))
303337 }
304338}
305339
0 commit comments