Upload songs/audio-files to a specific soundcloud

2019-08-31 10:15发布

问题:

I am working on a php code as shown below which convert mp4 files (present in the in_folder) to mp3 (and send it into out_folder)

<?php
foreach ($mp4_files as $f)
{

    $parts = pathinfo($f);
    switch ($parts['extension'])
    {
        case 'mp4' :
            $filePath = $src_dir . DS . $f;
            system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination . DS . $parts['filename'] . '.mp3', $result);

            break;

        case 'mp3' :
            // copy($f, $destination. DS . $parts['filename']. '.' . $parts['extension']);
            copy($f, $mp3_processed . DS . $parts['filename'] . '.mp3');
            copy($f, $destination . DS . $parts['filename'] . '.mp3');
            break;
    }
}
?>


<script src="https://connect.soundcloud.com/sdk/sdk-3.3.2.js"></script>
<script>
SC.initialize({
  client_id: 'YOUR_CLIENT_ID',
  redirect_uri: 'http://example.com/callback'
});

var getTracks = function() { return SC.get('/me/tracks'); };

// connect and update track data
SC.connect().then(getTracks).then(function(tracks){
  SC.put('/tracks' + tracks[0].id, {
    track: {
      description: 'This track was recorded in Berlin',
      genre: 'Electronic'
    }
  });
});
</script>

My next step is to upload the mp3 files(which is in the out_folder) to a specific soundcloud account. I added some script (where php code finish) from soundcloud developers guide as a 1st step in order to upload songs from the songs directory to soundcloud.

Problem Statement:

I am wondering what changes I should in the script code above so that we can make upload songs/audio file from the out_folder directory to any specific soundcloud account. Any pointers will be highly appreciated.

回答1:

After you write the file with copy(), something like this should work:

$blob=file_get_contents($destination . DS . $parts['filename'] . '.mp3');
$title='THE_TITLE';
echo "
<script>
var aFileParts = ['$blob']; // an array with binary data  
var audioBlob = new Blob(aFileParts, {type : 'audio/mpeg'}); 

SC.upload({
  file: audioBlob , // a Blob of your WAV, MP3...
  title: '$title'
});
</script>
";

You may need to escape $blob, of course