Skip to content

Commit

Permalink
Allow overriding camera name via CLI (#93)
Browse files Browse the repository at this point in the history
* Allow overriding camera name via CLI

* will be used in the future

* Revert back to fallback if location isnt' correct
  • Loading branch information
KonradIT authored Jan 19, 2023
1 parent cf54421 commit c38f919
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 67 deletions.
14 changes: 8 additions & 6 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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("")
}
2 changes: 1 addition & 1 deletion pkg/android/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 7 additions & 42 deletions pkg/dji/dji.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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) {
Expand All @@ -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)

Expand Down Expand Up @@ -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...)
Expand Down
9 changes: 6 additions & 3 deletions pkg/gopro/gopro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions pkg/insta360/insta360.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
13 changes: 0 additions & 13 deletions pkg/utils/cameras.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
Expand Down

0 comments on commit c38f919

Please sign in to comment.