123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <template>
- <!-- #ifdef APP -->
- <scroll-view style="flex:1">
- <!-- #endif -->
- <page-head :title="title"></page-head>
- <view>
- <view class="uni-padding-wrap">
- <video class="video" :src="beforeCompressPath" :controls="true" :poster="beforeCoverImagePath"></video>
- <view class="uni-title">
- <text class="uni-subtitle-text">压缩前视频信息</text>
- </view>
- <text>{{beforeCompressVideoInfo}}</text>
- <video class="video" :src="afterCompressPath" :controls="true" :poster="afterCoverImagePath"></video>
- <view class="uni-title">
- <text class="uni-subtitle-text">压缩后视频信息</text>
- </view>
- <text>{{afterCompressVideoInfo}}</text>
- <view class="uni-btn-v">
- <button type="primary" @click="chooseVideo">从相册中选取待压缩的视频</button>
- </view>
- <view class="uni-btn-v">
- <button type="primary" @click="compressVideo">压缩视频</button>
- </view>
- <enum-data title="压缩质量" :items="qualityItemTypes" @change="onQualityChange"></enum-data>
- <view class="uni-common-mt">
- <text class="uni-title uni-title-text">相对于原视频的分辨率比例,取值范围(0, 1]</text>
- <slider :min="0.1" :max="1" :step="0.1" :show-value="true" @change="onResolutionChange"></slider>
- </view>
- </view>
- </view>
- <!-- #ifdef APP -->
- </scroll-view>
- <!-- #endif -->
- </template>
- <script>
- import { ItemType } from '@/components/enum-data/enum-data-types';
- type VideoInfoForTest = {
- width: number;
- height: number;
- isSizeReduce: boolean;
- }
- export default {
- data() {
- return {
- title: "compressVideo",
- beforeCompressVideoInfo: "",
- afterCompressVideoInfo: "",
- beforeCompressPath: "",
- afterCompressPath: "",
- beforeCoverImagePath: "",
- afterCoverImagePath: "",
- quality: null as string | null,
- bitrate: null as number | null,
- fps: null as number | null,
- resolution: null as number | null,
- qualityItemTypes: [{ "value": 0, "name": "low(低)" }, { "value": 1, "name": "medium(中)" }, { "value": 2, "name": "high(高)" }] as ItemType[],
- qualityItems: ["low", "medium", "high"],
- // 自动化测试
- videoInfoForTest: null as VideoInfoForTest | null,
- videoSrcForTest: '/static/test-video/10second-demo.mp4',
- videoSrcForTestWidth: 0,
- videoSrcForTestHeight: 0
- }
- },
- methods: {
- compressVideo() {
- if (this.beforeCompressPath == "") {
- uni.showToast({
- title: "请先选择视频",
- icon: "error"
- });
- return;
- }
- uni.showLoading({
- title: "视频压缩中"
- });
- uni.compressVideo({
- src: this.beforeCompressPath,
- quality: this.quality,
- resolution: this.resolution,
- success: (res) => {
- console.log("compressVideo success", JSON.stringify(res));
- this.afterCompressPath = res.tempFilePath;
- uni.showToast({
- title: "压缩成功",
- icon: null
- });
- uni.getVideoInfo({
- src: res.tempFilePath,
- success: (_res) => {
- this.afterCompressVideoInfo = `视频画面方向: ${_res.orientation}\n视频格式: ${_res.type}\n视频长度: ${_res.duration}s\n视频大小: ${_res.size}KB\n视频宽度: ${_res.width}\n视频高度: ${_res.height}\n视频帧率: ${_res.fps}fps\n视频码率: ${_res.bitrate}kbps`;
- // #ifdef APP-ANDROID || APP-IOS
- this.afterCompressVideoInfo = this.afterCompressVideoInfo + `\n视频字节大小: ${_res.byteSize}B\n视频首帧图片路径: ${_res.thumbTempFilePath}`
- if(_res.thumbTempFilePath != null) {
- this.afterCoverImagePath = _res.thumbTempFilePath!
- }
- // #endif
- }
- });
- },
- fail: (err) => {
- uni.showModal({
- title: "压缩视频失败",
- content: JSON.stringify(err),
- showCancel: false
- });
- },
- complete: (_) => {
- uni.hideLoading();
- }
- });
- },
- chooseVideo() {
- uni.chooseVideo({
- sourceType: ["album"],
- compressed: false,
- success: (res) => {
- this.beforeCompressPath = res.tempFilePath;
- uni.getVideoInfo({
- src: res.tempFilePath,
- success: (_res) => {
- this.beforeCompressVideoInfo = `视频画面方向: ${_res.orientation}\n视频格式: ${_res.type}\n视频长度: ${_res.duration}s\n视频大小: ${_res.size}KB\n视频宽度: ${_res.width}\n视频高度: ${_res.height}\n视频帧率: ${_res.fps}fps\n视频码率: ${_res.bitrate}kbps`;
- // #ifdef APP-ANDROID || APP-IOS
- this.beforeCompressVideoInfo = this.beforeCompressVideoInfo + `\n视频字节大小: ${_res.byteSize}B\n视频首帧图片路径: ${_res.thumbTempFilePath}`
- if(_res.thumbTempFilePath != null) {
- this.beforeCoverImagePath = _res.thumbTempFilePath!
- }
- // #endif
- }
- });
- }
- });
- },
- onQualityChange(value : number) {
- this.quality = this.qualityItems[value];
- },
- onResolutionChange(event : UniSliderChangeEvent) {
- this.resolution = event.detail.value;
- },
- testCompressVideo() {
- let beforeCompressSize : number, afterComoressSize : number;
- uni.compressVideo({
- src: this.videoSrcForTest,
- quality: 'medium',
- success: (res) => {
- uni.getVideoInfo({
- src: this.videoSrcForTest,
- success: (_res) => {
- beforeCompressSize = Math.trunc(_res.size);
- this.videoSrcForTestWidth = _res.width
- this.videoSrcForTestHeight = _res.height
- uni.getVideoInfo({
- src: res.tempFilePath,
- success: (__res) => {
- afterComoressSize = Math.trunc(__res.size);
- this.videoInfoForTest = {
- "width": __res.width,
- "height": __res.height,
- "isSizeReduce": afterComoressSize < beforeCompressSize
- } as VideoInfoForTest;
- },
- fail(err) {
- console.log('>>>>>> 压缩失败', err.errMsg)
- }
- });
- }
- });
- },
- fail: (_) => {
- this.videoInfoForTest = null;
- }
- });
- }
- }
- }
- </script>
- <style>
- .video {
- align-self: center;
- }
- .image-container {
- flex-direction: row;
- }
- </style>
|