From c38f919e62c61692d64cbc95a780eb0b9ae60acb Mon Sep 17 00:00:00 2001 From: Konrad Iturbe Date: Thu, 19 Jan 2023 18:59:54 +0000 Subject: [PATCH] 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 --- cmd/import.go | 14 +++++++----- pkg/android/android.go | 2 +- pkg/dji/dji.go | 49 ++++++---------------------------------- pkg/gopro/gopro.go | 9 +++++--- pkg/insta360/insta360.go | 7 ++++-- pkg/utils/cameras.go | 13 ----------- 6 files changed, 27 insertions(+), 67 deletions(-) diff --git a/cmd/import.go b/cmd/import.go index 4618bd9..c86af86 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -41,6 +41,7 @@ var importCmd = &cobra.Command{ bufferSize := getFlagInt(cmd, "buffer", "1000") prefix := getFlagString(cmd, "prefix") dateRange := getFlagSlice(cmd, "range") + cameraName := getFlagString(cmd, "camera_name") customCameraOpts := make(map[string]interface{}) @@ -87,7 +88,7 @@ var importCmd = &cobra.Command{ } customCameraOpts["tag_names"] = getFlagSlice(cmd, "tag_names") } - r, err := importFromCamera(c, input, filepath.Join(output, projectName), dateFormat, bufferSize, prefix, dateRange, customCameraOpts) + r, err := importFromCamera(c, input, filepath.Join(output, projectName), dateFormat, bufferSize, prefix, dateRange, cameraName, customCameraOpts) if err != nil { cui.Error("Something went wrong", err) } @@ -130,22 +131,23 @@ func init() { importCmd.Flags().StringSlice("sort_by", []string{}, "Sort files by: `camera`, `location`") 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") importCmd.Flags().StringP("skip_aux", "s", "true", "Skip auxiliary files (GoPro: THM, LRV. DJI: SRT)") + importCmd.Flags().String("camera_name", "", "Override camera name detection with specified string") // Camera helpers importCmd.Flags().Bool("use_gopro", false, "Detect GoPro camera attached") importCmd.Flags().Bool("use_insta360", false, "Detect Insta360 camera attached") } -func importFromCamera(c utils.Camera, input string, output string, dateFormat string, bufferSize int, prefix string, dateRange []string, camOpts map[string]interface{}) (*utils.Result, error) { +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) { switch c { case utils.GoPro: - return gopro.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts) + return gopro.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts) case utils.DJI: - return dji.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts) + return dji.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts) case utils.Insta360: - return insta360.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts) + return insta360.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts) case utils.Android: - return android.Import(input, output, dateFormat, bufferSize, prefix, dateRange, camOpts) + return android.Import(input, output, dateFormat, bufferSize, prefix, dateRange, cameraName, camOpts) } return nil, mErrors.ErrUnsupportedCamera("") } diff --git a/pkg/android/android.go b/pkg/android/android.go index 21a4b83..91b2d9b 100644 --- a/pkg/android/android.go +++ b/pkg/android/android.go @@ -59,7 +59,7 @@ func prepare(out string, deviceFileName string, deviceModel string, mediaDate st } return bar, dayFolder, nil } -func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) { +func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) { var result utils.Result sortOptions := utils.ParseCliOptions(cameraOptions) diff --git a/pkg/dji/dji.go b/pkg/dji/dji.go index 201658b..c1c09d5 100644 --- a/pkg/dji/dji.go +++ b/pkg/dji/dji.go @@ -20,7 +20,7 @@ import ( "gopkg.in/djherbis/times.v1" ) -func getDeviceNameFromPhoto(path string) (string, error) { +func getDeviceNameFromPhoto(path string) (string, error) { //nolint:unused f, err := os.Open(path) if err != nil { return "", err @@ -44,10 +44,13 @@ func getDeviceNameFromPhoto(path string) (string, error) { var locationService = LocationService{} -func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) { +func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) { // Tested on Mavic Air 2. Osmo Pocket v1 and Spark specific changes to follow. sortOptions := utils.ParseCliOptions(cameraOptions) + if cameraName == "" { + cameraName = "DJI Device" + } di, err := disk.GetInfo(in) if err != nil { return nil, err @@ -76,9 +79,7 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange mpb.WithWidth(60), mpb.WithRefreshRate(180*time.Millisecond)) - inlineCounter := utils.ResultCounter{ - CameraName: "DJI Device", - } + inlineCounter := utils.ResultCounter{} for _, f := range folders { r := mediaFolderRegex.MatchString(f.Name()) @@ -151,7 +152,7 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange wg.Add(1) bar := utils.GetNewBar(progressBar, int64(info.Size()), de.Name(), utils.IoTX) - dayFolder := utils.GetOrder(sortOptions, locationService, osPathname, out, mediaDate, "DJI Device") + dayFolder := utils.GetOrder(sortOptions, locationService, osPathname, out, mediaDate, cameraName) switch ftype.Type { case Photo: 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 inlineCounter.SetFailure(err, filename) } else { inlineCounter.SetSuccess() - - // Get Device Name - - devName, err := getDeviceNameFromPhoto(osPathname) - if err != nil { - inlineCounter.SetFailure(err, filename) - return - } - // Rename directory - matchDeviceName, is := DeviceNames[devName] - if is { - devName = matchDeviceName - } - - if err != nil { - inlineCounter.SetFailure(err, filename) - return - } - inlineCounter.SetCameraName(devName) } }(f.Name(), de.Name(), osPathname, bar) @@ -266,23 +248,6 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange wg.Wait() progressBar.Shutdown() - // Rename each folder - - _ = godirwalk.Walk(out, &godirwalk.Options{ - Unsorted: true, - Callback: func(osPathname string, de *godirwalk.Dirent) error { - if !de.ModeType().IsDir() { - return godirwalk.SkipThis - } - - modified, err := utils.FindFolderInPath(osPathname, "DJI Device") - if err == nil { - _ = 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. - } - return nil - }, - }) - result.Errors = append(result.Errors, inlineCounter.Get().Errors...) result.FilesImported += inlineCounter.Get().FilesImported result.FilesNotImported = append(result.FilesNotImported, inlineCounter.Get().FilesNotImported...) diff --git a/pkg/gopro/gopro.go b/pkg/gopro/gopro.go index c208b0d..32088ea 100644 --- a/pkg/gopro/gopro.go +++ b/pkg/gopro/gopro.go @@ -54,7 +54,7 @@ func getRfpsFolder(pathName string) (string, error) { fpsAsFloat := strconv.Itoa(framerate.(int)) return fmt.Sprintf("%dx%d %s", s.Streams[0].Width, s.Streams[0].Height, fpsAsFloat), nil } -func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) { +func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) { /* Import method using SD card bay or SD card reader */ 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 root := strings.Split(gpVersion.FirmwareVersion, ".")[0] + if cameraName == "" { + cameraName = gpVersion.CameraType + } switch root { case "HD6", "HD7", "HD8", "H19", "HD9", "H21", "H22": - result := importFromGoProV2(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, gpVersion.CameraType) + result := importFromGoProV2(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, cameraName) return &result, nil case "HD2", "HD3", "HD4", "HX", "HD5": - result := importFromGoProV1(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, gpVersion.CameraType) + result := importFromGoProV1(filepath.Join(in, fmt.Sprint(DCIM)), out, sortOptions, cameraName) return &result, nil default: return nil, mErrors.ErrUnsupportedCamera(gpVersion.CameraType) diff --git a/pkg/insta360/insta360.go b/pkg/insta360/insta360.go index 1be31c7..c3397bd 100644 --- a/pkg/insta360/insta360.go +++ b/pkg/insta360/insta360.go @@ -40,9 +40,12 @@ func getDeviceName(manifest string) string { } return fmt.Sprintf("Insta360%s", modelName[0]) } -func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraOptions map[string]interface{}) (*utils.Result, error) { +func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange []string, cameraName string, cameraOptions map[string]interface{}) (*utils.Result, error) { sortOptions := utils.ParseCliOptions(cameraOptions) + if cameraName == "" { + cameraName = getDeviceName(filepath.Join(in, "DCIM", "fileinfo_list.list")) + } di, err := disk.GetInfo(in) if err != nil { return nil, err @@ -146,7 +149,7 @@ func Import(in, out, dateFormat string, bufferSize int, prefix string, dateRange wg.Add(1) bar := utils.GetNewBar(progressBar, int64(info.Size()), de.Name(), utils.IoTX) - dayFolder := utils.GetOrder(sortOptions, nil, osPathname, out, mediaDate, getDeviceName(filepath.Join(in, "DCIM", "fileinfo_list.list"))) + dayFolder := utils.GetOrder(sortOptions, nil, osPathname, out, mediaDate, cameraName) x := de.Name() diff --git a/pkg/utils/cameras.go b/pkg/utils/cameras.go index 504e31f..9e47e9a 100644 --- a/pkg/utils/cameras.go +++ b/pkg/utils/cameras.go @@ -310,7 +310,6 @@ func FindFolderInPath(entirePath, directory string) (string, error) { type ResultCounter struct { mu sync.Mutex - CameraName string Errors []error FilesNotImported []string FilesImported int @@ -323,18 +322,6 @@ func (rc *ResultCounter) SetFailure(err error, file string) { rc.mu.Unlock() } -func (rc *ResultCounter) SetCameraName(camName string) { - rc.mu.Lock() - rc.CameraName = camName - rc.mu.Unlock() -} - -func (rc *ResultCounter) GetCameraName() string { - rc.mu.Lock() - defer rc.mu.Unlock() - return rc.CameraName -} - func (rc *ResultCounter) SetSuccess() { rc.mu.Lock() rc.FilesImported++