Skip to content

Commit

Permalink
Add mp3 generator
Browse files Browse the repository at this point in the history
  • Loading branch information
macs1207 committed Jun 5, 2020
1 parent a3bc61e commit 584e964
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 66 deletions.
11 changes: 6 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def get_resource():
else:
return make_response("need resource id", 400)

@app.route('/api/video', methods=['POST'])
@app.route('/api/media', methods=['POST'])
def make_video():
text = request.values.get('text')
file_format = "mp3"
file_format = request.values.get('format')
if text is not None:
text = text.strip()
if len(text) == 0:
Expand All @@ -85,10 +85,11 @@ def make_video():
"error": "text is too long",
}), 400)
try:
dir_path, video_id = VideoProcessor().get_media(text, file_format)
print(video_id)
dir_path, media_id = VideoProcessor().get_media(text, file_format)
print(media_id)
return jsonify({
"video_id": video_id
"media_id": media_id,
"path": dir_path
})
except VideoNotFoundError as e:
logging.error(e)
Expand Down
104 changes: 43 additions & 61 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ <h3 class="col-xs-12 col-sm-12 col-md-12 col-lg-12 line-size">可以輸入注音
aria-describedby="inputGroup-sizing-sm" placeholder="上限暫時50字,守護How哥的嘴巴" v-model="text"></textarea>
</div>
<div class="btn-group space input-btn-width" role="group">
<button id="getVideo-btn" class="btn-font-size btn btn-primary" type="button" v-on:click="getVideo">
<button id="getVideo-btn" class="btn-font-size btn btn-primary" type="button" v-on:click="getMedia('mp4')">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" v-show="videoWaiting"></span>
取得祝賀影片
</button>
<button id="getVoice-btn" class="btn-font-size btn btn-primary" type="button" v-on:click="getVoice" disabled>
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" v-show="voiceWaiting"></span>
<button id="getAudio-btn" class="btn-font-size btn btn-primary" type="button" v-on:click="getMedia('mp3')">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" v-show="audioWaiting"></span>
取得祝賀音檔
</button>
</div>
<a v-show="video" :href="video" class="btn-font-size btn btn-primary space input-btn-width" target="_blank">影片沒有跳轉的話按這邊下載</a>
<a v-show="voice" :href="voice" class="btn-font-size btn btn-primary space input-btn-width" target="_blank">音檔沒有跳轉的話按這邊下載</a>
<a v-show="audio" :href="audio" class="btn-font-size btn btn-primary space input-btn-width" target="_blank">音檔沒有跳轉的話按這邊下載</a>
</div>
</div>
</div>
Expand All @@ -90,25 +90,41 @@ <h3 class="col-xs-12 col-sm-12 col-md-12 col-lg-12 line-size">可以輸入注音
data: {
text: '',
videoWaiting: false,
voiceWaiting: false,
audioWaiting: false,
video: null,
voice: null,
audio: null,
},
methods: {
getVideo: function () {
getMedia: function (format) {
if (this.text) {
$("#getVideo-btn").attr('disabled', true);
this.videoWaiting = true;
this.$http.post("/api/video", {
switch (format) {
case 'mp4':
console.log(format);
$("#getVideo-btn").attr('disabled', true);
this.videoWaiting = true;
break;
case 'mp3':
$("#getAudio-btn").attr('disabled', true);
this.audioWaiting = true;
}
this.$http.post("/api/media", {
text: this.text,
}, { emulateJSON: true }).then(
format: format
}, {
emulateJSON: true
}).then(
function (response) {
// console.log(`${response.status} ${response.bodyText}`);
this.video = `/video?v=${JSON.parse(response.bodyText)["video_id"]}`;
// console.log(this.video);
this.videoWaiting = false;
$("#getVideo-btn").attr('disabled', false);
window.open(this.video, '_blank')
switch (format) {
case 'mp4':
this.video = `/video?v=${JSON.parse(response.bodyText)["media_id"]}`;
window.open(this.video, '_blank')
break;
case 'mp3':
this.audio = `/audio?a=${JSON.parse(response.bodyText)["media_id"]}`;
window.open(this.audio, '_blank')
}
this.releaseBtn(format);
}, function (response) {
console.log(`${response.status} ${response.bodyText}`);
response = JSON.parse(response.bodyText);
Expand All @@ -130,8 +146,7 @@ <h3 class="col-xs-12 col-sm-12 col-md-12 col-lg-12 line-size">可以輸入注音
'',
'error'
)
this.videoWaiting = false;
$("#getVideo-btn").attr('disabled', false);
this.releaseBtn(format);
}
);
} else {
Expand All @@ -141,49 +156,16 @@ <h3 class="col-xs-12 col-sm-12 col-md-12 col-lg-12 line-size">可以輸入注音
'error'
)
}
}, getVoice: function() {
if (this.text) {
$("#getVoice-btn").attr('disabled', true);
this.waiting = true;
this.$http.post("/api/voice", {
text: this.text,
}, { emulateJSON: true }).then(
function (response) {
// console.log(`${response.status} ${response.bodyText}`);
this.voice = `/voice?v=${JSON.parse(response.bodyText)["voice_id"]}`;
// console.log(this.video);
this.waiting = false;
$("#getVoice-btn").attr('disabled', false);
window.open(this.voice, '_blank')
}, function (response) {
console.log(`${response.status} ${response.bodyText}`);
let errormsg = '未定義錯誤';
switch (response.bodyText) {
case "too long":
errormsg = "你要讓How哥念到嘴酸ㄇ";
break;
case "empty":
errormsg = "沒有文字不能送ㄛ";
break;
case "not found":
errormsg = "How哥沒念這個字";
break;
}
Swal.fire(
errormsg,
'',
'error'
)
this.waiting = false;
$("#getVoice-btn").attr('disabled', false);
}
);
} else {
Swal.fire(
'沒有文字不能送ㄛ',
'',
'error'
)
},
releaseBtn: function (format) {
switch (format) {
case 'mp4':
this.videoWaiting = false;
$("#getVideo-btn").attr('disabled', false);
break;
case 'mp3':
this.audioWaiting = false;
$("#getAudio-btn").attr('disabled', false);
}
}
}
Expand Down

0 comments on commit 584e964

Please sign in to comment.