获取远程视频的时长

时间:2019-11-17 12:00:01

标签: android

我正在尝试获取远程视频的时长(范围从5秒到10分钟),我目前正在使用远程服务器上的4个视频进行测试,我可以获取1或2个视频的时长,而其他视频抛出IllegalArgumentException setDataSource failed: status = 0xFFFFFFFF。相同的视频并不总是会发生这种情况,如果我再次运行代码,则某些视频将成功返回持续时间(即使先前运行代码时失败),而其他返回持续时间的视频将失败,基本上是随机的,有些失败,并且有些成功,并且每次启动都会改变结果,例如:

运行代码1

  • 视频1成功返回时长
  • 视频2失败,出现IllegalArgumentException
  • 视频3失败,出现IllegalArgumentException
  • 视频4成功返回时长

运行代码2

  • 视频1因IllegalArgumentException而失败
  • 视频2失败,出现IllegalArgumentException
  • 视频3成功返回时长
  • 视频4成功返回时长

运行代码#3

  • 视频1因IllegalArgumentException而失败
  • 视频2失败,出现IllegalArgumentException
  • 视频3成功返回时长
  • 视频4失败,出现IllegalArgumentException

等等。

代码:

    private void getFTP_DurationArray() {

        //Get File Names From FTP
        FTPClient client = new FTPClient();
        try {
            client.connect(myFTP_Server);
            client.enterLocalPassiveMode();
            client.login(myFTP_Username, myFTP_Password);
            client.changeWorkingDirectory(skyVideos_OPEN_Active_ftpDir);
            FTPFile[] files = client.listFiles();

            //SORT BY TIMESTAMP
            Arrays.sort(files,
                    Comparator.comparing((FTPFile remoteFile) -> remoteFile.getTimestamp()).reversed());

            file_path_remote = myDomain + "/" + skyVideos_OPEN_Active;

            for (FTPFile file : files) {

                if (file.isFile())
                {
                    //GetDuration
                    try{
                        //NOTE: must not use https
                        mmr = new FFmpegMediaMetadataRetriever();
                        mmr.setDataSource(myDomain_no_https + "/" + skyVideos_OPEN_Active+"/"+file.getName());
                        long duration =Long.parseLong(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
                        duration=duration/1000;
                        long minute=duration/(60);
                        long second=duration-(minute*60);
                        mmr.release();

                        strDuration = String.format("%02d:%02d" , minute, second);

                        Log.d("LOG", "strDuration " +strDuration + " -> "+file_name_remote);

                        arrListStr_Duration.add(strDuration);
                        stringArr_Duration = new String[arrListStr_Duration.size()];
                        stringArr_Duration = arrListStr_Duration.toArray(stringArr_Duration);

                    } catch (IllegalStateException e) {
                        Log.d("LOG", "IllegalStateException " + " -> "+e + " "+file_name_remote);

                    }catch (IllegalArgumentException e) {
                        Log.d("LOG", "IllegalArgumentException " + " -> "+e +" "+file_name_remote);

                    }
                }
            }
            client.disconnect();

        } catch (IOException e) {
            e.printStackTrace();

            Log.d("LOG", "IOException: " +e);
        }
    }

0 个答案:

没有答案