Skip to content

Commit 7ffde03

Browse files
committed
- [#] now finished percentage are calculated from file size
1 parent 153b9bf commit 7ffde03

4 files changed

Lines changed: 88 additions & 53 deletions

File tree

ffcvt.go

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3838
const _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

4356
var (
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
249281
func 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
259292
func 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
285318
func 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

test/ffcvt_test.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Saved: 0%
154154
] Skip symlink file: ./test2.avi
155155
] Skip symlink file: ./test3.webm
156156

157-
== Transcoding [1/1]: 'StreamSample.mkv'
157+
== Transcoding [1/1] (100%): 'StreamSample.mkv'
158158
under .
159159
] ffmpeg -i ./StreamSample.mkv -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./StreamSample_.mkv
160160
ffcvt: to execute -
@@ -166,25 +166,25 @@ Saved: 0%
166166
] None-video file './ffcvt_test.txt' duplicated to dest dir.
167167
] None-video file './test-all.sh' duplicated to dest dir.
168168

169-
== Transcoding [1/4]: 'StreamSample.mkv'
169+
== Transcoding [1/4] (99%): 'StreamSample.mkv'
170170
under .
171171
] ffmpeg -i ./StreamSample.mkv -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./StreamSample_.mkv
172172
ffcvt: to execute -
173173
ffmpeg -i ./StreamSample.mkv -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./StreamSample_.mkv
174174

175-
== Transcoding [2/4]: 'test1.avi'
175+
== Transcoding [2/4] (99%): 'test1.avi'
176176
under .
177177
] ffmpeg -i ./test1.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test1_.mkv
178178
ffcvt: to execute -
179179
ffmpeg -i ./test1.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test1_.mkv
180180

181-
== Transcoding [3/4]: 'test2.avi'
181+
== Transcoding [3/4] (99%): 'test2.avi'
182182
under .
183183
] ffmpeg -i ./test2.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test2_.mkv
184184
ffcvt: to execute -
185185
ffmpeg -i ./test2.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test2_.mkv
186186

187-
== Transcoding [4/4]: 'test3.webm'
187+
== Transcoding [4/4] (100%): 'test3.webm'
188188
under .
189189
] ffmpeg -i ./test3.webm -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test3_.mkv
190190
ffcvt: to execute -
@@ -199,7 +199,7 @@ Saved: 0%
199199
] Skip symlink file: ./test2.avi
200200
] Skip symlink file: ./test3.webm
201201

202-
== Transcoding [1/1]: 'StreamSample.mkv'
202+
== Transcoding [1/1] (100%): 'StreamSample.mkv'
203203
under .
204204
] ffmpeg -i ./StreamSample.mkv -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./StreamSample_.mkv
205205
ffcvt: to execute -
@@ -211,25 +211,25 @@ Saved: 0%
211211
] None-video file './ffcvt_test.txt' duplicated to dest dir.
212212
] None-video file './test-all.sh' duplicated to dest dir.
213213

214-
== Transcoding [1/4]: 'StreamSample.mkv'
214+
== Transcoding [1/4] (99%): 'StreamSample.mkv'
215215
under .
216216
] ffmpeg -i ./StreamSample.mkv -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./StreamSample_.mkv
217217
ffcvt: to execute -
218218
ffmpeg -i ./StreamSample.mkv -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./StreamSample_.mkv
219219

220-
== Transcoding [2/4]: 'test1.avi'
220+
== Transcoding [2/4] (99%): 'test1.avi'
221221
under .
222222
] ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test1_.mkv
223223
ffcvt: to execute -
224224
ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test1_.mkv
225225

226-
== Transcoding [3/4]: 'test2.avi'
226+
== Transcoding [3/4] (99%): 'test2.avi'
227227
under .
228228
] ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test2_.mkv
229229
ffcvt: to execute -
230230
ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test2_.mkv
231231

232-
== Transcoding [4/4]: 'test3.webm'
232+
== Transcoding [4/4] (100%): 'test3.webm'
233233
under .
234234
] ffmpeg -i ./test3.webm -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test3_.mkv
235235
ffcvt: to execute -
@@ -242,25 +242,25 @@ Saved: 0%
242242
] None-video file './ffcvt_test.txt' duplicated to dest dir.
243243
] None-video file './test-all.sh' duplicated to dest dir.
244244

245-
== Transcoding [1/4]: 'StreamSample.mkv'
245+
== Transcoding [1/4] (99%): 'StreamSample.mkv'
246246
under .
247247
] ffmpeg -i ./StreamSample.mkv -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/StreamSample.mkv
248248
ffcvt: to execute -
249249
ffmpeg -i ./StreamSample.mkv -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/StreamSample.mkv
250250

251-
== Transcoding [2/4]: 'test1.avi'
251+
== Transcoding [2/4] (99%): 'test1.avi'
252252
under .
253253
] ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/test1.mkv
254254
ffcvt: to execute -
255255
ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/test1.mkv
256256

257-
== Transcoding [3/4]: 'test2.avi'
257+
== Transcoding [3/4] (99%): 'test2.avi'
258258
under .
259259
] ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/test2.mkv
260260
ffcvt: to execute -
261261
ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/test2.mkv
262262

263-
== Transcoding [4/4]: 'test3.webm'
263+
== Transcoding [4/4] (100%): 'test3.webm'
264264
under .
265265
] ffmpeg -i ./test3.webm -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test/test3.mkv
266266
ffcvt: to execute -

