2
0

sndfile-metadata-set.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ** Copyright (C) 2008-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. ** Copyright (C) 2008-2010 George Blood Audio
  4. **
  5. ** All rights reserved.
  6. **
  7. ** Redistribution and use in source and binary forms, with or without
  8. ** modification, are permitted provided that the following conditions are
  9. ** met:
  10. **
  11. ** * Redistributions of source code must retain the above copyright
  12. ** notice, this list of conditions and the following disclaimer.
  13. ** * Redistributions in binary form must reproduce the above copyright
  14. ** notice, this list of conditions and the following disclaimer in
  15. ** the documentation and/or other materials provided with the
  16. ** distribution.
  17. ** * Neither the author nor the names of any contributors may be used
  18. ** to endorse or promote products derived from this software without
  19. ** specific prior written permission.
  20. **
  21. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  23. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38. #include <time.h>
  39. #include <sndfile.h>
  40. #include "common.h"
  41. #define BUFFER_LEN (1 << 16)
  42. static void usage_exit (const char *progname, int exit_code) ;
  43. static void missing_param (const char * option) ;
  44. static void read_localtime (struct tm * timedata) ;
  45. static int has_bext_fields_set (const METADATA_INFO * info) ;
  46. int
  47. main (int argc, char *argv [])
  48. { METADATA_INFO info ;
  49. struct tm timedata ;
  50. const char *progname ;
  51. const char * filenames [2] = { NULL, NULL } ;
  52. int k ;
  53. /* Store the program name. */
  54. progname = program_name (argv [0]) ;
  55. /* Check if we've been asked for help. */
  56. if (argc < 3 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
  57. usage_exit (progname, 0) ;
  58. /* Clear set all fields of the struct to zero bytes. */
  59. memset (&info, 0, sizeof (info)) ;
  60. /* Get the time in case we need it later. */
  61. read_localtime (&timedata) ;
  62. for (k = 1 ; k < argc ; k++)
  63. { char tmp [20] ;
  64. if (argv [k][0] != '-')
  65. { if (filenames [0] == NULL)
  66. filenames [0] = argv [k] ;
  67. else if (filenames [1] == NULL)
  68. filenames [1] = argv [k] ;
  69. else
  70. { printf ("Error : Already have two file names on the command line and then found '%s'.\n\n", argv [k]) ;
  71. usage_exit (progname, 1) ;
  72. } ;
  73. continue ;
  74. } ;
  75. #define HANDLE_BEXT_ARG(cmd, field) \
  76. if (strcmp (argv [k], cmd) == 0) \
  77. { k ++ ; \
  78. if (k == argc) missing_param (argv [k - 1]) ; \
  79. info.field = argv [k] ; \
  80. continue ; \
  81. } ;
  82. HANDLE_BEXT_ARG ("--bext-description", description) ;
  83. HANDLE_BEXT_ARG ("--bext-originator", originator) ;
  84. HANDLE_BEXT_ARG ("--bext-orig-ref", originator_reference) ;
  85. HANDLE_BEXT_ARG ("--bext-umid", umid) ;
  86. HANDLE_BEXT_ARG ("--bext-orig-date", origination_date) ;
  87. HANDLE_BEXT_ARG ("--bext-orig-time", origination_time) ;
  88. HANDLE_BEXT_ARG ("--bext-coding-hist", coding_history) ;
  89. HANDLE_BEXT_ARG ("--bext-time-ref", time_ref) ;
  90. #define HANDLE_STR_ARG(cmd, field) \
  91. if (strcmp (argv [k], cmd) == 0) \
  92. { k ++ ; \
  93. if (k == argc) missing_param (argv [k - 1]) ; \
  94. info.field = argv [k] ; \
  95. continue ; \
  96. } ;
  97. HANDLE_STR_ARG ("--str-comment", comment) ;
  98. HANDLE_STR_ARG ("--str-title", title) ;
  99. HANDLE_STR_ARG ("--str-copyright", copyright) ;
  100. HANDLE_STR_ARG ("--str-artist", artist) ;
  101. HANDLE_STR_ARG ("--str-date", date) ;
  102. HANDLE_STR_ARG ("--str-album", album) ;
  103. HANDLE_STR_ARG ("--str-license", license) ;
  104. /* Following options do not take an argument. */
  105. if (strcmp (argv [k], "--bext-auto-time-date") == 0)
  106. { snprintf (tmp, sizeof (tmp), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
  107. info.origination_time = strdup (tmp) ;
  108. snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
  109. info.origination_date = strdup (tmp) ;
  110. continue ;
  111. } ;
  112. if (strcmp (argv [k], "--bext-auto-time") == 0)
  113. { snprintf (tmp, sizeof (tmp), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
  114. info.origination_time = strdup (tmp) ;
  115. continue ;
  116. } ;
  117. if (strcmp (argv [k], "--bext-auto-date") == 0)
  118. { snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
  119. info.origination_date = strdup (tmp) ;
  120. continue ;
  121. } ;
  122. if (strcmp (argv [k], "--str-auto-date") == 0)
  123. { snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
  124. info.date = strdup (tmp) ;
  125. continue ;
  126. } ;
  127. printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
  128. usage_exit (progname, 1) ;
  129. } ;
  130. /* Find out if any of the 'bext' fields are set. */
  131. info.has_bext_fields = has_bext_fields_set (&info) ;
  132. if (filenames [0] == NULL)
  133. { printf ("Error : No input file specificed.\n\n") ;
  134. exit (1) ;
  135. } ;
  136. if (filenames [1] != NULL && strcmp (filenames [0], filenames [1]) == 0)
  137. { printf ("Error : Input and output files are the same.\n\n") ;
  138. exit (1) ;
  139. } ;
  140. if (info.coding_history != NULL && filenames [1] == NULL)
  141. { printf ("\n"
  142. "Error : Trying to update coding history of an existing file which unfortunately\n"
  143. " is not supported. Instead, create a new file using :\n"
  144. "\n"
  145. " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n"
  146. "\n",
  147. progname) ;
  148. exit (1) ;
  149. } ;
  150. sfe_apply_metadata_changes (filenames, &info) ;
  151. return 0 ;
  152. } /* main */
  153. /*==============================================================================
  154. ** Print version and usage.
  155. */
  156. static void
  157. usage_exit (const char *progname, int exit_code)
  158. { printf ("\nUsage :\n\n"
  159. " %s [options] <file>\n"
  160. " %s [options] <input file> <output file>\n"
  161. "\n",
  162. progname, progname) ;
  163. puts (
  164. "Where an option is made up of a pair of a field to set (one of\n"
  165. "the 'bext' or metadata fields below) and a string. Fields are\n"
  166. "as follows :\n"
  167. ) ;
  168. puts (
  169. " --bext-description Set the 'bext' description.\n"
  170. " --bext-originator Set the 'bext' originator.\n"
  171. " --bext-orig-ref Set the 'bext' originator reference.\n"
  172. " --bext-umid Set the 'bext' UMID.\n"
  173. " --bext-orig-date Set the 'bext' origination date.\n"
  174. " --bext-orig-time Set the 'bext' origination time.\n"
  175. " --bext-coding-hist Set the 'bext' coding history.\n"
  176. " --bext-time-raf Set the 'bext' Time ref.\n"
  177. "\n"
  178. " --str-comment Set the metadata comment.\n"
  179. " --str-title Set the metadata title.\n"
  180. " --str-copyright Set the metadata copyright.\n"
  181. " --str-artist Set the metadata artist.\n"
  182. " --str-date Set the metadata date.\n"
  183. " --str-album Set the metadata album.\n"
  184. " --str-license Set the metadata license.\n"
  185. ) ;
  186. puts (
  187. "There are also the following arguments which do not take a\n"
  188. "parameter :\n\n"
  189. " --bext-auto-time-date Set the 'bext' time and date to current time/date.\n"
  190. " --bext-auto-time Set the 'bext' time to current time.\n"
  191. " --bext-auto-date Set the 'bext' date to current date.\n"
  192. " --str-auto-date Set the metadata date to current date.\n"
  193. ) ;
  194. puts (
  195. "Most of the above operations can be done in-place on an existing\n"
  196. "file. If any operation cannot be performed, the application will\n"
  197. "exit with an appropriate error message.\n"
  198. ) ;
  199. printf ("Using %s.\n\n", sf_version_string ()) ;
  200. exit (exit_code) ;
  201. } /* usage_exit */
  202. static void
  203. missing_param (const char * option)
  204. {
  205. printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option) ;
  206. exit (1) ;
  207. } /* missing_param */
  208. /*==============================================================================
  209. */
  210. static int
  211. has_bext_fields_set (const METADATA_INFO * info)
  212. {
  213. if (info->description || info->originator || info->originator_reference)
  214. return 1 ;
  215. if (info->origination_date || info->origination_time || info->umid || info->coding_history || info->time_ref)
  216. return 1 ;
  217. return 0 ;
  218. } /* has_bext_fields_set */
  219. static void
  220. read_localtime (struct tm * timedata)
  221. { time_t current ;
  222. time (&current) ;
  223. memset (timedata, 0, sizeof (struct tm)) ;
  224. #if defined (HAVE_LOCALTIME_R)
  225. /* If the re-entrant version is available, use it. */
  226. localtime_r (&current, timedata) ;
  227. #elif defined (HAVE_LOCALTIME)
  228. {
  229. struct tm *tmptr ;
  230. /* Otherwise use the standard one and copy the data to local storage. */
  231. if ((tmptr = localtime (&current)) != NULL)
  232. memcpy (timedata, tmptr, sizeof (struct tm)) ;
  233. }
  234. #endif
  235. return ;
  236. } /* read_localtime */