Repository navigation
Expand file tree
/
Copy pathUploadLargeVideo.java
More file actions
96 lines (85 loc) · 3.96 KB
/
Copy pathUploadLargeVideo.java
File metadata and controls
96 lines (85 loc) · 3.96 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Upload a video with the chunked uploader and print its streaming-ready URLs.
*
* Prerequisites
* - CLOUDINARY_URL set in the environment.
* - cloudinary-http5 on the classpath.
* - A local video file. This example downloads one from the Cloudinary demo cloud if
* SOURCE_FILE does not exist, so it is runnable with no setup.
*
* Run
* javac -cp "<classpath>" -d out examples/UploadLargeVideo.java
* java -cp "<classpath>:out" UploadLargeVideo
*
* How this differs in a real project
* A real application would stream a user upload to a temporary file, pass
* notification_url instead of blocking on the response, and delete the temp file in a
* finally block.
*
* Docs: docs/upload-large-video.md
*/
import com.cloudinary.Cloudinary;
import com.cloudinary.Transformation;
import com.cloudinary.utils.ObjectUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Map;
public class UploadLargeVideo {
private static final String SOURCE_URL =
"https://res.cloudinary.com/demo/video/upload/dog.mp4";
private static final String SOURCE_FILE = "example-video.mp4";
private static final String PUBLIC_ID = "examples/upload_large_video";
public static void main(String[] args) {
try {
Cloudinary cloudinary = new Cloudinary();
if (cloudinary.config.cloudName == null) {
System.err.println("No Cloudinary configuration found. Set CLOUDINARY_URL.");
System.exit(1);
}
File video = new File(SOURCE_FILE);
if (!video.exists()) {
System.out.println("Downloading a sample video to " + SOURCE_FILE + " ...");
try (InputStream in = new URL(SOURCE_URL).openStream()) {
Files.copy(in, video.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
// resource_type is REQUIRED here. uploadLarge defaults to "image" and fails
// with "Invalid image file" on a video without it.
Map result = cloudinary.uploader().uploadLarge(video, ObjectUtils.asMap(
"public_id", PUBLIC_ID,
"resource_type", "video",
"overwrite", true,
"chunk_size", 20_000_000));
System.out.println("public_id: " + result.get("public_id"));
System.out.println("asset_id: " + result.get("asset_id"));
System.out.println("resource_type: " + result.get("resource_type"));
System.out.println("format: " + result.get("format"));
System.out.println("duration: " + result.get("duration") + "s");
System.out.println("bytes: " + result.get("bytes"));
System.out.println("secure_url: " + result.get("secure_url"));
String scaled = cloudinary.url().resourceType("video")
.transformation(new Transformation()
.width(640).crop("scale").quality("auto"))
.generate(PUBLIC_ID + "." + result.get("format"));
System.out.println("scaled: " + scaled);
String poster = cloudinary.url().resourceType("video").format("jpg")
.transformation(new Transformation()
.width(640).crop("fill").startOffset("2"))
.generate(PUBLIC_ID);
System.out.println("poster: " + poster);
} catch (IllegalArgumentException e) {
System.err.println("Configuration error: " + e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
System.exit(1);
} catch (RuntimeException e) {
System.err.println("Upload failed: " + e.getMessage());
System.exit(1);
}
}
}