Java를 사용하여 프레젠테이션에서 비디오 프레임 관리
소개
프레젠테이션에 적절히 배치된 비디오는 메시지를 더욱 설득력 있게 만들고 청중과의 참여도를 높일 수 있습니다.
PowerPoint에서는 프레젠테이션의 슬라이드에 비디오를 두 가지 방법으로 추가할 수 있습니다:
- 로컬 비디오 추가 또는 삽입(컴퓨터에 저장된 비디오)
- 온라인 비디오 추가(YouTube와 같은 웹 소스)
프레젠테이션에 비디오(비디오 개체)를 추가할 수 있도록 Aspose.Slides는 IVideo 인터페이스, IVideoFrame 인터페이스 및 기타 관련 타입을 제공합니다.
임베드된 비디오 프레임 만들기
슬라이드에 추가하려는 비디오 파일이 로컬에 저장돼 있는 경우, 비디오 프레임을 만들어 프레젠테이션에 비디오를 삽입할 수 있습니다.
- Presentation class의 인스턴스를 생성합니다.
- 인덱스를 통해 슬라이드 참조를 가져옵니다.
- IVideo 개체를 추가하고 비디오 파일 경로를 전달해 프레젠테이션에 비디오를 삽입합니다.
- IVideoFrame 개체를 추가해 비디오 프레임을 생성합니다.
- 수정된 프레젠테이션을 저장합니다.
다음 Java 코드는 로컬에 저장된 비디오를 프레젠테이션에 추가하는 방법을 보여 줍니다:
// Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation("pres.pptx");
try {
// 비디오를 로드합니다
FileInputStream fileStream = new FileInputStream("Wildlife.mp4");
IVideo video = pres.getVideos().addVideo(fileStream, LoadingStreamBehavior.KeepLocked);
// 첫 번째 슬라이드를 가져와 비디오 프레임을 추가합니다
pres.getSlides().get_Item(0).getShapes().addVideoFrame(10, 10, 150, 250, video);
// 프레젠테이션을 디스크에 저장합니다
pres.save("pres-with-video.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}
또는 addVideoFrame(float x, float y, float width, float height, IVideo video) 메서드에 파일 경로를 직접 전달해 비디오를 추가할 수 있습니다:
Presentation pres = new Presentation();
try {
ISlide sld = pres.getSlides().get_Item(0);
IVideoFrame vf = sld.getShapes().addVideoFrame(50, 150, 300, 150, "video1.avi");
} finally {
if (pres != null) pres.dispose();
}
웹 소스 비디오를 사용한 비디오 프레임 만들기
Microsoft PowerPoint 2013 및 이후 버전에서는 YouTube 비디오를 프레젠테이션에 삽입할 수 있습니다. 온라인에 비디오가 존재한다면(예: YouTube) 웹 링크를 통해 프레젠테이션에 추가할 수 있습니다.
- Presentation class의 인스턴스를 생성합니다.
- 인덱스를 통해 슬라이드 참조를 가져옵니다.
- IVideo 개체를 추가하고 비디오 링크를 전달합니다.
- 비디오 프레임의 썸네일을 설정합니다.
- 프레젠테이션을 저장합니다.
다음 Java 코드는 웹에서 비디오를 가져와 PowerPoint 슬라이드에 추가하는 방법을 보여 줍니다:
// 프레젠테이션 파일을 나타내는 Presentation 객체를 인스턴스화합니다
Presentation pres = new Presentation();
try {
addVideoFromYouTube(pres, "Tj75Arhq5ho");
pres.save("out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
private static void addVideoFromYouTube(Presentation pres, String videoID)
{
// 비디오 프레임을 추가합니다
IVideoFrame videoFrame = pres.getSlides().get_Item(0).getShapes().addVideoFrame(
10, 10, 427, 240, "https://www.youtube.com/embed/" + videoID);
videoFrame.setPlayMode(VideoPlayModePreset.Auto);
// 썸네일을 로드합니다
String thumbnailUri = "http://img.youtube.com/vi/" + videoID + "/hqdefault.jpg";
URL url;
try {
url = new URL(thumbnailUri);
videoFrame.getPictureFormat().getPicture().setImage(pres.getImages().addImage(url.openStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
비디오 프레임 자르기
Aspose.Slides는 IVideoFrame.setTrimFromStart 및 IVideoFrame.setTrimFromEnd을 통해 시작점과 종료점에서 잘라낼 시간을 밀리초 단위로 지정함으로써 비디오 재생 구간을 제어할 수 있습니다. 이 설정은 프레젠테이션 내 비디오 재생 방식을 변경하지만, 임베드된 비디오 파일 자체를 잘라내거나 수정하지는 않습니다.
Trim 설정 지정
비디오 프레임을 만들고 Trim 설정을 지정하려면:
- Presentation class의 인스턴스를 생성합니다.
- 프레젠테이션에 IVideo 개체를 추가합니다.
- 슬라이드에 IVideoFrame 개체를 추가합니다.
- IVideoFrame.setTrimFromStart와 IVideoFrame.setTrimFromEnd을 사용해 시작·끝 Trim 값을 설정합니다.
- 수정된 프레젠테이션을 저장합니다.
다음 코드 예제는 임베드된 비디오 재생 시 앞쪽 2.5초와 뒤쪽 1초를 건너뛰도록 합니다:
Presentation presentation = new Presentation();
try {
FileInputStream videoStream = new FileInputStream("video.mp4");
try {
IVideo video = presentation.getVideos().addVideo(
videoStream, LoadingStreamBehavior.ReadStreamAndRelease);
ISlide slide = presentation.getSlides().get_Item(0);
IVideoFrame videoFrame = slide.getShapes().addVideoFrame(50, 50, 640, 360, video);
videoFrame.setTrimFromStart(2500f);
videoFrame.setTrimFromEnd(1000f);
presentation.save("video_with_trim.pptx", SaveFormat.Pptx);
} finally {
videoStream.close();
}
} finally {
presentation.dispose();
}
Trim 설정 읽기
기존 Trim 설정을 확인하려면 프레젠테이션을 로드하고, 첫 번째 슬라이드의 도형 중 IVideoFrame 개체를 찾아 IVideoFrame.getTrimFromStart와 IVideoFrame.getTrimFromEnd을 통해 값을 읽어봅니다.
다음 코드 예제는 첫 번째 슬라이드에서 첫 번째 비디오 프레임을 찾아 밀리초 단위의 Trim 설정을 출력합니다:
Presentation presentation = new Presentation("video_with_trim.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes()) {
if (shape instanceof IVideoFrame) {
IVideoFrame videoFrame = (IVideoFrame) shape;
float trimFromStart = videoFrame.getTrimFromStart();
float trimFromEnd = videoFrame.getTrimFromEnd();
System.out.println("Trim from start: " + trimFromStart + " ms");
System.out.println("Trim from end: " + trimFromEnd + " ms");
break;
}
}
} finally {
presentation.dispose();
}
비디오 캡션 관리
Aspose.Slides는 PowerPoint 프레젠테이션의 비디오 프레임에 대한 폐쇄 캡션을 관리할 수 있게 해 줍니다. 캡션은 WebVTT 형식으로 저장되며 IVideoFrame.getCaptionTracks 메서드를 통해 접근할 수 있습니다.
비디오 프레임에 캡션 추가
비디오 프레임에 캡션을 추가하려면:
- Presentation class의 인스턴스를 생성합니다.
- 프레젠테이션에 비디오를 추가합니다.
- 슬라이드에 IVideoFrame 개체를 추가합니다.
- getCaptionTracks이 반환하는 ICaptionsCollection을 사용해 WebVTT 캡션 트랙을 추가합니다.
- 수정된 프레젠테이션을 저장합니다.
다음 코드는 비디오 프레임에 캡션을 추가하는 방법을 보여 줍니다:
Presentation presentation = new Presentation();
try {
byte[] videoData = Files.readAllBytes(Paths.get("video.mp4"));
IVideo video = presentation.getVideos().addVideo(videoData);
ISlide slide = presentation.getSlides().get_Item(0);
IVideoFrame videoFrame = slide.getShapes().addVideoFrame(0, 0, 100, 100, video);
// WebVTT 파일에서 새로운 캡션 트랙을 추가합니다.
videoFrame.getCaptionTracks().add("English", "track.vtt");
presentation.save("video_with_captions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
ICaptionsCollection 인터페이스는 스트림에서 캡션을 추가할 수 있는 오버로드도 제공합니다.
비디오 프레임에서 캡션 추출
비디오 프레임에서 캡션을 추출하려면:
- 비디오가 포함된 프레젠테이션을 로드합니다.
- 대상 IVideoFrame 개체를 찾습니다.
- ICaptionsCollection의 캡션 트랙을 순회합니다.
- 각 캡션 트랙을
.vtt파일로 저장합니다.
다음 코드는 비디오 프레임에서 캡션을 추출하는 방법을 보여 줍니다:
Presentation presentation = new Presentation("video_with_captions.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes()) {
if (shape instanceof IVideoFrame) {
IVideoFrame videoFrame = (IVideoFrame)shape;
for (ICaptions captionTrack : videoFrame.getCaptionTracks()) {
// 캡션 트랙을 WebVTT 파일에 저장합니다.
String filePath = captionTrack.getCaptionId().toString() + ".vtt";
Files.write(Paths.get(filePath), captionTrack.getBinaryData());
}
}
}
} finally {
presentation.dispose();
}
각 ICaptions 개체는 캡션 식별자, 레이블, 바이너리 데이터 및 UTF-8 문자열 형태의 캡션 텍스트를 제공한다.
비디오 프레임에서 캡션 제거
비디오 프레임에서 캡션을 제거하려면:
- 비디오가 포함된 프레젠테이션을 로드합니다.
- 대상 IVideoFrame 개체를 가져옵니다.
- ICaptionsCollection에서 캡션 트랙을 제거합니다.
- 수정된 프레젠테이션을 저장합니다.
다음 코드는 비디오 프레임의 모든 캡션을 제거하는 방법을 보여 줍니다:
Presentation presentation = new Presentation("video_with_captions.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IVideoFrame videoFrame = (IVideoFrame)slide.getShapes().get_Item(0);
// 비디오 프레임에서 모든 캡션을 제거합니다.
videoFrame.getCaptionTracks().clear();
presentation.save("video_without_captions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
하나의 캡션 트랙만 제거하려면 clear 대신 remove 또는 removeAt 메서드를 사용하세요.
슬라이드에서 비디오 추출
비디오를 슬라이드에 추가하는 것 외에도 Aspose.Slides를 사용하면 프레젠테이션에 임베드된 비디오를 추출할 수 있습니다.
- 비디오가 포함된 프레젠테이션을 로드하기 위해 Presentation class의 인스턴스를 생성합니다.
- 모든 ISlide 개체를 순회합니다.
- 모든 IShape 개체를 순회해 VideoFrame을 찾습니다.
- 비디오를 디스크에 저장합니다.
다음 Java 코드는 프레젠테이션 슬라이드에서 비디오를 추출하는 방법을 보여 줍니다:
// 프레젠테이션 파일을 나타내는 Presentation 객체를 인스턴스화합니다
Presentation pres = new Presentation("VideoSample.pptx");
try {
for (ISlide slide : pres.getSlides())
{
for (IShape shape : slide.getShapes())
{
if (shape instanceof VideoFrame)
{
IVideoFrame vf = (IVideoFrame) shape;
String type = vf.getEmbeddedVideo().getContentType();
int ss = type.lastIndexOf('-');
byte[] buffer = vf.getEmbeddedVideo().getBinaryData();
//파일 확장자를 가져옵니다
int charIndex = type.indexOf("/");
type = type.substring(charIndex + 1);
FileOutputStream fop = new FileOutputStream("testing2." + type);
fop.write(buffer);
fop.flush();
fop.close();
}
}
}
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}
FAQ
VideoFrame에 대해 변경할 수 있는 비디오 재생 매개변수는 무엇인가요?
VideoFrame 객체의 속성을 통해 재생 모드(자동 또는 클릭)와 루프 설정을 제어할 수 있습니다.
비디오를 추가하면 PPTX 파일 크기가 늘어나나요?
예. 로컬 비디오를 임베드하면 바이너리 데이터가 문서에 포함돼 파일 크기에 비례해 프레젠테이션 크기가 증가합니다. 온라인 비디오를 추가하면 링크와 썸네일만 임베드되므로 증가량이 훨씬 작습니다.
기존 VideoFrame의 위치와 크기는 유지하면서 비디오를 교체할 수 있나요?
예. 프레임 내부의 video content를 교체하면 도형의 기하학적 속성을 그대로 유지하면서 미디어를 업데이트할 수 있는 일반적인 시나리오입니다.
임베드된 비디오의 콘텐츠 유형(MIME)을 확인할 수 있나요?
예. 임베드된 비디오는 content type을 가지고 있으며, 이를 읽어 디스크에 저장할 때 활용할 수 있습니다.