|
@@ -45,9 +45,22 @@ public class HuaweiObsServiceImpl implements IHuaweiObsService {
|
|
|
public static final String REGION_NORTH1 = "cn-north-1";
|
|
public static final String REGION_NORTH1 = "cn-north-1";
|
|
|
public static final String REGION_EAST2 = "cn-east-2";
|
|
public static final String REGION_EAST2 = "cn-east-2";
|
|
|
|
|
|
|
|
- // ak/sk
|
|
|
|
|
- private static final String AK = "K2UTJGIN7UTZJR2XMXYG";
|
|
|
|
|
- private static final String SK = "sbyeNJLbcYmH6copxeFP9pAoksM4NIT9Zw4x0SRX";
|
|
|
|
|
|
|
+ // ak/sk — 通过环境变量 HW_OBS_AK / HW_OBS_SK 配置,禁止硬编码
|
|
|
|
|
+ private static String obsAk() {
|
|
|
|
|
+ String v = System.getenv("HW_OBS_AK");
|
|
|
|
|
+ if (v == null || v.isEmpty()) {
|
|
|
|
|
+ throw new IllegalStateException("请配置环境变量 HW_OBS_AK");
|
|
|
|
|
+ }
|
|
|
|
|
+ return v;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String obsSk() {
|
|
|
|
|
+ String v = System.getenv("HW_OBS_SK");
|
|
|
|
|
+ if (v == null || v.isEmpty()) {
|
|
|
|
|
+ throw new IllegalStateException("请配置环境变量 HW_OBS_SK");
|
|
|
|
|
+ }
|
|
|
|
|
+ return v;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String FIXED_VIDEO_PATH = "C:\\fs\\mergeVideo\\first.mp4";
|
|
private static final String FIXED_VIDEO_PATH = "C:\\fs\\mergeVideo\\first.mp4";
|
|
@@ -68,13 +81,17 @@ public class HuaweiObsServiceImpl implements IHuaweiObsService {
|
|
|
|
|
|
|
|
private void mergeVideos(String fixedVideoPath, String uploadedVideoPath, String outputPath) throws IOException, InterruptedException {
|
|
private void mergeVideos(String fixedVideoPath, String uploadedVideoPath, String outputPath) throws IOException, InterruptedException {
|
|
|
|
|
|
|
|
- String command = String.format("ffmpeg -i %s -i %s -filter_complex \"[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]\" -map \"[outv]\" -map \"[outa]\" %s", fixedVideoPath,uploadedVideoPath, outputPath);
|
|
|
|
|
-
|
|
|
|
|
- // 打印调试信息
|
|
|
|
|
- System.out.println("Executing command: " + command);
|
|
|
|
|
-
|
|
|
|
|
- // 执行命令
|
|
|
|
|
- Process process = Runtime.getRuntime().exec(command);
|
|
|
|
|
|
|
+ ProcessBuilder pb = new ProcessBuilder(
|
|
|
|
|
+ "ffmpeg",
|
|
|
|
|
+ "-i", fixedVideoPath,
|
|
|
|
|
+ "-i", uploadedVideoPath,
|
|
|
|
|
+ "-filter_complex", "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]",
|
|
|
|
|
+ "-map", "[outv]",
|
|
|
|
|
+ "-map", "[outa]",
|
|
|
|
|
+ outputPath
|
|
|
|
|
+ );
|
|
|
|
|
+ System.out.println("Executing command: " + String.join(" ", pb.command()));
|
|
|
|
|
+ Process process = pb.start();
|
|
|
|
|
|
|
|
// 等待命令执行完成
|
|
// 等待命令执行完成
|
|
|
int exitCode = process.waitFor();
|
|
int exitCode = process.waitFor();
|
|
@@ -116,7 +133,7 @@ public class HuaweiObsServiceImpl implements IHuaweiObsService {
|
|
|
|
|
|
|
|
// 创建ObsClient实例
|
|
// 创建ObsClient实例
|
|
|
// 使用永久AK/SK初始化客户端
|
|
// 使用永久AK/SK初始化客户端
|
|
|
- ObsClient obsClient = new ObsClient(AK, SK,endPoint);
|
|
|
|
|
|
|
+ ObsClient obsClient = new ObsClient(obsAk(), obsSk(), endPoint);
|
|
|
// 使用临时AK/SK和SecurityToken初始化客户端
|
|
// 使用临时AK/SK和SecurityToken初始化客户端
|
|
|
// ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);
|
|
// ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);
|
|
|
String objKey = generateUploadFileName(file.getName());
|
|
String objKey = generateUploadFileName(file.getName());
|
|
@@ -192,7 +209,7 @@ public class HuaweiObsServiceImpl implements IHuaweiObsService {
|
|
|
for (FsUserCourseVideo video : videos) {
|
|
for (FsUserCourseVideo video : videos) {
|
|
|
String endPoint = "https://obs.cn-north-4.myhuaweicloud.com";
|
|
String endPoint = "https://obs.cn-north-4.myhuaweicloud.com";
|
|
|
|
|
|
|
|
- ObsClient obsClient = new ObsClient(AK, SK, endPoint);
|
|
|
|
|
|
|
+ ObsClient obsClient = new ObsClient(obsAk(), obsSk(), endPoint);
|
|
|
String objKey = extractPathByRegex(video.getLineOne());
|
|
String objKey = extractPathByRegex(video.getLineOne());
|
|
|
String url = "https://obs.ylrztop.com/" + objKey;
|
|
String url = "https://obs.ylrztop.com/" + objKey;
|
|
|
try {
|
|
try {
|
|
@@ -224,33 +241,45 @@ public class HuaweiObsServiceImpl implements IHuaweiObsService {
|
|
|
return urlString.replaceAll("^https?://[^/]+/", "");
|
|
return urlString.replaceAll("^https?://[^/]+/", "");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static final String YL_SECRET_ID = "AKIDiMq9lDf2EOM9lIfqqfKo7FNgM5meD0sT";
|
|
|
|
|
- private static final String YL_SECRET_KEY = "u5SuS80342xzx8FRBukza9lVNHKNMSaB";
|
|
|
|
|
private static final String YL_BUCKET = "hylj-1323137866";
|
|
private static final String YL_BUCKET = "hylj-1323137866";
|
|
|
- private static final String YL_APP_ID = "1323137866";
|
|
|
|
|
private static final String YL_REGION = "ap-chongqing";
|
|
private static final String YL_REGION = "ap-chongqing";
|
|
|
- private static final String YL_PROXY = "hylj";
|
|
|
|
|
|
|
|
|
|
- private static COSClient cosClient = createClient();
|
|
|
|
|
|
|
+ private static volatile COSClient cosClient;
|
|
|
|
|
|
|
|
private static COSClient createClient() {
|
|
private static COSClient createClient() {
|
|
|
- // 初始化用户身份信息(secretId, secretKey)
|
|
|
|
|
- COSCredentials cred = new BasicCOSCredentials(YL_SECRET_ID,YL_SECRET_KEY);
|
|
|
|
|
- // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
|
|
|
|
|
|
|
+ String secretId = System.getenv("COS_SECRET_ID");
|
|
|
|
|
+ String secretKey = System.getenv("COS_SECRET_KEY");
|
|
|
|
|
+ if (secretId == null || secretId.isEmpty()) {
|
|
|
|
|
+ secretId = System.getenv("TENCENT_CLOUD_SECRET_ID");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (secretKey == null || secretKey.isEmpty()) {
|
|
|
|
|
+ secretKey = System.getenv("TENCENT_CLOUD_SECRET_KEY");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (secretId == null || secretId.isEmpty() || secretKey == null || secretKey.isEmpty()) {
|
|
|
|
|
+ throw new IllegalStateException("请配置环境变量 COS_SECRET_ID/COS_SECRET_KEY 或 TENCENT_CLOUD_SECRET_ID/TENCENT_CLOUD_SECRET_KEY");
|
|
|
|
|
+ }
|
|
|
|
|
+ COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
|
|
ClientConfig clientConfig = new ClientConfig(new Region(YL_REGION));
|
|
ClientConfig clientConfig = new ClientConfig(new Region(YL_REGION));
|
|
|
- // 生成cos客户端
|
|
|
|
|
- COSClient cosclient = new COSClient(cred, clientConfig);
|
|
|
|
|
|
|
+ return new COSClient(cred, clientConfig);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return cosclient;
|
|
|
|
|
|
|
+ private static COSClient cosClient() {
|
|
|
|
|
+ if (cosClient == null) {
|
|
|
|
|
+ synchronized (HuaweiObsServiceImpl.class) {
|
|
|
|
|
+ if (cosClient == null) {
|
|
|
|
|
+ cosClient = createClient();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return cosClient;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static byte[] getObjectInputStream(String key) throws IOException {
|
|
private static byte[] getObjectInputStream(String key) throws IOException {
|
|
|
-// String key = "course/202517/1736224647237.mp4";
|
|
|
|
|
GetObjectRequest getObjectRequest = new GetObjectRequest(YL_BUCKET, key);
|
|
GetObjectRequest getObjectRequest = new GetObjectRequest(YL_BUCKET, key);
|
|
|
InputStream cosObjectInput = null;
|
|
InputStream cosObjectInput = null;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- COSObject cosObject = cosClient.getObject(getObjectRequest);
|
|
|
|
|
|
|
+ COSObject cosObject = cosClient().getObject(getObjectRequest);
|
|
|
cosObjectInput = cosObject.getObjectContent();
|
|
cosObjectInput = cosObject.getObjectContent();
|
|
|
} catch (CosServiceException e) {
|
|
} catch (CosServiceException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -258,16 +287,15 @@ public class HuaweiObsServiceImpl implements IHuaweiObsService {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 处理下载到的流
|
|
|
|
|
- // 这里是直接读取,按实际情况来处理
|
|
|
|
|
byte[] bytes = null;
|
|
byte[] bytes = null;
|
|
|
try {
|
|
try {
|
|
|
bytes = IOUtils.toByteArray(cosObjectInput);
|
|
bytes = IOUtils.toByteArray(cosObjectInput);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
} finally {
|
|
|
- // 用完流之后一定要调用 close()
|
|
|
|
|
- cosObjectInput.close();
|
|
|
|
|
|
|
+ if (cosObjectInput != null) {
|
|
|
|
|
+ cosObjectInput.close();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return bytes;
|
|
return bytes;
|
|
|
}
|
|
}
|