-
Notifications
You must be signed in to change notification settings - Fork 45
/
exiftool_sample_test.go
79 lines (63 loc) · 1.47 KB
/
exiftool_sample_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package exiftool_test
import (
"fmt"
"io"
"os"
"io/ioutil"
"path/filepath"
"github.com/barasher/go-exiftool"
)
func ExampleExiftool_Read() {
et, err := exiftool.NewExiftool()
if err != nil {
fmt.Printf("Error when intializing: %v\n", err)
return
}
defer et.Close()
fileInfos := et.ExtractMetadata("testdata/20190404_131804.jpg")
for _, fileInfo := range fileInfos {
if fileInfo.Err != nil {
fmt.Printf("Error concerning %v: %v\n", fileInfo.File, fileInfo.Err)
continue
}
for k, v := range fileInfo.Fields {
fmt.Printf("[%v] %v\n", k, v)
}
}
}
func copyFile(src, dest string) (err error) {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
d, err := os.Create(dest)
if err != nil {
return err
}
defer d.Close()
_, err = io.Copy(d, s)
if err != nil {
return err
}
return nil
}
func ExampleExiftool_Write() {
// error handling are skipped in this example
tmpDir, _ := ioutil.TempDir("", "ExampleExiftoolWrite")
testFile := filepath.Join(tmpDir, "20190404_131804.jpg")
copyFile("testdata/20190404_131804.jpg", testFile)
e, _ := exiftool.NewExiftool()
defer e.Close()
originals := e.ExtractMetadata(testFile)
title, _ := originals[0].GetString("Title")
fmt.Println("title:" + title)
originals[0].SetString("Title", "newTitle")
e.WriteMetadata(originals)
altered := e.ExtractMetadata(testFile)
title, _ = altered[0].GetString("Title")
fmt.Println("title:" + title)
// Output:
// title:
// title:newTitle
}