TglibSharp i cant write to the metadata of an mp3 song - c#

I have been trying to read/write metadata of mp3 files.I searched around a bit and found out i could use Taglibsharp in order to do it.Reading the metadata works perfectly however when i try to write in order to change a tag for example the title nothing happens.Also there is no documentation for taglibsharp so if someone who has previously used it knows what i can do , please help me.Thanks in advance!
Some code:
var tfile = TagLib.File.Create(songPath);
//Reading: works perfectly
string title = tfile.Tag.Title;
//writing: does nothing
tfile.Tag.Title = "A title";

You need to call the .Save() method:
using (var tfile = TagLib.File.Create(songPath))
{
tfile.Tag.Title = "A title";
tfile.Save();
}

Related

Kentico 12 (Portal) - Retrieve Media File Info from Media Selection in code behind

I'm creating a widget that would allow a user to select an image from the media library using the Media Selector control, and return both the image and the file's attributes (Title, alt text, etc).
I'm attempting to use the MediaFileInfoProvider method by providing the Site name, File Path, and Library folder, but I can't get it to return anything. I also can't find any documentation on this method for Kentico 12 - so I'm going based on what I could find for 8.2.
File Path Provided: /NAIT/media/nait-ca-content/about/0I3A6642_060519_Inst_Excellence_Nominees_2.jpg?ext=.jpg
Code:
MediaFileInfo mediaInfo1 = MediaFileInfoProvider.GetMediaFileInfo("NAIT", "about/0I3A6642_060519_Inst_Excellence_Nominees_2.jpg", "nait-ca-content");
string strFileTitle = mediaInfo1["FileTitle"].ToString();
There's no error using this code, nothing in the event log, but nothing returned either. I've tried a number of different ways to format the File Path, including the ones suggested in this post - so far none have worked.
Does anyone know if this method is still supported in Kentico 12? Or if there's something I'm missing?
By saying that you want to create an widget I assume that you are using Kentico 12 MVC. You can use the 'MediaFilesSelector' by adding the following property in your widget properties.
[EditingComponent(MediaFilesSelector.IDENTIFIER, Label = "Image", Order = 1)]
public IList<MediaFilesSelectorItem> Image { get; set; }
In your widget controller you can then use this to retrieve the media file GUID.
var fileGuid = GetProperties().Image?.FirstOrDefault()?.FileGuid ?? Guid.Empty
Then you can get the media file info by using the following code:
var mediaFile = MediaFileInfoProvider.GetMediaFileInfo(fileGuid, SiteContext.CurrentSiteName);

Retrieve Video Stream from Youtube Link

I am currently working on a project to retrieve mp3 files from the audio of YouTube videos.
I started my project with some basic parsing of youtube links and came up with a regex to extract the id.
private static string GetYoutubeID(string url)
{
var match = youtubeRegex.Match(url);
if(match.Success)
{
return match.Groups[0].Value ?? throw new ArgumentException("url");
}
else
{
throw new ArgumentException(url);
}
}
Then i am using the id in a request like so:
public static string GetResponse(string id)
{
using (WebClient client = new WebClient())
{
return client.DownloadString($"http://youtube.com/get_video_info?video_id={id}");
}
}
So now this call returns a string with tags like "status" and "errorcode". There is also one particular tag named "url_encoded_fmt_stream_map". In it I can find the urls to download the videos in various qualities.
For Exampe:
https%3A%2F%2Fr2---sn-h0jeened.googlevideo.com%2Fvideoplayback...
But my query seemed to fail and the result delivered me an empty "url_encoded_fmt_stream_map"-tag with "status=fail" and the "errorcode=150" which is "video unavailable".
From my understanding the libraries out there to help downloading youtube videos have some similar problems.
So my question is, is my method reliable? If so, what am I doing wrong?
And if it is not reliable, how can i reliably retrieve a link to a video stream?
I was able to fix my problem. After some more trying and searching I was able to change the request url to achieve a result.
For anyone interested:
https://www.youtube.com/get_video_info?video_id={id}
Can be changed to:
https://www.youtube.com/get_video_info?video_id={id}&el=detailpage
This returns a full answer for me and the error 150 does not occur anymore for me.

TagLib-Sharp - Retagging files