test/test-all.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ FFCVT=../ffcvt
99

1010
$FFCVT -version
1111

12+
echo
1213
echo '- Test (config.go) cli help output'
1314
$FFCVT > /tmp/ffcvt_test.txt 2>&1
1415

@@ -24,8 +25,8 @@ $FFCVT -n -d . -sym >> /tmp/ffcvt_test.txt 2>&1
2425

2526
$FFCVT -n -sym -debug 2 -d . -w /tmp >> /tmp/ffcvt_test.txt 2>&1
2627

27-
echo - Compare test results
28-
sed -i '/ [0-9.]*[]*s$/d' /tmp/ffcvt_test.txt
28+
echo - 'Compare test results (0 means AOK)'
29+
sed -i '/ [0-9.]*[nmµ]*s$/d' /tmp/ffcvt_test.txt
2930
diff -wU 1 ffcvt_test.txt /tmp/ffcvt_test.txt
3031

3132
ret=$?

test2/ffcvt_test.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,25 @@ Saved: 0%
161161
] None-video file './ffcvt_test.txt' duplicated to dest dir.
162162
] None-video file './test-all.sh' duplicated to dest dir.
163163

164-
== Transcoding [1/4]: 'test_s1.avi'
164+
== Transcoding [1/4] (35%): 'test_s1.avi'
165165
under subdir
166166
] ffmpeg -i ./subdir/test_s1.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./subdir/test_s1_.mkv
167167
ffcvt: to execute -
168168
ffmpeg -i ./subdir/test_s1.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./subdir/test_s1_.mkv
169169

170-
== Transcoding [2/4]: 'test1.avi'
170+
== Transcoding [2/4] (56%): 'test1.avi'
171171
under .
172172
] ffmpeg -i ./test1.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test1_.mkv
173173
ffcvt: to execute -
174174
ffmpeg -i ./test1.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test1_.mkv
175175

176-
== Transcoding [3/4]: 'test2.avi'
176+
== Transcoding [3/4] (77%): 'test2.avi'
177177
under .
178178
] ffmpeg -i ./test2.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test2_.mkv
179179
ffcvt: to execute -
180180
ffmpeg -i ./test2.avi -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test2_.mkv
181181

182-
== Transcoding [4/4]: 'test3.webm'
182+
== Transcoding [4/4] (100%): 'test3.webm'
183183
under .
184184
] ffmpeg -i ./test3.webm -c:v libx265 -x265-params crf=28 -c:a libopus -b:a 64k ./test3_.mkv
185185
ffcvt: to execute -
@@ -201,25 +201,25 @@ Saved: 0%
201201
] None-video file './ffcvt_test.txt' duplicated to dest dir.
202202
] None-video file './test-all.sh' duplicated to dest dir.
203203

204-
== Transcoding [1/4]: 'test_s1.avi'
204+
== Transcoding [1/4] (35%): 'test_s1.avi'
205205
under subdir
206206
] ffmpeg -i ./subdir/test_s1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./subdir/test_s1_.mkv
207207
ffcvt: to execute -
208208
ffmpeg -i ./subdir/test_s1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./subdir/test_s1_.mkv
209209

210-
== Transcoding [2/4]: 'test1.avi'
210+
== Transcoding [2/4] (56%): 'test1.avi'
211211
under .
212212
] ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test1_.mkv
213213
ffcvt: to execute -
214214
ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test1_.mkv
215215

216-
== Transcoding [3/4]: 'test2.avi'
216+
== Transcoding [3/4] (77%): 'test2.avi'
217217
under .
218218
] ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test2_.mkv
219219
ffcvt: to execute -
220220
ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test2_.mkv
221221

222-
== Transcoding [4/4]: 'test3.webm'
222+
== Transcoding [4/4] (100%): 'test3.webm'
223223
under .
224224
] ffmpeg -i ./test3.webm -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy ./test3_.mkv
225225
ffcvt: to execute -
@@ -232,25 +232,25 @@ Saved: 0%
232232
] None-video file './ffcvt_test.txt' duplicated to dest dir.
233233
] None-video file './test-all.sh' duplicated to dest dir.
234234

235-
== Transcoding [1/4]: 'test_s1.avi'
235+
== Transcoding [1/4] (35%): 'test_s1.avi'
236236
under subdir
237237
] ffmpeg -i ./subdir/test_s1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/subdir/test_s1.mkv
238238
ffcvt: to execute -
239239
ffmpeg -i ./subdir/test_s1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/subdir/test_s1.mkv
240240

241-
== Transcoding [2/4]: 'test1.avi'
241+
== Transcoding [2/4] (56%): 'test1.avi'
242242
under .
243243
] ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/test1.mkv
244244
ffcvt: to execute -
245245
ffmpeg -i ./test1.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/test1.mkv
246246

247-
== Transcoding [3/4]: 'test2.avi'
247+
== Transcoding [3/4] (77%): 'test2.avi'
248248
under .
249249
] ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/test2.mkv
250250
ffcvt: to execute -
251251
ffmpeg -i ./test2.avi -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/test2.mkv
252252

253-
== Transcoding [4/4]: 'test3.webm'
253+
== Transcoding [4/4] (100%): 'test3.webm'
254254
under .
255255
] ffmpeg -i ./test3.webm -c:v libvpx-vp9 -b:v 0 -crf 42 -c:a libopus -b:a 64k -c:s copy /tmp/test2/test3.mkv
256256
ffcvt: to execute -

0 commit comments

Comments
 (0)