Skip to content

Commit

Permalink
feat: Use ffmpmeg to convert webm to mp3 (support mp3 headers)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinkey04 committed May 27, 2021
1 parent 2266bd8 commit 1959371
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.flutterFFmpegPackage = 'audio-lts'
repositories {
google()
jcenter()
Expand Down
39 changes: 38 additions & 1 deletion lib/provider/audio_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 'dart:io';
import 'dart:math';

import 'package:audiotagger/audiotagger.dart';
import 'package:flutter/animation.dart';
import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';

AnimationController playFABController;

Expand Down Expand Up @@ -28,7 +34,6 @@ String formatDuration(Duration d) {
return format;
}


bool isValidYouTubeURL(String url) {
url =
url.replaceAll('://m.y', '://www.y'); //For changing mobile URL to web URL
Expand All @@ -41,3 +46,35 @@ bool isValidYouTubeURL(String url) {
else
return true;
}

Future<File> urlToFile(String imageUrl) async {
//https://mrgulshanyadav.medium.com/convert-image-url-to-file-format-in-flutter-10421bccfd18
var random = new Random();
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = new File('$tempPath' + (random.nextInt(100)).toString() + '.png');
http.Response response = await http.get(Uri.parse(imageUrl));
await file.writeAsBytes(response.bodyBytes);
return file;
}

Future<String> webmToMP3(String path) async {
final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();
int result = 0;
final newPath = path.replaceAll('.webm', '_RaagApp.mp3');
var command = "-i \"$path\" -vn -ab 128k -ar 44100 -y \"$newPath\"";
await _flutterFFmpeg.execute(command).then((rc) {
print('FFmpeg Result: $rc');
result = rc;
});
await File(path).delete();
return newPath;
}

Future<bool> tagArtWork(String url, String path) async {
var tagger = new Audiotagger();
File albumArtFile = await urlToFile(url);
final result = await tagger.writeTag(
path: path, tagField: "artist", value: 'Artist');
return result;
}
8 changes: 7 additions & 1 deletion lib/view/download_music.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class _DownloadMusicState extends State<DownloadMusic> {
.replaceAll('/', ' ')
.replaceAll(':', ' ');
//TODO Do something efficient to choose only alpha-numeric characters from $title
var filePath = _raagDownloadsDirectory.path + '/' + tempTitle + '.mp3';
var filePath = _raagDownloadsDirectory.path + '/' + tempTitle + '.webm';

if (streamInfo != null) {
var stream = yt.videos.streamsClient.get(streamInfo);
Expand All @@ -156,6 +156,12 @@ class _DownloadMusicState extends State<DownloadMusic> {
return s;
}).pipe(fileSink);

setTitle('Converting to MP3');
filePath = await webmToMP3(filePath);

setTitle('Adding tags');
print(await tagArtWork(video.thumbnails.mediumResUrl, filePath));

setTitle(downloadComplete);
setBody(
'$fileLocation: $filePath\n$fileSize: ${(streamInfo.size.totalMegaBytes.toString().substring(0, 4))} MB');
Expand Down
41 changes: 24 additions & 17 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.1"
audiotagger:
dependency: "direct main"
description:
name: audiotagger
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -113,20 +120,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.6"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.2"
version: "0.17.0"
cupertino_icons:
dependency: "direct main"
description:
Expand All @@ -147,7 +147,7 @@ packages:
name: equatable
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.6"
version: "2.0.0"
fading_edge_scrollview:
dependency: transitive
description:
Expand Down Expand Up @@ -188,6 +188,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.5+6"
flutter_ffmpeg:
dependency: "direct main"
description:
name: flutter_ffmpeg
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
Expand Down Expand Up @@ -218,21 +225,21 @@ packages:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+4"
version: "0.15.0"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2"
version: "0.13.3"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
version: "4.0.0"
js:
dependency: transitive
description:
Expand All @@ -246,7 +253,7 @@ packages:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.1"
version: "4.0.1"
marquee:
dependency: "direct main"
description:
Expand Down Expand Up @@ -337,7 +344,7 @@ packages:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
version: "4.1.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -538,14 +545,14 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.1"
version: "5.1.0"
youtube_explode_dart:
dependency: "direct main"
description:
name: youtube_explode_dart
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.9.3+2"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.24.0-10"
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ dependencies:
audio_manager: ^0.5.5+3
audioplayer: 0.8.1
audioplayer_web: 0.7.1
audiotagger: ^2.2.0
clipboard: ^0.1.2+8
connectivity: ^2.0.2
downloads_path_provider_28: ^0.1.0
flutter_audio_query: ^0.3.5+6
flutter_ffmpeg: ^0.4.0
fluttertoast: ^7.1.1
http: ^0.13.3
marquee: ^1.6.1
open_file: ^3.2.0
path_provider: ^2.0.0-nullsafety.1
Expand Down

0 comments on commit 1959371

Please sign in to comment.