im about to create a small tool, which recreates all tags on my mp3 files.
Because they are in a mess, i want to remove all tags and recreate them with the correct values.
Doing so ive encountered the problem that im not able to set the tag values.
But the problem is, that im not able to set the tags. I have the following code:
File tagLibFile = File.Create(filePath);
tagLibFile.RemoveTags(TagLib.TagTypes.AllTags);
tagLibFile.Tag.Album = album;
tagLibFile.Tag.AlbumArtists = artists.ToArray();
tagLibFile.Tag.Track = track;
tagLibFile.Tag.Title = title;
tagLibFile.Tag.TitleSort = titleSort;
...
tagLibFile.Save();
The file is read out correctly. Then the tags are removed.
But after that setting the Tag does not work. The strings inside the tag are still null.
I havent seen a method like "tagLibFile.SetTag(Tag t)". The Tag is only available as a getter, but not a setter.
After that ive added some Frames, but that doesent have the effect of setting the tags. Maybe im using it the wrong way?
Hope you can help me out of this!
Kind regards,
SyLuS
I'm guessing that after removing tags, TagLib# (or TagLib, for that matter) does not create a new tag to hold information. However, when opening a file, it possibly does some checking and if the file doesn't have one, it creates a new tag.
Hence, as a workaround, you could save the file once after removing the tags, and then proceed to add new tag information.
File tagLibFile = File.Create(filePath);
tagLibFile.RemoveTags(TagLib.TagTypes.AllTags);
// Save the file once, so that Taglib Sharp takes care of creating any necessary tags when opening the file next time and dispose the file reference:
tagLibFile.Save();
tagLibFile.Dispose();
You can then proceed to editing the tags as you're already doing after opening the file again:
tagLibFile = File.Create(filePath);
tagLibFile.Tag.Album = album;
tagLibFile.Tag.AlbumArtists = artists.ToArray();
tagLibFile.Tag.Track = track;
tagLibFile.Tag.Title = title;
tagLibFile.Tag.TitleSort = titleSort;
// ...
Remember to save the file again, after you're done editing tags:
tagLibFile.Save();
I hope this helps. If you have any further questions, or the above code doesn't work still, feel free to comment. :)

Reading/Writing metadata of audio/video files

I need some help in reading/writing meta data inforamation of audio/vido file. I have searched alot but not find anything thing helpful. Taglib sharp is an open source library that provide help in reading/writing metadata. Using tag lib i'm able to edit some of values but not all like.
TagLib.File videoFile = TagLib.File.Create("test.mp4");
videoFile.Tag.Title = "Test";
videoFile.Tag.Comment = "Nothing";
but i'm unable to edit following properties like Author url, producers etc. How i edit these properties ??
I've never done this for video files before but I have for mp3 files. You can get access to those frames like this:
TagLib.File file = TagLib.File.Create(mp3FileName);
file.Tag.Title = "some title"; // you've got this
TagLib.Id3v2.Tag tag = (TagLib.Id3v2.Tag)file.GetTag(TagTypes.Id3v2);
tag.SetTextFrame("WOAR", "some url"); // WOAR = Official artist/performer webpage
file.Save();
You can find a list of the text frame identifiers at Wikipedia:
ID3v2 Frame Specification (Version 2.3)
I don't know if video files give you the same range of frames that ID3 does, though notice that Wikipedia also says (Implementation in non-mp3s and alternatives)
MP4 also allows the embedding of an ID3 tag, and this is widely supported.
So I would guess this also works for mp4 files like you're trying.
You will need to use AppleTag. This will work. For mp4 file you have to write value into dashbox. Like this:
TagLib.File videoFile = TagLib.File.Create("test.mp4");
TagLib.Mpeg4.AppleTag customTag = (TagLib.Mpeg4.AppleTag)f.GetTag(TagLib.TagTypes.Apple);
customTag.SetDashBox("Producer","Producer1", "value");
f.Save();
f.Dispose();
And you can get the value like this:
var tokenValue = customTag.GetDashBox("Producer", "Producer1");

How do you get the markup of a webpage in asp.net similar to php's get_file_contents

I was trying to do something similar to the PHP command get_file_contents to get the market up a webpage, but it has to be in ASP.net. I wasnt sure if i could just open the webpage via file, and then just read the stream into another file.
One thought iw as thinking was: How to read an entire file to a string using C#? but i wasnt sure if that was the route i would be needing to go or if someone else knew something that would do what i wanted.
Looking for the answer to the following pseudocode:
$test = "www.google.com";
$string = get_file_contents($test);
$filePointer = fopen("sample.txt", "w+");
$filePointer.write($string);
fclose($filePointer);
string html;
using (var client = new WebClient())
{
html = client.DownloadString("http://www.google.com");
}
Console.WriteLine(html);
figured this might be what i was looking at: http://www.devprise.com/2006/07/14/c-method-to-mimic-php-file_get_contents/ Not sure though.

Categories