Skip to content

Commit c38f919

Browse files
authored
Allow overriding camera name via CLI (#93)
* Allow overriding camera name via CLI * will be used in the future * Revert back to fallback if location isnt' correct
1 parent cf54421 commit c38f919

File tree

6 files changed

+27
-67
lines changed

6 files changed

+27
-67
lines changed

cmd/import.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var importCmd = &cobra.Command{
4141
bufferSize := getFlagInt(cmd, "buffer", "1000")
4242
prefix := getFlagString(cmd, "prefix")
4343
dateRange := getFlagSlice(cmd, "range")
44+
cameraName := getFlagString(cmd, "camera_name")
4445

4546
customCameraOpts := make(map[string]interface{})
4647

@@ -87,7 +88,7 @@ var importCmd = &cobra.Command{
8788
}
8889
customCameraOpts["tag_names"] = getFlagSlice(cmd, "tag_names")
8990
}
90-
r, err := importFromCamera(c, input, filepath.Join(output, projectName), dateFormat, bufferSize, prefix, dateRange, customCameraOpts)
91+
r, err := importFromCamera(c, input, filepath.Join(output, projectName), dateFormat, bufferSize, prefix, dateRange, cameraName, customCameraOpts)
9192
if err != nil {
9293
cui.Error("Something went wrong", err)
9394
}
@@ -130,22 +131,23 @@ func init() {
130131
importCmd.Flags().StringSlice("sort_by", []string{}, "Sort files by: `camera`, `location`")
131132
importCmd.Flags().StringSlice("tag_names", []string{}, "Tag names for number of HiLight tags in last 10s of video, each position being the amount, eg: 'marked 1,good stuff,important' => num of tags: 1,2,3")
132133
importCmd.Flags().StringP("skip_aux", "s", "true", "Skip auxiliary files (GoPro: THM, LRV. DJI: SRT)")
134+
importCmd.Flags().String("camera_name", "", "Override camera name detection with specified string")
133135

134136
// Camera helpers
135137
importCmd.Flags().Bool("use_gopro", false, "Detect GoPro camera attached")
136138
importCmd.Flags().Bool("use_insta360", false, "Detect Insta360 camera attached")
137139
}
138140

139-
func importFromCamera(c utils.Camera, input string, output string, dateFormat string, bufferSize int, prefix string, dateRange []string, camOpts map[string]interface{}) (*utils.Result, error) {
141+
func importFromCamera(c utils.Camera, input string, output string, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, camOpts map[string]interface{}) (*utils.Result, error) {
140142
switch c {
141143
case utils.GoPro:
142-
return gopro.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts)
144+
return gopro.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts)
143145
case utils.DJI:
144-
return dji.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts)
146+
return dji.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts)
145147
case utils.Insta360:
146-
return insta360.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts)
148+
return insta360.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts)
147149
case utils.Android:
148-
return android.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts)
150+
return android.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts)
149151
}
150152
return nil, mErrors.ErrUnsupportedCamera("")
151153
}

pkg/android/android.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func prepare(out string, deviceFileName string, deviceModel string, mediaDate st
5959
}
6060
return bar, dayFolder, nil
6161
}
62-
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) {
62+
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) {
6363
var result utils.Result
6464

6565
sortOptions := utils.ParseCliOptions(cameraOptions)

pkg/dji/dji.go

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"gopkg.in/djherbis/times.v1"
2121
)
2222

