目前我的所有烧瓶路线都在一个文件中:
let composition = AVMutableComposition()
let vidAsset = AVURLAsset(URL: NSURL(fileURLWithPath: moviePath), options: nil)
// GET THE VIDEO TRACK
let vtrack = vidAsset.tracksWithMediaType(AVMediaTypeVideo)
let videoTrack:AVAssetTrack = vtrack[0]
let vid_timerange = CMTimeRangeMake(kCMTimeZero, vidAsset.duration)
do {
let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
try compositionvideoTrack.insertTimeRange(vid_timerange, ofTrack: videoTrack, atTime: kCMTimeZero)
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
} catch {
print(error)
}
let animationImage: UIImage = self.artworkImage
let artWorkOverlayLayer: CALayer = CALayer()
artWorkOverlayLayer.contents = (animationImage.CGImage as! AnyObject)
artWorkOverlayLayer.frame = CGRectMake(0, 0, 512, 512)
artWorkOverlayLayer.opacity = 0
artWorkOverlayLayer.masksToBounds = true
let animation: CABasicAnimation = CABasicAnimation(keyPath: "opacity")
animation.duration = 10
animation.repeatCount = 0
animation.autoreverses = false
animation.fromValue = Int(0.0)
animation.toValue = Int(1.0)
animation.beginTime = 5.0
artWorkOverlayLayer.addAnimation(animation, forKey: "animateOpacity")
let videolayer = CALayer()
videolayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height)
let parentlayer = CALayer()
parentlayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height)
parentlayer.addSublayer(artWorkOverlayLayer)
let layercomposition = AVMutableVideoComposition()
layercomposition.frameDuration = CMTimeMake(1, 30)
layercomposition.renderSize = videoTrack.naturalSize
layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, inLayer: parentlayer)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
let videotrack = composition.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)
instruction.layerInstructions = NSArray(object: layerinstruction) as! [AVVideoCompositionLayerInstruction]
layercomposition.instructions = NSArray(object: instruction) as! [AVVideoCompositionInstructionProtocol]
// EXPORT
let filePath: NSURL = NSURL.fileURLWithPath(NSTemporaryDirectory().stringByAppendingString("output-tmp.mp4"))
let assetExportSession: AVAssetExportSession! = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
assetExportSession.outputFileType = AVFileTypeMPEG4
assetExportSession.outputURL = filePath
assetExportSession.videoComposition = layercomposition
assetExportSession.exportAsynchronouslyWithCompletionHandler({() -> Void in
print(filePath)
})
如何在单独的文件中最好地重构路由?(没有循环导入)
答案 0 :(得分:0)
您可以使用blueprints或导入包含路线的模块。
/app
__init__.py
/views
|- x.py
|- y.py
|- z.py
然后,您唯一需要做的就是在烧瓶应用程序初始化后导入模块(例如在__init__.py
中)。
app = Flask(__name__)
...
import views.x
import views.y
import views.z
现在所有路线都在您的应用中注册。但是,您不应该从导入的模块中调用函数,因为它会创建循环依赖项。
答案 1 :(得分:0)
根据建议使用蓝图是最佳方式。
首先,我将视图重构为一个单独的文件,并在其中定义了一个蓝图。
#views/skills.py
from flask import Blueprint
from flask import jsonify, make_response
api_blueprint = Blueprint('api', __name__)
@api_blueprint.route('/api/skills/frontend')
def getSkills():
skills = {
"HTML/CSS": [
"SASS",
"Bootstrap"
]
}
return jsonify(skills)
@api_blueprint.route('/api/skills/backend')
def getBackendSkills():
skills = {
"Java": [],
"Python": [
"Django",
"Flask"
],
"Ruby": [
"Ruby on Rails"
],
"PHP": [
"Laravel"
],
"SQL": []
}
return jsonify(skills)
@api_blueprint.route('/api/skills/mixed')
def getMixedSkills():
skills = {
"Javascript": {
'Frontend': [
"angular.js",
"knockout.js",
"coffeescript",
"jquery"
],
'Backend': [
"Node.js",
"Express.js"
]
}
}
return jsonify(skills)
然后我导入并在主文件(app.py)中注册蓝图
from flask import Flask, render_template
from views.skills import api_blueprint
app = Flask(__name__)
app.debug = True
app.register_blueprint(api_blueprint)
@app.route('/skills')
def skillchart():
return render_template('skills.html')
if __name__ == '__main__':
app.run(threaded=True)
这很好用。