Skip to content

Commit

Permalink
Merge pull request #15 from vimondev/feat/speed
Browse files Browse the repository at this point in the history
[LUS] 0206 generateVideo -> Parameter change to EMusicSpeed, automati…
  • Loading branch information
vimondev authored Feb 27, 2024
2 parents 2d6ff1a + 24a6fd2 commit 81c9a6c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 143 deletions.
15 changes: 8 additions & 7 deletions lib/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -53,22 +53,23 @@ class TestWidget extends StatelessWidget {
if (mediaList.length >= 30) break;
}

for (int i=0; i<EMusicStyle.values.length; i++) {
EMusicStyle style = EMusicStyle.energetic; //EMusicStyle.values[i % EMusicStyle.values.length];
for (int i=0; i<EMusicSpeed.values.length; i++) {
EMusicSpeed speed = EMusicSpeed.values[i % EMusicSpeed.values.length];
if (speed == EMusicSpeed.none) continue;

VideoGeneratedResult result =
await _vmsdkWidget.generateVideo(mediaList, style, false,
// ["THIS IS TITLE", "THIS IS SUBTITLE"], "ko", (status, progress) {
await _vmsdkWidget.generateVideo(mediaList, speed, false,
["THIS IS TITLE", "THIS IS SUBTITLE"], "ko", (status, progress) {
// ["THIS IS TITLE 😀", "🕹 이모지 테스트 ✅"], "ko", (status, progress) {
// ["THIS IS TITLE", "☻ ♥ ♦ ♣ ♠ 특수문자 테스트"], "ko", (status, progress) {
["THIS IS TITLE 😀", "🕹 ☻ ♥ ♦ ♣ ♠ 특수문자 테스트 ✅"], "ko", (status, progress) {
// ["THIS IS TITLE 😀", "🕹 ☻ ♥ ♦ ♣ ♠ 특수문자 테스트 ✅"], "ko", (status, progress) {
//["THIS IS TITLE", "☻ ♥ ♦ ♣ ♠ 특수문자 테스트"
print(status);
print(progress);
});

await GallerySaver.saveVideo(result.generatedVideoPath);
break;
// break;
}

// VideoGeneratedResult result = await _vmsdkWidget.generateVideo(
Expand Down
138 changes: 33 additions & 105 deletions lib/vm_sdk/impl/auto_edit_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import 'type_helper.dart';
import 'resource_fetch_helper.dart';

class _GetMusicResponse {
EMusicStyle musicStyle;
EMusicSpeed musicSpeed;
List<MusicData> musicList;

_GetMusicResponse(this.musicStyle, this.musicList);
_GetMusicResponse(this.musicSpeed, this.musicList);
}

Map<int, EMediaLabel> _classifiedLabelMap = {};
Expand Down Expand Up @@ -140,30 +140,28 @@ ERatio detectRatio(List<EditedMedia> list) {

Future<AllEditedData> generateAllEditedData(
List<MediaData> list,
EMusicStyle? musicStyle,
EMusicSpeed? musicSpeed,
List<TemplateData> templateList,
bool isAutoSelect,
{ isRunFFmpeg = true }) async {
final AllEditedData allEditedData = AllEditedData();

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<MusicData> 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;

Expand Down Expand Up @@ -582,7 +580,7 @@ Future<AllEditedData> generateAllEditedData(
// TO DO : Load from Template Data
final List<XFadeTransitionData> originXfadeTransitionList =
ResourceManager.getInstance().getAllXFadeTransitions(speed: speed);
ResourceManager.getInstance().getAllXFadeTransitions();
// final List<OverlayTransitionData>
// originOverlayTransitionList = ResourceManager.getInstance().getAllOverlayTransitions(speed: speed);

Expand Down Expand Up @@ -827,7 +825,7 @@ Future<AllEditedData> generateAllEditedData(
int musicIndex = 0;

Map<String, MusicData> musicDataMap = {};
while (totalRemainDuration > 0) {
while (musicList.isNotEmpty && totalRemainDuration > 0) {
MusicData musicData = musicList[musicIndex % musicList.length];
allEditedData.musicList.add(musicData);

Expand Down Expand Up @@ -859,11 +857,17 @@ Future<void> _downloadAndMapMusic(MusicData musicData) async {
musicData.absolutePath = file.path;
}

Future<_GetMusicResponse> _getMusics(EMusicStyle? musicStyle) async {
Future<_GetMusicResponse> _getMusics(EMusicSpeed? musicSpeed) async {
final List<MusicData> randomSortMusicList = [];

final Map<EMusicStyle, List<SongFetchModel>> songMapByMusicStyle = {};
final Map<String, List<SongFetchModel>> songMapBySpeed = {};
final Map<EMusicSpeed, List<SongFetchModel>> songMapBySpeed = {
EMusicSpeed.ss: [],
EMusicSpeed.s: [],
EMusicSpeed.m: [],
EMusicSpeed.mm: [],
EMusicSpeed.f: [],
EMusicSpeed.ff: [],
};

List<SongFetchModel> songs = [];
songs.addAll(ResourceManager.getInstance().getAllSongFetchModels()); // copy elements
Expand All @@ -874,108 +878,32 @@ 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<double, String> speedProbabilityMap = {
0.3: "S",
0.6: "M",
0.8: "MM",
1.0: "F"
if (musicSpeed == null || musicSpeed == EMusicSpeed.none) {
Map<double, EMusicSpeed> 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) {
randSpeed = elem.value;
break;
}
}

List<SongFetchModel> 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<SongFetchModel> originSongList = songMapByMusicStyle[curStyle]!;
// List<SongFetchModel> recommendedSongList = [];
// List<SongFetchModel> 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<SongFetchModel> 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) {
Expand All @@ -1002,5 +930,5 @@ Future<_GetMusicResponse> _getMusics(EMusicStyle? musicStyle) async {
}
}

return _GetMusicResponse(curStyle, randomSortMusicList);
return _GetMusicResponse(musicSpeed, randomSortMusicList);
}
14 changes: 0 additions & 14 deletions lib/vm_sdk/impl/type_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 2 additions & 6 deletions lib/vm_sdk/types/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class SongFetchModel {
String title = "";
double duration = 0;
bool isRecommended = false;
String speed = "M";
EMusicSpeed speed = EMusicSpeed.none;
List<HashTagModel> hashtags = [];
SourceModel? source;

SongFetchModel.fromJson(Map map) {
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) {
Expand All @@ -53,14 +53,12 @@ class FrameFetchModel {
String name = "";
double duration = 0;
EMediaLabel type = EMediaLabel.none;
EMusicSpeed speed = EMusicSpeed.medium;
Map<ERatio, SourceModel> 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"]);
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
24 changes: 18 additions & 6 deletions lib/vm_sdk/types/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ enum EMediaLabel {
others
}
enum EMusicSpeed {
slow,
medium,
fast
none,
ss,
s,
m,
mm,
f,
ff
}
enum EMusicStyle {
none,
Expand All @@ -34,6 +38,15 @@ enum EMusicStyle {
energetic
}

const Map<String, EMusicSpeed> musicSpeedMap = {
"SS": EMusicSpeed.ss,
"S": EMusicSpeed.s,
"M": EMusicSpeed.m,
"MM": EMusicSpeed.mm,
"F": EMusicSpeed.f,
"FF": EMusicSpeed.ff
};

const Map<String, EMusicStyle> musicStyleMap = {
"Energetic": EMusicStyle.energetic,
"Cheerful": EMusicStyle.cheerful,
Expand Down Expand Up @@ -163,9 +176,8 @@ class Resolution {
class AllEditedData {
List<EditedMedia> editedMediaList = [];
List<MusicData> musicList = [];
String speed = "";
ERatio ratio = ERatio.ratio11;
EMusicStyle style = EMusicStyle.none;
EMusicSpeed speed = EMusicSpeed.none;
Resolution resolution = Resolution.fromRatio(ERatio.ratio11);
}

Expand All @@ -181,7 +193,7 @@ class VideoGeneratedResult {
String generatedVideoPath;
List<SpotInfo> spotInfoList;
List<String> thumbnailList;
EMusicStyle musicStyle = EMusicStyle.none;
EMusicSpeed speed = EMusicSpeed.none;

List<EditedMedia> editedMediaList = [];
List<MusicData> musicList = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/vm_sdk/types/resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum EStickerType { object }
class MusicData {
String title = "";
String filename = "";
String speed = "";
EMusicSpeed? speed;
String url = "";
String? absolutePath;
double duration = 0;
Expand Down
Loading

0 comments on commit 81c9a6c

Please sign in to comment.