iOS Swift/애플 문서 한글화
Exporting video to alternative formats
basker
2023. 6. 7. 19:16
Article
비디오를 다른 포맷으로 내보내기
기존동영상 파일을 다른 형식으로 변환
Overview
기존 동영상 파일을 다른 장치와 호환되는 형식으로 변환하려면 기존 파일의 내용을 기반으로 새 동영상 파일을 생성해야 합니다. 저장된 비디오의 형식을 변경할 수는 없지만 이 문서에 설명된 단계에 따라 원하는 형식으로 새 파일을 만들 수 있습니다.
Tip 앱이 비디오 캡처에서 직접 동영상을 저장할 수 있는 경우 Recording movies in alternative formats를 참조하여 기본 포맷을 수정하는 게 더 효율적입니다. |
요청된 포맷으로 새 비디오 내보내기
비디오를 다른 형식으로 내보내려면 AVAset 동영상 파일로 다음 단계의 작업을 수행해야 합니다.
- Export Presets 리스트에서 내보낼 프리셋을 선택합니다.
- AVFileType 프리셋 설정 목록에서 내보낼 파일 타입을 선택합니다.
- AVAssetExportSession이 동영상을 입력 형식에서 원하는 출력 형식으로 변환할 수 있는지 확인합니다.
- AVAssetExportSession 인스턴스를 만들고 구성한 다음 이 인스턴스를 사용하여 내보내기를 수행합니다.
다음 예제에서는 이를 수행하는 메서드를 정의하고 위의 과정을 수행합니다.
func export(video: AVAsset,
withPreset preset: String = AVAssetExportPresetHighestQuality,
toFileType outputFileType: AVFileType = .mov,
atURL outputURL: URL) async {
// Check the compatibility of the preset to export the video to the output file type.
guard await AVAssetExportSession.compatibility(ofExportPreset: preset,
with: video,
outputFileType: outputFileType) else {
print("The preset can't export the video to the output file type.")
return
}
// Create and configure the export session.
guard let exportSession = AVAssetExportSession(asset: video,
presetName: preset) else {
print("Failed to create export session.")
return
}
exportSession.outputFileType = outputFileType
exportSession.outputURL = outputURL
// Convert the video to the output file type and export it to the output URL.
await exportSession.export()
}
let video = // Your source AVAsset video. //
let outputURL = // The destination URL for your exported video. //
// Use a preset that encodes to H.264 to convert a video to the .mov file type,
// and asynchronously perform the export.
Task {
await export(video: video,
withPreset: AVAssetExportPresetHighestQuality,
toFileType: .mov,
atURL: outputURL)
}