I'm actually trying to launch my Unix code on my Mac.
I know that I have to build ffmpeg on my Mac.
I used a Github script for that :
Script
My code :
package test1;
import it.sauronsoftware.jave.*;
import test1.MyFFMPEGExecutableLocator;
import java.io.*;
public class Test1 {
public static void main (String[] zero) throws IllegalArgumentException, InputFormatException, EncoderException{
File source = new File("/Users/Mokeight/Desktop/Test2.mp3");
File target = new File("/Users/Mokeight/Desktop/target2.wav");
System.out.println("START");
AudioAttributes audio = new AudioAttributes();
EncodingAttributes attrs = new EncodingAttributes();
//codec de wav
audio.setCodec("pcm_s16le");
attrs.setFormat("wav");
attrs.setAudioAttributes(audio);
//System.out.println(new MyFFMPEGExecutableLocator().toString());
Encoder encoder = new Encoder(new MyFFMPEGExecutableLocator());
encoder.encode(source, target, attrs);
System.out.println("END");
}
}
My second class is :
package test1;
import it.sauronsoftware.jave.FFMPEGLocator;
public class MyFFMPEGExecutableLocator extends FFMPEGLocator{
#Override
protected String getFFMPEGExecutablePath() {
// TODO Auto-generated method stub
String path = "/Users/Mokeight/Desktop/ffmpeg_mac/ffmpeg-build/workspace/bin/ffmpeg" ;
return path;
}
}
When i'm running that , i have this error :
START
Exception in thread "main" it.sauronsoftware.jave.EncoderException: Metadata:
at it.sauronsoftware.jave.Encoder.encode(Encoder.java:863)
at it.sauronsoftware.jave.Encoder.encode(Encoder.java:713)
at test1.Test1.main(Test1.java:27)
Please, help
Thanks !!
Related
Does anyone know a repo that shows what a simple HelloWorld java or scala code would look like to build the jar that could be executed using the AWS SageMaker SparkJarProcessing class?
Readthedocs (https://sagemaker-examples.readthedocs.io/en/latest/sagemaker_processing/spark_distributed_data_processing/sagemaker-spark-processing.html) mentions:
"In the next example, you’ll take a Spark application jar (located in ./code/spark-test-app.jar)..."
My question is how does the source code look like for this jar (spark-test-app.jar)?
I tried building a simple Java project jar
src>com.test>HW.java:
public class HW {
public static void main(String[] args) {
System.out.printf("hello world!");
}
}
and running it inside SageMaker Notebook conda_python3 kernel using
from sagemaker.spark.processing import SparkJarProcessor
from sagemaker import get_execution_role
role = get_execution_role()
print(role)
spark_processor = SparkJarProcessor(
base_job_name="sm-spark-java",
framework_version="3.1",
role=role,
instance_count=2,
instance_type="ml.m5.xlarge",
max_runtime_in_seconds=1200,
)
spark_processor.run(
submit_app="./SparkJarProcessing-1.0-SNAPSHOT.jar",
submit_class="com.test.HW",
arguments=["--input", "abc"],
logs=True,
)
But end up getting an error:
Could not execute HW class.
Any sample source code for spark-test-app.jar would be highly appreciated!
To answer your question, the source code of that class looks like:
package com.amazonaws.sagemaker.spark.test;
import java.lang.invoke.SerializedLambda;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.Options;
import org.apache.spark.sql.Dataset;
import org.apache.commons.cli.CommandLine;
import org.apache.spark.sql.types.DataTypes;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import org.apache.spark.sql.SparkSession;
public class HelloJavaSparkApp
{
public static void main(final String[] args) {
System.out.println("Hello World, this is Java-Spark!");
final CommandLine parsedArgs = parseArgs(args);
final String inputPath = parsedArgs.getOptionValue("input");
final String outputPath = parsedArgs.getOptionValue("output");
final SparkSession spark = SparkSession.builder().appName("Hello Spark App").getOrCreate();
System.out.println("Got a Spark session with version: " + spark.version());
System.out.println("Reading input from: " + inputPath);
final Dataset salesDF = spark.read().json(inputPath);
salesDF.printSchema();
salesDF.createOrReplaceTempView("sales");
final Dataset topDF = spark.sql("SELECT date, sale FROM sales WHERE sale > 750 SORT BY sale DESC");
topDF.show();
final Dataset avgDF = salesDF.groupBy("date", new String[0]).avg(new String[0]).orderBy("date", new String[0]);
System.out.println("Collected average sales: " + StringUtils.join((Object[])new List[] { avgDF.collectAsList() }));
spark.sqlContext().udf().register("double", n -> n + n, DataTypes.LongType);
final Dataset saleDoubleDF = salesDF.selectExpr(new String[] { "date", "sale", "double(sale) as sale_double" }).orderBy("date", new String[] { "sale" });
saleDoubleDF.show();
System.out.println("Writing output to: " + outputPath);
saleDoubleDF.coalesce(1).write().json(outputPath);
spark.stop();
}
private static CommandLine parseArgs(final String[] args) {
final Options options = new Options();
final CommandLineParser parser = (CommandLineParser)new BasicParser();
final Option input = new Option("i", "input", true, "input path");
input.setRequired(true);
options.addOption(input);
final Option output = new Option("o", "output", true, "output path");
output.setRequired(true);
options.addOption(output);
try {
return parser.parse(options, args);
}
catch (ParseException e) {
new HelpFormatter().printHelp("HelloScalaSparkApp --input /opt/ml/input/foo --output /opt/ml/output/bar", options);
throw new RuntimeException((Throwable)e);
}
}
}
At the same time, I have created a simple example that shows how to run an hello world app here. Please note that I have run that example on Amazon SageMaker Studio Notebooks, using the Data Science 1.0 kernel.
Hope this helps.
On the first run, I want to copy the given File to a new location with a new file name.
Every subsequent run should overwrite the same destination file created during first run.
During first run, the destination file does not exist. Only the directory exists.
I wrote the following program:
package myTest;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
public class FileCopy {
public static void main(String[] args) {
TestFileCopy fileCopy = new TestFileCopy();
File sourceFile = new File("myFile.txt");
fileCopy.saveFile(sourceFile);
File newSourceFile = new File("myFile_Another.txt");
fileCopy.saveFile(newSourceFile);
}
}
class TestFileCopy {
private static final String DEST_FILE_PATH = "someDir/";
private static final String DEST_FILE_NAME = "myFileCopied.txt";
public void saveFile(File sourceFile) {
URL destFileUrl = getClass().getClassLoader().getResource(DEST_FILE_PATH
+ DEST_FILE_NAME);
try {
File destFile = Paths.get(destFileUrl.toURI()).toFile();
FileUtils.copyFile(sourceFile, destFile);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
}
However, this throws null pointer exception on the following line:
File destFile = Paths.get(destFileUrl.toURI()).toFile();
What am I missing?
Directory someDir is directly under my project's root directory in eclipse.
Both source files myFile.txt and myFile_Another.txt exists directly under my project's root directory in eclipse.
I used this and it works as I am expecting:
public void saveFile1(File sourceFile) throws IOException {
Path from = sourceFile.toPath();
Path to = Paths.get(DEST_FILE_PATH + DEST_FILE_NAME);
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
}
Using Java nio.
When I attempt to run the program that takes the metadata and prints it from an mp3 file, I am returned with an "Exception in thread "main" java.lang.NullPointerException at project.mp3MetaData.main(musicdj.java:18)". For this class you need the jid3lib jar. How do I avoid this exception and do I need to pass any variables through the tags at the bottom?
package 1234;
import java.io.File;
import java.io.IOException;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v1;
public class mp3MetaData {
public static void main(String[] args) throws IOException, TagException {
// TODO Auto-generated method stub
File sourceFile = new File("/Users/JohnSmith/Desktop/MusicTester/1234.mp3");
MP3File mp3file = new MP3File(sourceFile);
ID3v1 tag = mp3file.getID3v1Tag();
System.out.println(tag.getAlbum());
System.out.println(tag.getAlbumTitle());
System.out.println(tag.getTitle());
System.out.println(tag.getComment());
}
}
Any help will be greatly appreciated.
Your MP3 file might not contain an ID3 tag. So check whether tag is null or not, before using it. Something like this:
public static void main(String[] args) throws IOException, TagException
{
File sourceFile = new File("/Users/JohnSmith/Desktop/MusicTester/1234.mp3");
final MP3File mp3file = new MP3File(sourceFile);
final ID3v1 tag = mp3file.getID3v1Tag();
if (null == tag)
{
System.out.println("No ID3 tag found!");
}
else
{
System.out.println(tag.getAlbum());
System.out.println(tag.getAlbumTitle());
System.out.println(tag.getTitle());
System.out.println(tag.getComment());
}
}
I`m trying to import jcurses library to IntelliJ but get an error.
File -> Project Structure -> Modules -> import jar file.
Then in code:
import jcurses.widgets.Window;
class main {
public static void main(String[] args){
Window win = new Window(800, 600, true, "test");
win.setVisible(true);
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at jcurses.system.InputChar.<clinit>(InputChar.java:25)
at jcurses.widgets.Window.<clinit>(Window.java:209)
at main.main(main.java:7)
Caused by: java.lang.RuntimeException: couldn't find jcurses library
at jcurses.system.Toolkit.getLibraryPath(Toolkit.java:121)
at jcurses.system.Toolkit.<clinit>(Toolkit.java:37)
Could someone point out where is my mistake?
Thanks in advance!
The answer is that dll must be in the same directory as jar file. Thanks to everyone!
Libray download url
https://sourceforge.net/projects/javacurses/files/javacurses/0.9.5/
The path of the library is in the root directory of the project
dynamically add the library
import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
public class LoadLibrary {
public static void main(String cor[]) throws Exception {
// Cargando librerias necesarias
loadLibraryJCourses();
}
public static void loadLibraryJCourses() throws Exception{
loadDynamicLibrary(new File("lib/bin/jcourses/").getAbsolutePath(),"libjcurses64");
}
private static void addLibraryPath(String pathToAdd) throws Exception {
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
//get array of paths
final String[] paths = (String[]) usrPathsField.get(null);
//check if the path to add is already present
for (String path : paths) {
if (path.equals(pathToAdd)) {
return;
}
}
//add the new path
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
private static void loadDynamicLibrary(String pathLibraryDirectory, String libraryName) throws Exception{
File pathLibraryFile = new File(pathLibraryDirectory);
addLibraryPath(pathLibraryFile.getAbsolutePath());
System.loadLibrary(libraryName);
}
}
I am writing a code in Java to upload videos to youtube from mu application.
My code is :
package com.youtube.video;
import java.io.IOException;
import java.net.URL;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.FormUploadToken;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
public class YouTubeController {
public static YouTubeService service;
public void init() {
if (service == null) {
service = new YouTubeService("mail#gmail.com", "A...A");
String username = "mail#gmail.com";
String password = "xxxxxxxxx";
try {
service.setUserCredentials(username, password);
} catch (AuthenticationException ae) {
ae.printStackTrace();
}
}
}
static String token;
static String formUrl;
public static void setFormDetails() throws IOException {
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
String videoTitle = "this is a title";
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
mg.setTitle(new MediaTitle());
mg.setPrivate(false);
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("");
mg.getTitle().setPlainTextContent(videoTitle);
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent(videoTitle);
URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
try {
FormUploadToken fut = service.getFormUploadToken(uploadUrl, newEntry);
token = fut.getToken();
System.out.println(">>>>>"+token);
formUrl = fut.getUrl();
} catch (ServiceException se) {
se.printStackTrace();
}
}
public static void main(String s[]) throws IOException {
YouTubeController yc = new YouTubeController();
yc.init();
yc.setFormDetails();
}
}
I have the following jars in lib folder :
gdata-client-1.0.jar
gdata-youtube-2.0.jar
gdata-core-1.0.jar
gdata-media-1.0.jar
google-collect-1.0.jar
guava-13.0.1.jar
mail.jar
activation.jar
But while running this piece of code, it is giving me the following error :
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet;
at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableTypes(AltFormat.java:399)
at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableXmlTypes(AltFormat.java:387)
at com.google.gdata.wireformats.AltFormat.<clinit>(AltFormat.java:49)
at com.google.gdata.client.Service.<clinit>(Service.java:558)
at com.zeta.video.YouTubeController.init(YouTubeController.java:23)
at com.zeta.video.YouTubeController.main(YouTubeController.java:66)
I tried all solutions over net, and all of them points out that this error is because i am missing some JAR files. But i am having all the JARs. Can someone help me with this issue?
It looks as though you are referencing both google-collections and guava. Since guava is a superset of google-collections you actually only need to reference guava. Remove the reference to google-collections and you should find the problem is resolved.