github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/live_upload_reader_ffmpeg_filename_builder.go (about) 1 package sdk 2 3 import ( 4 "path/filepath" 5 "strconv" 6 "strings" 7 ) 8 9 // FileNameBuilder build file name based output format 10 type FileNameBuilder interface { 11 OutDir() string 12 FileExt() string 13 OutFile() string 14 ClipsFile(index int) string 15 ClipsFileName(index int) string 16 } 17 18 var ( 19 builderFactory = make(map[string]func(outDir, fileName, fileExt string) FileNameBuilder) 20 ) 21 22 func init() { 23 builderFactory[".m3u8"] = func(outDir, fileName, fileExt string) FileNameBuilder { 24 return m3u8NameBuilder{ 25 26 outDir: outDir, 27 fileName: fileName, 28 fileExt: fileExt, 29 } 30 } 31 } 32 33 // createFileNameBuilder create a FileNameBuilder instance 34 func createFileNameBuilder(file string) FileNameBuilder { 35 fileExt := filepath.Ext(file) 36 37 dir, fileName := filepath.Split(file) 38 39 fileName = strings.TrimRight(fileName, fileExt) 40 41 factory, ok := builderFactory[strings.ToLower(fileExt)] 42 43 var builder FileNameBuilder 44 45 if ok { 46 builder = factory(dir, fileName, fileExt) 47 } else { 48 builder = &mediaNameBuilder{ 49 outDir: dir, 50 fileName: fileName, 51 fileExt: fileExt, 52 } 53 } 54 55 return builder 56 57 } 58 59 type mediaNameBuilder struct { 60 // outDir output dir 61 outDir string 62 // fileName output file name 63 fileName string 64 // fileExt extention of output file 65 fileExt string 66 } 67 68 // OutDir output dir 69 func (b mediaNameBuilder) OutDir() string { 70 return b.outDir 71 } 72 73 // OutFile build file 74 func (b mediaNameBuilder) OutFile() string { 75 return filepath.Join(b.outDir, b.fileName+"%d"+b.fileExt) 76 } 77 78 // FileExt get file ext 79 func (b mediaNameBuilder) FileExt() string { 80 return b.fileExt 81 } 82 83 // ClipsFile build filename 84 func (b mediaNameBuilder) ClipsFile(index int) string { 85 return filepath.Join(b.outDir, b.ClipsFileName(index)) 86 } 87 88 // ClipsFileName build filename 89 func (b mediaNameBuilder) ClipsFileName(index int) string { 90 return b.fileName + strconv.Itoa(index) + b.fileExt 91 } 92 93 type m3u8NameBuilder struct { 94 // outDir output dir 95 outDir string 96 // fileName output file name 97 fileName string 98 // fileExt extention of output file 99 fileExt string 100 } 101 102 // OutDir output dir 103 func (b m3u8NameBuilder) OutDir() string { 104 return b.outDir 105 } 106 107 // File build file 108 func (b m3u8NameBuilder) OutFile() string { 109 return filepath.Join(b.outDir, b.fileName+b.fileExt) 110 } 111 112 func (b m3u8NameBuilder) ClipsFile(index int) string { 113 return filepath.Join(b.outDir, b.ClipsFileName(index)) 114 } 115 116 func (b m3u8NameBuilder) ClipsFileName(index int) string { 117 return b.fileName + strconv.Itoa(index) + ".ts" 118 } 119 120 // FileExt get file ext 121 func (b m3u8NameBuilder) FileExt() string { 122 return b.fileExt 123 }