{"id":6911,"date":"2025-05-31T14:15:29","date_gmt":"2025-06-05T06:56:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6911"},"modified":"2025-06-05T14:15:29","modified_gmt":"2025-06-05T06:56:29","slug":"https-badgameshow-com-steven-131","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-131\/","title":{"rendered":"Swift \u5f71\u7247\u526a\u8f2f\u5b8c\u5168\u653b\u7565\uff1a\u4f7f\u7528 AVAssetExportSession \ud83c\udfa5"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, AVAssetExportSession, \u5f71\u7247\u526a\u8f2f, Swift \u6559\u5b78\"><\/p>\n<h1>Swift \u5f71\u7247\u526a\u8f2f\u5b8c\u5168\u653b\u7565\uff1a\u4f7f\u7528 AVAssetExportSession \ud83c\udfa5<\/h1>\n<p>\u5728 2025 \u5e74\uff0cSwift \u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\uff0c\u5176\u4e2d\u5305\u62ec\u5f71\u7247\u526a\u8f2f\u529f\u80fd\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 <code>AVAssetExportSession<\/code> \u4f86\u9032\u884c\u5f71\u7247\u526a\u8f2f\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>AVAssetExportSession \u7c21\u4ecb<\/h2>\n<p><code>AVAssetExportSession<\/code> \u662f Swift \u63d0\u4f9b\u7684\u4e00\u500b API\uff0c\u5c08\u9580\u7528\u65bc\u5c07\u5f71\u7247\u8f38\u51fa\u70ba\u591a\u7a2e\u683c\u5f0f\uff0c\u5982 MP4\u3001MOV\u3001AVI \u7b49\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u6b64 API \u526a\u8f2f\u5f71\u7247\uff0c\u4e26\u6839\u64da\u9700\u6c42\u8abf\u6574\u5f71\u7247\u9577\u5ea6\u53ca\u683c\u5f0f\u3002<\/p>\n<h2>\u4f7f\u7528 AVAssetExportSession \u526a\u8f2f\u5f71\u7247<\/h2>\n<p>\u4f7f\u7528 <code>AVAssetExportSession<\/code> \u9032\u884c\u5f71\u7247\u526a\u8f2f\u7684\u904e\u7a0b\u5982\u4e0b\uff1a<\/p>\n<h3>1. \u5275\u5efa AVAssetExportSession \u7269\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5275\u5efa\u4e00\u500b <code>AVAssetExportSession<\/code> \u7269\u4ef6\uff0c\u5c07\u5f71\u7247\u8cc7\u6e90\u50b3\u905e\u7d66\u5b83\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let assetExportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)<\/code><\/pre>\n<h3>2. \u6307\u5b9a\u8f38\u51fa\u683c\u5f0f<\/h3>\n<p>\u63a5\u8457\uff0c\u8a2d\u7f6e\u8f38\u51fa\u7684\u5f71\u7247\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">assetExportSession.outputFileType = AVFileType.mp4<\/code><\/pre>\n<h3>3. \u8a2d\u5b9a\u8f38\u51fa\u6642\u9593\u7bc4\u570d<\/h3>\n<p>\u7136\u5f8c\uff0c\u6307\u5b9a\u5f71\u7247\u7684\u8f38\u51fa\u6642\u9593\u7bc4\u570d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">assetExportSession.timeRange = CMTimeRange(start: startTime, duration: duration)<\/code><\/pre>\n<h3>4. \u57f7\u884c\u5f71\u7247\u8f38\u51fa<\/h3>\n<p>\u6700\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u5c07\u5f71\u7247\u8f38\u51fa\u70ba\u6307\u5b9a\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">assetExportSession.exportAsynchronously {\n    switch assetExportSession.status {\n    case .completed:\n        print(\"\u5f71\u7247\u8f38\u51fa\u6210\u529f\")\n    case .failed:\n        print(\"\u5f71\u7247\u8f38\u51fa\u5931\u6557\uff0c\u932f\u8aa4: \\(String(describing: assetExportSession.error))\")\n    case .cancelled:\n        print(\"\u5f71\u7247\u8f38\u51fa\u88ab\u53d6\u6d88\")\n    default:\n        break\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 <code>AVAssetExportSession<\/code> \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u5f71\u7247\u683c\u5f0f\u4e0d\u652f\u63f4\uff1a<\/strong>\u78ba\u4fdd\u8f38\u51fa\u7684\u5f71\u7247\u683c\u5f0f\u5728\u60a8\u7684\u8a2d\u5099\u4e0a\u662f\u652f\u63f4\u7684\u3002<\/li>\n<li><strong>\u5c0e\u51fa\u5931\u6557\uff1a<\/strong>\u8acb\u6aa2\u67e5\u8f38\u51fa\u8def\u5f91\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u6709\u8db3\u5920\u7684\u5b58\u5132\u7a7a\u9593\u3002<\/li>\n<li><strong>\u6642\u9593\u7bc4\u570d\u8a2d\u7f6e\u932f\u8aa4\uff1a<\/strong>\u78ba\u4fdd\u8a2d\u7f6e\u7684 <code>startTime<\/code> \u548c <code>duration<\/code> \u5408\u7406\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5f71\u7247\u526a\u8f2f\u529f\u80fd\uff0c<code>AVAssetExportSession<\/code> \u9084\u53ef\u4ee5\u8207\u5176\u4ed6 AVFoundation \u7d44\u4ef6\u7d50\u5408\u4f7f\u7528\uff0c\u5982\u6dfb\u52a0\u97f3\u6a02\u3001\u5b57\u5e55\u6216\u7279\u6548\uff0c\u8b93\u5f71\u7247\u66f4\u52a0\u751f\u52d5\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>AVAssetExportSession \u662f\u4e00\u500b\u5f37\u5927\u7684 Swift API\uff0c\u7c21\u5316\u4e86\u5f71\u7247\u526a\u8f2f\u7684\u904e\u7a0b\u3002\u53ea\u9700\u5e7e\u884c\u7a0b\u5f0f\u78bc\uff0c\u4fbf\u53ef\u8f15\u9b06\u5b8c\u6210\u5f71\u7247\u7684\u526a\u8f2f\u8207\u8f38\u51fa\uff0c\u662f\u6bcf\u4f4d Swift \u958b\u767c\u8005\u5fc5\u5099\u7684\u5de5\u5177\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5f71\u7247\u526a\u8f2f\uff1aAVAssetExportSession-\ud83c\udfa5_1675398293.20259.webp\" alt=\"Swift \u5f71\u7247\u526a\u8f2f\uff1aAVAssetExportSession \ud83c\udfa5\" title=\"Swift \u5f71\u7247\u526a\u8f2f\u3001AVAssetExportSession\u3001\u5f71\u7247\u526a\u8f2f\u7a0b\u5f0f\u78bc\u3001Swift \u5f71\u7247\u7de8\u8f2f\u3001Swift \u5f71\u7247\u526a\u8f2f\u6559\u5b78\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. AVAssetExportSession \u652f\u63f4\u54ea\u4e9b\u5f71\u7247\u683c\u5f0f\uff1f<\/h3>\n<p>AVAssetExportSession \u652f\u63f4\u591a\u7a2e\u683c\u5f0f\uff0c\u5305\u62ec MP4\u3001MOV \u548c AVI \u7b49\u7b49\uff0c\u5177\u9ad4\u53d6\u6c7a\u65bc\u6240\u9078\u7684 <code>outputFileType<\/code>\u3002<\/p>\n<h3>2. \u4f7f\u7528 AVAssetExportSession \u6642\u9047\u5230\u932f\u8aa4\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>\u6aa2\u67e5\u60a8\u7684\u8f38\u51fa\u8def\u5f91\u3001\u5f71\u7247\u683c\u5f0f\u53ca\u6642\u9593\u7bc4\u570d\u8a2d\u7f6e\u3002\u5982\u679c\u4ecd\u6709\u554f\u984c\uff0c\u5efa\u8b70\u67e5\u770b\u932f\u8aa4\u4fe1\u606f\u9032\u884c\u6392\u67e5\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u5728\u8f38\u51fa\u5f71\u7247\u4e2d\u6dfb\u52a0\u97f3\u6548\u6216\u5b57\u5e55\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 AVFoundation \u7684\u5176\u4ed6\u7d44\u4ef6\uff0c\u4f8b\u5982 <code>AVMutableComposition<\/code>\uff0c\u5728\u5f71\u7247\u4e2d\u6dfb\u52a0\u97f3\u6548\u548c\u5b57\u5e55\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684AVAssetExportSession\u4f86\u526a\u8f2f\u5f71\u7247\uff0c\u4e26\u63d0\u4f9b\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u8b93\u5b78\u7fd2\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n","protected":false},"author":1,"featured_media":6909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5f71\u7247\u526a\u8f2f\uff1aAVAssetExportSession-\ud83c\udfa5_1675398293.20259.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Nt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6911"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6911\/revisions"}],"predecessor-version":[{"id":13589,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6911\/revisions\/13589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6909"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}