From 24a6fd205fd977760e30136b8bece680ac344b72 Mon Sep 17 00:00:00 2001 From: LUS Date: Tue, 6 Feb 2024 19:27:04 +0900 Subject: [PATCH] [LUS] 0206 generateVideo -> Parameter change to EMusicSpeed, automatic music recommendation logic change --- lib/test.dart | 15 +-- lib/vm_sdk/impl/auto_edit_helper.dart | 138 ++++++-------------------- lib/vm_sdk/impl/type_helper.dart | 14 --- lib/vm_sdk/types/fetch.dart | 8 +- lib/vm_sdk/types/global.dart | 24 +++-- lib/vm_sdk/types/resource.dart | 2 +- lib/vm_sdk/vm_sdk.dart | 8 +- 7 files changed, 66 insertions(+), 143 deletions(-) diff --git a/lib/test.dart b/lib/test.dart index 2990368..928b9a5 100644 --- a/lib/test.dart +++ b/lib/test.dart @@ -17,7 +17,7 @@ class TestWidget extends StatelessWidget { await _vmsdkWidget.initialize(); } - String testSetName = "set1_1080p"; + String testSetName = "set1"; final filelist = json.decode( await rootBundle.loadString("assets/_test/mediajson-joined/$testSetName.json")); @@ -53,22 +53,23 @@ class TestWidget extends StatelessWidget { if (mediaList.length >= 30) break; } - for (int i=0; i musicList; - _GetMusicResponse(this.musicStyle, this.musicList); + _GetMusicResponse(this.musicSpeed, this.musicList); } Map _classifiedLabelMap = {}; @@ -140,7 +140,7 @@ ERatio detectRatio(List list) { Future generateAllEditedData( List list, - EMusicStyle? musicStyle, + EMusicSpeed? musicSpeed, List templateList, bool isAutoSelect, { isRunFFmpeg = true }) async { @@ -148,22 +148,20 @@ Future generateAllEditedData( list.sort((a, b) => a.createDate.compareTo(b.createDate)); - final musicsData = await _getMusics(musicStyle); - allEditedData.style = musicsData.musicStyle; + final musicsData = await _getMusics(musicSpeed); final List musicList = musicsData.musicList; - final String speed = musicList.isNotEmpty ? musicList[0].speed : "M"; - allEditedData.speed = speed; + allEditedData.speed = musicsData.musicSpeed; - print("curSpeed : $speed"); + print("curSpeed : ${allEditedData.speed}"); bool isUseTemplateDuration = false; if (list.length >= 10) { - switch (speed) { - case "MM": - case "F": - case "FF": + switch (allEditedData.speed) { + case EMusicSpeed.mm: + case EMusicSpeed.f: + case EMusicSpeed.ff: isUseTemplateDuration = true; break; @@ -582,7 +580,7 @@ Future generateAllEditedData( // TO DO : Load from Template Data final List originXfadeTransitionList = - ResourceManager.getInstance().getAllXFadeTransitions(speed: speed); + ResourceManager.getInstance().getAllXFadeTransitions(); // final List // originOverlayTransitionList = ResourceManager.getInstance().getAllOverlayTransitions(speed: speed); @@ -827,7 +825,7 @@ Future generateAllEditedData( int musicIndex = 0; Map musicDataMap = {}; - while (totalRemainDuration > 0) { + while (musicList.isNotEmpty && totalRemainDuration > 0) { MusicData musicData = musicList[musicIndex % musicList.length]; allEditedData.musicList.add(musicData); @@ -859,11 +857,17 @@ Future _downloadAndMapMusic(MusicData musicData) async { musicData.absolutePath = file.path; } -Future<_GetMusicResponse> _getMusics(EMusicStyle? musicStyle) async { +Future<_GetMusicResponse> _getMusics(EMusicSpeed? musicSpeed) async { final List randomSortMusicList = []; - final Map> songMapByMusicStyle = {}; - final Map> songMapBySpeed = {}; + final Map> songMapBySpeed = { + EMusicSpeed.ss: [], + EMusicSpeed.s: [], + EMusicSpeed.m: [], + EMusicSpeed.mm: [], + EMusicSpeed.f: [], + EMusicSpeed.ff: [], + }; List songs = []; songs.addAll(ResourceManager.getInstance().getAllSongFetchModels()); // copy elements @@ -874,31 +878,18 @@ Future<_GetMusicResponse> _getMusics(EMusicStyle? musicStyle) async { SongFetchModel song = songs[randIdx]; songs.removeAt(randIdx); - if (song.hashtags.isNotEmpty) { - for (final hashTagModel in song.hashtags) { - if (musicStyleMap.containsKey(hashTagModel.name)) { - EMusicStyle style = musicStyleMap[hashTagModel.name]!; - if (!songMapByMusicStyle.containsKey(style)) songMapByMusicStyle[style] = []; - songMapByMusicStyle[style]!.add(song); - } - } - } - if (song.speed.isNotEmpty) { - if (!songMapBySpeed.containsKey(song.speed)) songMapBySpeed[song.speed] = []; - songMapBySpeed[song.speed]!.add(song); - } + songMapBySpeed[song.speed]!.add(song); } - EMusicStyle? curStyle = musicStyle; - if (curStyle == null || curStyle == EMusicStyle.none) { - Map speedProbabilityMap = { - 0.3: "S", - 0.6: "M", - 0.8: "MM", - 1.0: "F" + if (musicSpeed == null || musicSpeed == EMusicSpeed.none) { + Map speedProbabilityMap = { + 0.3: EMusicSpeed.s, + 0.6: EMusicSpeed.m, + 0.8: EMusicSpeed.mm, + 1.0: EMusicSpeed.f }; double randValue = Random().nextDouble(); - String randSpeed = songMapBySpeed.keys.first; + EMusicSpeed randSpeed = songMapBySpeed.keys.first; for (final elem in speedProbabilityMap.entries) { if (randValue < elem.key) { @@ -906,76 +897,13 @@ Future<_GetMusicResponse> _getMusics(EMusicStyle? musicStyle) async { break; } } - - List randSongs = songMapBySpeed[randSpeed]!; - int randIdx = (Random()).nextInt(randSongs.length) % randSongs.length; - SongFetchModel randSong = randSongs[randIdx]; - - if (randSong.hashtags.isNotEmpty) { - int randHashtagIdx = (Random()).nextInt(randSong.hashtags.length) % randSong.hashtags.length; - HashTagModel randHashtag = randSong.hashtags[randHashtagIdx]; - - if (musicStyleMap.containsKey(randHashtag.name)) { - curStyle = musicStyleMap[randHashtag.name]!; - } - else { - curStyle = EMusicStyle.fun; - } - } - else { - curStyle = EMusicStyle.fun; - } + musicSpeed = randSpeed; } - - // List originSongList = songMapByMusicStyle[curStyle]!; - // List recommendedSongList = []; - // List otherSongList = []; - - // recommendedSongList.addAll(originSongList.where((song) => song.isRecommended)); - // otherSongList.addAll(originSongList.where((song) => !song.isRecommended)); - - // print("style : $musicStyle"); - // print("style : $curStyle"); - // print("recommendedSongList : ${recommendedSongList.length}"); - // print("otherSongList : ${otherSongList.length}"); - - // while (recommendedSongList.isNotEmpty || otherSongList.isNotEmpty) { - // SongFetchModel song; - // if (otherSongList.isEmpty || recommendedSongList.isNotEmpty) {// && Random().nextDouble() <= 0.7) { - // int randIdx = (Random()).nextInt(recommendedSongList.length) % recommendedSongList.length; - // song = recommendedSongList[randIdx]; - // recommendedSongList.removeAt(randIdx); - // } - // else { - // int randIdx = (Random()).nextInt(otherSongList.length) % otherSongList.length; - // song = otherSongList[randIdx]; - // otherSongList.removeAt(randIdx); - // } - - // double duration = song.duration; - // SourceModel? source = song.source; - - // if (source != null) { - // String name = source.name; - // String url = source.url; - - // MusicData musicData = MusicData(); - // musicData.title = song.title; - // musicData.duration = duration; - // musicData.filename = name; - // musicData.speed = song.speed; - // musicData.url = url; - // musicData.volume = 0.5; - - // randomSortMusicList.add(musicData); - // } - // } List allSongList = []; - allSongList.addAll(songMapByMusicStyle[curStyle]!); + allSongList.addAll(songMapBySpeed[musicSpeed]!); - print("style : $musicStyle"); - print("style : $curStyle"); + print("style : $musicSpeed"); print("allSongList : ${allSongList.length}"); while (allSongList.isNotEmpty) { @@ -1002,5 +930,5 @@ Future<_GetMusicResponse> _getMusics(EMusicStyle? musicStyle) async { } } - return _GetMusicResponse(curStyle, randomSortMusicList); + return _GetMusicResponse(musicSpeed, randomSortMusicList); } \ No newline at end of file diff --git a/lib/vm_sdk/impl/type_helper.dart b/lib/vm_sdk/impl/type_helper.dart index 79a9a2f..70263be 100644 --- a/lib/vm_sdk/impl/type_helper.dart +++ b/lib/vm_sdk/impl/type_helper.dart @@ -26,18 +26,4 @@ EMediaLabel getMediaLabel(String type) { default: return EMediaLabel.none; } -} - -EMusicSpeed getMusicSpeed(String speed) { - switch (speed) { - case "F": - return EMusicSpeed.fast; - - case "S": - return EMusicSpeed.slow; - - case "M": - default: - return EMusicSpeed.medium; - } } \ No newline at end of file diff --git a/lib/vm_sdk/types/fetch.dart b/lib/vm_sdk/types/fetch.dart index c7fd752..fd0dedd 100644 --- a/lib/vm_sdk/types/fetch.dart +++ b/lib/vm_sdk/types/fetch.dart @@ -26,7 +26,7 @@ class SongFetchModel { String title = ""; double duration = 0; bool isRecommended = false; - String speed = "M"; + EMusicSpeed speed = EMusicSpeed.none; List hashtags = []; SourceModel? source; @@ -34,7 +34,7 @@ class SongFetchModel { title = map["title"] ?? ""; duration = map["duration"] != null ? map["duration"] * 1.0 : 0; isRecommended = map["isRecommended"] ?? false; - speed = map["speed"] ?? "M"; + speed = musicSpeedMap[map["speed"]?.toString()] ?? EMusicSpeed.none; Map? sourceMap = map["source"]; if (sourceMap != null) { @@ -53,14 +53,12 @@ class FrameFetchModel { String name = ""; double duration = 0; EMediaLabel type = EMediaLabel.none; - EMusicSpeed speed = EMusicSpeed.medium; Map sourceMap = {}; FrameFetchModel.fromJson(Map map) { name = map["name"] ?? ""; duration = map["duration"] != null ? map["duration"] * 1.0 : 0; type = getMediaLabel(map["type"]); - speed = getMusicSpeed(map["speed"]); sourceMap[ERatio.ratio11] = SourceModel.fromJson(map["source_11"]); sourceMap[ERatio.ratio169] = SourceModel.fromJson(map["source_169"]); @@ -74,7 +72,6 @@ class StickerFetchModel { int width = 0; int height = 0; EMediaLabel type = EMediaLabel.none; - EMusicSpeed speed = EMusicSpeed.medium; SourceModel? source; StickerFetchModel.fromJson(Map map) { @@ -83,7 +80,6 @@ class StickerFetchModel { width = map["width"] ?? 0; height = map["height"] ?? 0; type = getMediaLabel(map["type"]); - speed = getMusicSpeed(map["speed"]); Map? sourceMap = map["source"]; if (sourceMap != null) { diff --git a/lib/vm_sdk/types/global.dart b/lib/vm_sdk/types/global.dart index 6410c2f..3a8981f 100644 --- a/lib/vm_sdk/types/global.dart +++ b/lib/vm_sdk/types/global.dart @@ -14,9 +14,13 @@ enum EMediaLabel { others } enum EMusicSpeed { - slow, - medium, - fast + none, + ss, + s, + m, + mm, + f, + ff } enum EMusicStyle { none, @@ -34,6 +38,15 @@ enum EMusicStyle { energetic } +const Map musicSpeedMap = { + "SS": EMusicSpeed.ss, + "S": EMusicSpeed.s, + "M": EMusicSpeed.m, + "MM": EMusicSpeed.mm, + "F": EMusicSpeed.f, + "FF": EMusicSpeed.ff +}; + const Map musicStyleMap = { "Energetic": EMusicStyle.energetic, "Cheerful": EMusicStyle.cheerful, @@ -163,9 +176,8 @@ class Resolution { class AllEditedData { List editedMediaList = []; List musicList = []; - String speed = ""; ERatio ratio = ERatio.ratio11; - EMusicStyle style = EMusicStyle.none; + EMusicSpeed speed = EMusicSpeed.none; Resolution resolution = Resolution.fromRatio(ERatio.ratio11); } @@ -181,7 +193,7 @@ class VideoGeneratedResult { String generatedVideoPath; List spotInfoList; List thumbnailList; - EMusicStyle musicStyle = EMusicStyle.none; + EMusicSpeed speed = EMusicSpeed.none; List editedMediaList = []; List musicList = []; diff --git a/lib/vm_sdk/types/resource.dart b/lib/vm_sdk/types/resource.dart index 17c8325..ed182ff 100644 --- a/lib/vm_sdk/types/resource.dart +++ b/lib/vm_sdk/types/resource.dart @@ -7,7 +7,7 @@ enum EStickerType { object } class MusicData { String title = ""; String filename = ""; - String speed = ""; + EMusicSpeed? speed; String url = ""; String? absolutePath; double duration = 0; diff --git a/lib/vm_sdk/vm_sdk.dart b/lib/vm_sdk/vm_sdk.dart index 7c2cebc..8c7527b 100644 --- a/lib/vm_sdk/vm_sdk.dart +++ b/lib/vm_sdk/vm_sdk.dart @@ -67,7 +67,7 @@ class VMSDKWidget extends StatelessWidget { // In the current version, only styleA works. Future generateVideo( List mediaList, - EMusicStyle? style, + EMusicSpeed? speed, bool isAutoEdit, List texts, String language, @@ -90,7 +90,7 @@ class VMSDKWidget extends StatelessWidget { mediaList = await _filterNotExistsMedia(mediaList); final AllEditedData allEditedData = await generateAllEditedData( - mediaList, style, randomSortedTemplateList, isAutoEdit, isRunFFmpeg: isRunFFmpeg); + mediaList, speed, randomSortedTemplateList, isAutoEdit, isRunFFmpeg: isRunFFmpeg); Resolution resolution = allEditedData.resolution; final int maxTextWidth = (resolution.width * 0.9).floor(); @@ -194,7 +194,7 @@ class VMSDKWidget extends StatelessWidget { allEditedData.editedMediaList[0].canvasTexts.add(canvasTextData); } else { - List textDatas = ResourceManager.getInstance().getTextDataList(lineCount: texts.length, speed: allEditedData.speed); + List textDatas = ResourceManager.getInstance().getTextDataList(lineCount: texts.length); if (textDatas.isEmpty) { textDatas = ResourceManager.getInstance().getTextDataList(lineCount: texts.length); } @@ -264,7 +264,7 @@ class VMSDKWidget extends StatelessWidget { allEditedData.ratio, progressCallback, isAutoEdit: true, isRunFFmpeg: isRunFFmpeg); - result.musicStyle = allEditedData.style; + result.speed = allEditedData.speed; result.editedMediaList.addAll(allEditedData.editedMediaList); result.musicList.addAll(allEditedData.musicList);