Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
libvlc_media_get_duration始终返回0_C_Libvlc - Fatal编程技术网

libvlc_media_get_duration始终返回0

libvlc_media_get_duration始终返回0,c,libvlc,C,Libvlc,我正在用纯C编写一个媒体播放器,我正在使用libvlc。目前我正在开发媒体库,我正在编写目录行者和媒体文件解析器。它可以很好地处理各种元数据,如艺术家或相册等,但libvlc_media_get_duration始终返回0。我什么都试过了,到处都找遍了,但还是没法成功。有人能帮我吗 代码如下: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <vlc/vlc.h>

我正在用纯C编写一个媒体播放器,我正在使用libvlc。目前我正在开发媒体库,我正在编写目录行者和媒体文件解析器。它可以很好地处理各种元数据,如艺术家或相册等,但libvlc_media_get_duration始终返回0。我什么都试过了,到处都找遍了,但还是没法成功。有人能帮我吗

代码如下:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vlc/vlc.h>
#include <stdarg.h>
#include <stdbool.h>
#include <dirent.h>
#include <sys/stat.h>

void strcopy(char **dst, const char *src) {
  unsigned int size = strlen(src);
  *dst = (char *) realloc(*dst, sizeof(char) * (size + 1));
  strncpy(*dst, src, size);
  *(*dst+size) = 0;
}

void strconcat(char **dst, int n, ...) {

  va_list args;
  unsigned int count = 0;

  // Count
  va_start(args, n);
  for (unsigned short i = 0; i < n; i++)
    count += strlen(va_arg(args, char*));
  va_end(args);

  // Allocate
  *dst = (char *) realloc(*dst, sizeof(char) * (count+1));
  unsigned int cursor = 0;
  va_start(args, n);
  for(unsigned short i = 0; i < n; i++) {
    char *src = va_arg(args, char*);
    strncpy((*dst+cursor), src, strlen(src));
    cursor += strlen(src);
    *(*dst+cursor) = 0;
  }
  va_end(args);

}

void /* Process tags and add file to database */
__db_add_file(libvlc_instance_t *inst, const char *url, bool compute_hash) {

  // Create new media
  libvlc_media_t *media = libvlc_media_new_path(inst, url);
  libvlc_media_parse(media);

  if (libvlc_media_is_parsed(media)) {

    printf("%s\n", url);
    printf("%llu\n", libvlc_media_get_duration(media));
  }

  libvlc_media_release(media);

}

void /* Walker over directory */
__db_dir_walker(libvlc_instance_t *inst, const char *dir_url, bool compute_hash) {

  // Build base path
  char *base_url = NULL;
  if (dir_url[strlen(dir_url)-1] != '/')
    strconcat(&base_url, 2, dir_url, "/");
  else
    strcopy(&base_url, dir_url);

  // Create necessary variables
  struct dirent *entry;
  DIR *dir;
  struct stat fs;

  // Try to open dir
  if (!(dir = opendir(dir_url))) return;

  while (entry = readdir(dir)) {

    // Strip parent entries
    if ((strcmp(".", entry->d_name) == 0) ||
        (strcmp("..", entry->d_name) == 0)) continue;

    char *dir_full_path = NULL;
    strconcat(&dir_full_path, 2, base_url, entry->d_name);

    if (stat(dir_full_path, &fs) < 0) return;

    if (S_ISDIR(fs.st_mode)) { // Process directory

      __db_dir_walker(inst, dir_full_path, compute_hash);

    } else { // Process media file

      __db_add_file(inst, dir_full_path, compute_hash);
    }

  }

  // Free memory
  closedir(dir);
}

void
db_scan_directory(const char *dir_url, bool compute_hash) {

  // Try to open target dir
  if (!opendir(dir_url)) return;

  // Preload vlc instance for tag data retrieving
  libvlc_instance_t *inst = libvlc_new(0, NULL);

  // Walk over directory
  __db_dir_walker(inst, dir_url, compute_hash);

  // Free resources
  libvlc_release(inst);
}

int main () {

  db_scan_directory("/media/storage/music/Blur/", false);

  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效strcopy(字符**dst,常量字符*src){
无符号整数大小=strlen(src);
*dst=(char*)realloc(*dst,sizeof(char)*(size+1));
strncpy(*dst、src、尺寸);
*(*dst+尺寸)=0;
}
无效strconcat(字符**dst,整数n,…){
va_列表参数;
无符号整数计数=0;
//计数
va_启动(args,n);
for(无符号短i=0;id_name)==0)||
(strcmp(“…”,entry->d_name)==0)继续;
char*dir\u full\u path=NULL;
strconcat(&dir\u full\u path,2,base\u url,entry->d\u name);
if(stat(dir\u full\u path,&fs)<0)返回;
if(S_ISDIR(fs.st_模式)){//进程目录
__db_dir_walker(inst、dir_full_path、compute_hash);
}else{//处理媒体文件
__db\u add\u文件(inst、dir\u full\u path、compute\u hash);
}
}
//空闲内存
closedir(dir);
}
无效的
数据库扫描目录(常量字符*目录url,布尔计算哈希){
//尝试打开目标目录
如果(!opendir(dir_url))返回;
//预加载vlc实例以检索标记数据
libvlc_instance_t*inst=libvlc_new(0,NULL);
//浏览目录
__db_dir_walker(inst、dir_url、compute_hash);
//免费资源
libvlc_发布(inst);
}
int main(){
db_scan_目录(“/media/storage/music/Blur/”,false);
返回0;
}

谢谢大家!

如果有人也想知道这个问题的答案,这里是:

您需要播放才能获得持续时间。


感谢来自Videolan论坛的Jean Baptiste Kempf。

最好的方法可能是调用或其异步计数器部分


调用
libvlc\u media\u parse()
后,您的元数据(包括持续时间)将被归档。

是,我选中了。一切都很好,正如我之前所说,它可以完美地处理元数据,
media
不是空的,
url
也是正确的。持续时间是我唯一不能正确提取的东西。如果你愿意帮忙,我可以把整个代码放在这里;)瓦尔格林说一切都很好。我让我的程序更简单了——摆脱了walker函数,留下了
main
,并将vlc代码放入其中——仍然没有结果。非常感谢你帮助我。似乎我应该开始为libvlc寻找替代品,例如gstreamer或xine。