xyzio

Archive for the ‘java’ Category

Batch converting videos to play on the Nexus 7 using FFMPEG

leave a comment »

How to batch convert videos to play on the Nexus 7 using FFMPEG in Python.

mypath = r'path_to_video_files'

from os import listdir
from os.path import isfile, join
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]

for file in onlyfiles:
    infile = '"' + mypath + file + '"'
    outfile = '"' + mypath + file + '.mp4' + '"'
    ffmpegpath = r'ffmpeg.exe'
    cmd = r' -i ' + infile + ' -acodec libmp3lame -vcodec libx264 -aspect 16:9 -s 800x480 -ab 128k -b 512k -f mp4 -strict experimental ' + outfile
    import os
    os.system(ffmpegpath + cmd)

Starting source:

http://stackoverflow.com/questions/8589976/how-to-convert-an-mkv-with-subtitles-to-something-nexus-one-friendly

Written by M Kapoor

September 29, 2014 at 3:01 pm

Posted in java

Tagged with

Passing a datastructure between Android activities as JSON

leave a comment »

Passing a data structure between Android activities as JSON using Gson.

In the sending Activity:

//Data
Droplet data = new Droplet();
//Create intent
Intent intent = new Intent(SendingClass.this, ReceivingClass.class);
//Create JSON object from data structure
Gson gson = new Gson();
String json = gson.toJson(data);
//Add JSON to intent
intent.putExtra("json", json);
//Start Activity with intent
startActivity(intent);

In the receiving Activity:

//Get string
String json = extras.getString("json","-1");
//Convert JSON to class
Gson gson = new Gson();
Droplet droplet = gson.fromJson(json, Droplet.class);

Where Droplet is defined here.

Written by M Kapoor

September 26, 2014 at 5:13 pm

Posted in java

Getting a JSON object from DigitalOcean in Java

leave a comment »

How to get a JSON object from DigitalOcean. This uses Gson.

Getting the HTTP object.

Inputs:

url: URL to access i.e. https://api.digitalocean.com/v2/droplets/
auth_key: Authorization key from DO.

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

    private static InputStream retrieveStream(String url, String auth_key) {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(url);

        getRequest.addHeader("Authorization","Bearer " + auth_key);
    try {
        HttpResponse getResponse = client.execute(getRequest);  
        statusCode = getResponse.getStatusLine().getStatusCode();

        HttpEntity getResponseEntity = getResponse.getEntity();
        return getResponseEntity.getContent();
    } 
    catch (IOException e) {
        getRequest.abort();
    }
    return null;
}

Converting the response into a Java object:

        InputStream stream = retrieveStream(url, auth_key);
        Reader reader = new InputStreamReader(stream);

        Gson gson = new Gson();
        Droplets response = gson.fromJson(reader, Droplets.class);

Where Droplets.class is implemented like this and Droplet.class is implemented like this.

Written by M Kapoor

September 23, 2014 at 3:46 pm

Posted in java