m3u.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:4k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * m3u.c : M3U playlist export module
  3.  *****************************************************************************
  4.  * Copyright (C) 2004-2009 the VideoLAN team
  5.  * $Id: d3564d352e084934b5e83e995cca3eb274e42738 $
  6.  *
  7.  * Authors: Clément Stenac <zorglub@videolan.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_playlist.h>
  31. #include <vlc_input.h>
  32. #include <vlc_meta.h>
  33. #include <assert.h>
  34. /*****************************************************************************
  35.  * Local prototypes
  36.  *****************************************************************************/
  37. int Export_M3U ( vlc_object_t * );
  38. /*****************************************************************************
  39.  * Export_M3U: main export function
  40.  *****************************************************************************/
  41. static void DoChildren( playlist_t *p_playlist, playlist_export_t *p_export,
  42.                         playlist_item_t *p_root )
  43. {
  44.     int i, j;
  45.     /* Go through the playlist and add items */
  46.     for( i = 0; i< p_root->i_children ; i++)
  47.     {
  48.         playlist_item_t *p_current = p_root->pp_children[i];
  49.         assert( p_current );
  50.         if( p_current->i_flags & PLAYLIST_SAVE_FLAG )
  51.             continue;
  52.         if( p_current->i_children >= 0 )
  53.         {
  54.             DoChildren( p_playlist, p_export, p_current );
  55.             continue;
  56.         }
  57.         /* General info */
  58.         char *psz_uri = input_item_GetURI( p_current->p_input );
  59.         assert( psz_uri );
  60.         char *psz_name = input_item_GetName( p_current->p_input );
  61.         if( psz_name && strcmp( psz_uri, psz_name ) )
  62.         {
  63.             char *psz_artist = input_item_GetArtist( p_current->p_input );
  64.             if( psz_artist == NULL ) psz_artist = strdup( "" );
  65.             mtime_t i_duration = input_item_GetDuration( p_current->p_input );
  66.             if( psz_artist && *psz_artist )
  67.             {
  68.                 /* write EXTINF with artist */
  69.                 fprintf( p_export->p_file, "#EXTINF:%i,%s - %sn",
  70.                           (int)( i_duration / 1000000 ), psz_artist, psz_name);
  71.             }
  72.             else
  73.             {
  74.                 /* write EXTINF without artist */
  75.                 fprintf( p_export->p_file, "#EXTINF:%i,%sn",
  76.                          (int)( i_duration / 1000000 ), psz_name);
  77.             }
  78.             free( psz_artist );
  79.         }
  80.         free( psz_name );
  81.         /* VLC specific options */
  82.         vlc_mutex_lock( &p_current->p_input->lock );
  83.         for( j = 0; j < p_current->p_input->i_options; j++ )
  84.         {
  85.             fprintf( p_export->p_file, "#EXTVLCOPT:%sn",
  86.                      p_current->p_input->ppsz_options[j][0] == ':' ?
  87.                      p_current->p_input->ppsz_options[j] + 1 :
  88.                      p_current->p_input->ppsz_options[j] );
  89.         }
  90.         vlc_mutex_unlock( &p_current->p_input->lock );
  91.         fprintf( p_export->p_file, "%sn", psz_uri );
  92.         free( psz_uri );
  93.     }
  94. }
  95. int Export_M3U( vlc_object_t *p_this )
  96. {
  97.     playlist_t *p_playlist = (playlist_t*)p_this;
  98.     playlist_export_t *p_export = (playlist_export_t *)p_playlist->p_private;
  99.     msg_Dbg(p_playlist, "saving using M3U format");
  100.     /* Write header */
  101.     fprintf( p_export->p_file, "#EXTM3Un" );
  102.     DoChildren( p_playlist, p_export, p_export->p_root );
  103.     return VLC_SUCCESS;
  104. }