23-
func getDeviceNameFromPhoto(path string) (string, error) {
23+
func getDeviceNameFromPhoto(path string) (string, error) { //nolint:unused
2424
f, err := os.Open(path)
2525
if err != nil {
2626
return "", err
@@ -44,10 +44,13 @@ func getDeviceNameFromPhoto(path string) (string, error) {
4444

4545
var locationService = LocationService{}
4646

47-
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) {
47+
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) {
4848
// Tested on Mavic Air 2. Osmo Pocket v1 and Spark specific changes to follow.
4949
sortOptions := utils.ParseCliOptions(cameraOptions)
5050

51+
if cameraName == "" {
52+
cameraName = "DJI Device"
53+
}
5154
di, err := disk.GetInfo(in)
5255
if err != nil {
5356
return nil, err
@@ -76,9 +79,7 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange
7679
mpb.WithWidth(60),
7780
mpb.WithRefreshRate(180*time.Millisecond))
7881

79-
inlineCounter := utils.ResultCounter{
80-
CameraName: "DJI Device",
81-
}
82+
inlineCounter := utils.ResultCounter{}
8283

8384
for _, f := range folders {
8485
r := mediaFolderRegex.MatchString(f.Name())
@@ -151,7 +152,7 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange
151152
wg.Add(1)
152153
bar := utils.GetNewBar(progressBar, int64(info.Size()), de.Name(), utils.IoTX)
153154

154-
dayFolder := utils.GetOrder(sortOptions, locationService, osPathname, out, mediaDate, "DJI Device")
155+
dayFolder := utils.GetOrder(sortOptions, locationService, osPathname, out, mediaDate, cameraName)
155156
switch ftype.Type {
156157
case Photo:
157158
if _, err := os.Stat(filepath.Join(dayFolder, "photos")); os.IsNotExist(err) {
@@ -168,25 +169,6 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange
168169
inlineCounter.SetFailure(err, filename)
169170
} else {
170171
inlineCounter.SetSuccess()
171-
172-
// Get Device Name
173-
174-
devName, err := getDeviceNameFromPhoto(osPathname)
175-
if err != nil {
176-
inlineCounter.SetFailure(err, filename)
177-
return
178-
}
179-
// Rename directory
180-
matchDeviceName, is := DeviceNames[devName]
181-
if is {
182-
devName = matchDeviceName
183-
}
184-
185-
if err != nil {
186-
inlineCounter.SetFailure(err, filename)
187-
return
188-
}
189-
inlineCounter.SetCameraName(devName)
190172
}
191173
}(f.Name(), de.Name(), osPathname, bar)
192174

@@ -266,23 +248,6 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange
266248
wg.Wait()
267249
progressBar.Shutdown()
268250

269-
// Rename each folder
270-
271-
_ = godirwalk.Walk(out, &godirwalk.Options{
272-
Unsorted: true,
273-
Callback: func(osPathname string, de *godirwalk.Dirent) error {
274-
if !de.ModeType().IsDir() {
275-
return godirwalk.SkipThis
276-
}
277-
278-
modified, err := utils.FindFolderInPath(osPathname, "DJI Device")
279-
if err == nil {
280-
_ = os.Rename(modified, strings.Replace(modified, "DJI Device", inlineCounter.CameraName, -1)) // Could be a folder already exists... time to move the content to that folder.
281-
}
282-
return nil
283-
},
284-
})
285-
286251
result.Errors = append(result.Errors, inlineCounter.Get().Errors...)
287252
result.FilesImported += inlineCounter.Get().FilesImported
288253
result.FilesNotImported = append(result.FilesNotImported, inlineCounter.Get().FilesNotImported...)

pkg/gopro/gopro.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func getRfpsFolder(pathName string) (string, error) {
5454
fpsAsFloat := strconv.Itoa(framerate.(int))
5555
return fmt.Sprintf("%dx%d %s", s.Streams[0].Width, s.Streams[0].Height, fpsAsFloat), nil
5656
}
57-
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) {
57+
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) {
5858
/* Import method using SD card bay or SD card reader */
5959

6060
dateStart := time.Date(0000, time.Month(1), 1, 0, 0, 0, 0, time.UTC)
@@ -166,12 +166,15 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange
166166

167167
root := strings.Split(gpVersion.FirmwareVersion, ".")[0]
168168

169+
if cameraName == "" {
170+
cameraName = gpVersion.CameraType
171+
}
169172
switch root {
170173
case "HD6", "HD7", "HD8", "H19", "HD9", "H21", "H22":
171-
result := importFromGoProV2(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, gpVersion.CameraType)
174+
result := importFromGoProV2(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, cameraName)
172175
return &result, nil
173176
case "HD2", "HD3", "HD4", "HX", "HD5":
174-
result := importFromGoProV1(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, gpVersion.CameraType)
177+
result := importFromGoProV1(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, cameraName)
175178
return &result, nil
176179
default:
177180
return nil, mErrors.ErrUnsupportedCamera(gpVersion.CameraType)

pkg/insta360/insta360.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ func getDeviceName(manifest string) string {
4040
}
4141
return fmt.Sprintf("Insta360%s", modelName[0])
4242
}
43-
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) {
43+
func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) {
4444
sortOptions := utils.ParseCliOptions(cameraOptions)
4545

46+
if cameraName == "" {
47+
cameraName = getDeviceName(filepath.Join(in, "DCIM", "fileinfo_list.list"))
48+
}
4649
di, err := disk.GetInfo(in)
4750
if err != nil {
4851
return nil, err
@@ -146,7 +149,7 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange
146149

147150
wg.Add(1)
148151
bar := utils.GetNewBar(progressBar, int64(info.Size()), de.Name(), utils.IoTX)
149-
dayFolder := utils.GetOrder(sortOptions, nil, osPathname, out, mediaDate, getDeviceName(filepath.Join(in, "DCIM", "fileinfo_list.list")))
152+
dayFolder := utils.GetOrder(sortOptions, nil, osPathname, out, mediaDate, cameraName)
150153

151154
x := de.Name()
152155

pkg/utils/cameras.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ func FindFolderInPath(entirePath, directory string) (string, error) {
310310

311311
type ResultCounter struct {
312312
mu sync.Mutex
313-
CameraName string
314313
Errors []error
315314
FilesNotImported []string
316315
FilesImported int
@@ -323,18 +322,6 @@ func (rc *ResultCounter) SetFailure(err error, file string) {
323322
rc.mu.Unlock()
324323
}
325324

326-
func (rc *ResultCounter) SetCameraName(camName string) {
327-
rc.mu.Lock()
328-
rc.CameraName = camName
329-
rc.mu.Unlock()
330-
}
331-
332-
func (rc *ResultCounter) GetCameraName() string {
333-
rc.mu.Lock()
334-
defer rc.mu.Unlock()
335-
return rc.CameraName
336-
}
337-
338325
func (rc *ResultCounter) SetSuccess() {
339326
rc.mu.Lock()
340327
rc.FilesImported++

0 commit comments

Comments
 (0)