Getting duration of audio file C# - c#

I'm trying to get the duration for an audio file in c#.
I got the following code to work on my local machine but when I deploy it to a windows server 2008 box it always returns a duration of 0 for .avi and .wav files.
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia(filePath);
return mediaInfo.durationString
I have windows media player 11 installed on the server.
Any help would be most appreciated

I don't have experience with "WindowsMediaPlayerClass", but I have used TagLibSharp extensively, which is what I try to use for such things - it supports a pretty broad set of formats and that makes it's API much more usable that the built-in support in .NET
http://developer.novell.com/wiki/index.php/TagLib_Sharp
Quite possibly simply using TagLibSharp will solve your problems.
(Note that taglibsharp is maintained by the banshee project, but is hosted # novell for apparently historical reasons. I don't know about the details, but that can make contacting the devs a little confusing)

To retrieve duration info, media file must be opened first.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd564790(v=vs.85).aspx#Y656
To retrieve the duration for files that are not in the user's library, you must wait for Windows Media Player to open the file; that is, the current OpenState must equal MediaOpen. You can verify this by handling the AxWindowsMediaPlayer._WMPOCXEvents_OpenStateChange event or by periodically checking the value of AxWindowsMediaPlayer.openState.
The link also has sample codes for C#.
If you don't like to play the video, you may use the IWMPMedia class:
http://johndyer.name/post/Retreiving-the-duration-of-a-WMV-in-C.aspx

My guess is that the media player isn't installed (it's not by default on server SKUs).
Did you try installing the desktop experience package?

Related

Edit local media file metadata information

I managed to download audio file from a remote source, and I want to change it's metadata, like Author, Title, Album.
I save it locally so it's easily accessible and editable, but I can't find anything to edit it's metadata.
I've tried:
Android.Media.MediaMetadataEditor but the error is pretty straightforward:
This class is obsoleted in this android platform
I'm using latest SDK (28) and API, for android Pie. And it is also stated on android developer documentation
This class was deprecated in API level 21.
Use MediaMetadata instead together with MediaSession.
I really searched for some example, and I didn't found anything useful, most of cases are with playlist.
I don't get how MediaSession can replace in any manner the MediaMetadataEditor, since MediaSession
Allows interaction with media controllers, volume keys, media buttons, and transport controls.
A MediaSession should be created when an app wants to publish media playback information or handle media keys
Instead MediaMetadata seems what I need because
Contains metadata about an item, such as the title, artist, etc.
And its Builder class is promising.
I'm sure that just reading that pages I should be able to understand how to do it, but after hours of trying, it seems I'm not experienced enough to learn from that documentation.
Is there an example to understand it?
Thanks
I found a solution outside android Library.
taglib-sharp seems working on android project, and it can be installed via nuget package, the name is TagLibSharp
Really easy to use:
TagLib.File my_file = TagLib.File.Create(path_to_my_file);
string[] performers = new string[1]{"My Performer"}
my_file.Tag.Title = "The real title";
my_file.Tag.Performers = performers;
my_file.Save();
Easy.

DirectShow.Net no combination of filters but fine in GSpot

I'm trying to use the DirectShot.Net wrapper from "http://directshownet.sourceforge.net/" to extract frames from a number of video files.
Whenever I run the DxScan sample app. I get a "No combination of intermediate filters could be found to make the connection." This happens for WMV files, MP4 files, AVI files. Any media I point at the sample app.
If I open any of the videos using the GSpot codec tool and ask it to render the graph, it does so without problem. So the machine is definitely capable of playing the content.
I'm on Win 7 64 bit. The same error is thrown targeting x64, x86 or Auto. Including running Visual Studio as an Administrator.
I've modified the sample code to try and find the correct pin containing the video media type as suggested by Romain R below.
I'm using:
IEnumPins epins;
capFilter.EnumPins(out epins);
IntPtr fetched = Marshal.AllocCoTaskMem(4);
IPin[] pins = new IPin[1];
while (epins.Next(1, pins, fetched) == 0)
{
PinInfo pinfo;
pins[0].QueryPinInfo(out pinfo);
IEnumMediaTypes mtypes;
pins[0].EnumMediaTypes(out mtypes);
AMMediaType[] types = new AMMediaType[1];
while(mtypes.Next(1, types, new IntPtr()) == 0){
var majorType = types[0].majorType;
if (majorType == MediaType.Video)
{
//This is never reached
}
}
}
For MP4 files, it's never hitting the commented line above. However for WMV files, the demo will now run correctly.
Presumably, this is because it isn't finding an appropriate MP4 file filter. Which is obscure as the content will play fine in windows media player.
Is this a likely cause?
DxScan sample is building a filter graph in an unreliable way, in particular is makes an assumption that the first pin it grabs from the source filter supplied for a media file is a video pin. It is not always the case. A better graph building approach should be working out fine. You might want to step through to find out at what line you have an error. Most likely, it is going to be connection of Sample Grabber Filter input pin.
UPD. Note that it might so happen that original filter has no input pins, it requires additional filters in between, so called parser/demultiplexer filters which convert streams into video frames and audio samples. DirectShow API offers Render* methods to assist in graph building in terms of suggesting required links of this chain and direct connection DxScan is doing might or might not work out.
That is, DxScan might be not the best sample to start from, MSDN samples/reference for native API are perhaps better for taking off the ground with DirectShow.
Your playground and primary research and API exploration tool should be GraphEdit from Windows SDK (GraphStudio, or GraphStudioNext as alternate improved options), not a DirectShow.NET wrapper library sample.
Which is obscure as the content will play fine in windows media player.
WMP does not use DirectShow for playback.

Is there an Adobe equivalent to the Java deployment.properties file?

Just as the title states, is there an Adobe equivalent to the Java deployment.properties file?
I am writing a c# application to test installations of application in our network. The top three on my boss's list are java, flash, and reader. I need to be able to find out what versions of each application are installed on a machine for the reports im going to generate (force the user to update/etc).
I know i can check version number and confirm the ability of IE to access my JRE by checking "\Sun\Java\Deployment\deployment.properties". What file would I check to confirm the same for adobe reader and flash?
Thanks in advance for any help given or links provided to more info.
edit: I need to do this from the browser.
This is not a very clean solution, but since the only "official" way seems to be to check it from the Windows registries perhaps this will help:
We know that the flash files are located are in the (windows directory)\system32\Macromed\Flash (or SysWow64\Macromed\Flash on 64 bit systems).
Each time a flash updates it keeps the track of the progress in the log files. Depending on the flash version you will either have a) install.log (very old versions of flash) or b) FlashInstall.log
a) If you browse through the file you see various entires and one type goes like this:WriteRegStr: "HKEY_CURRENT_USER\SOFTWARE\Macromedia\FlashPlayer" "FlashPlayerVersion"="10.0.45.2".
Now you can just go through that file bottom-top and match the "FlashPlayerVersion"= string to get the most recent version.
However, this is for a really old versions of flash and the install.log file never got deleted from this directory, so make sure you check for the FlashInstall.log too!
b) Use a similar approach, except the new install logs don't keep the "WriteRegStr" information. Now you can instead look for the dll file name itself, for example my last update created an install log 0009 [I] 00000014 C:\WINDOWS\system32\Macromed\Flash\NPSWF32_11_5_502_146.dll, meaning my flash version is 11.5.502.146
another options are to
check the plugin core files creation date and compare with the versions release dates (quite unreliable in case someone somehow manages to install an older version)
check the actual property of the NPSWF[..version..].dll file. You can see all the complete and precise version details in the "Version" tab. however, I don't know how to access the rightclick->properties from inside a script, so you'll have to find out by yourself if you decide to go for this option
ask the unicorns

Write/Read file in my iPhone app Shared File

I wrote an app on my iPhone. It's a more portable and smaller version of my pc software. I activated the File Sharing feature on my app so now I can transfer files through iTunes. But I want my pc software to be able to read or write files to that shared folder on my iPhone without having to do it manually through iTunes.
I have big constraints:
I can't use a Jailbroken iPhone/iPod/iPad
The vast majority of my customers don't have Internet access (It's a farm management software so even cellular are not available in some area)... :(
I heard there is a way using Manzana and MobileDevice.dll (itunesmobiledevice.dll). I don't really know how to use these dll. I tried to use Manzana a little but I can't access my folder since it's not a jailbroken iPhone.. Can someone help me with a little bit of code example?
Or is there other ways to make my iPhone app communicate with my C# application using the USB cable without internet access or Wifi?
mobiledevice.codeplex.com. This project should let you send and retrieve files from the phone
I'd suggest seeing if you can use the iTunes scripting interface. Add the COM reference iTunes 1.1 Type Library to a project and you can control many parts of itunes automatically. I can't find the documentation for it, but you can play around with the library and see if there is something to access the file sharing section.
Here's a decent introduction to using it:
http://www.codeproject.com/Articles/7723/Controlling-iTunes-through-COM

Working way to make video from images in C#

Does anybody have a known reliable way to create a video from a series of image files? Before you mod me down for not searching for the answer before posting the question, and before you fire off a simple message like "use FFMPEG," read the rest of this message.
I'm trying to create a video, it doesn't matter too much what format as long as it's widely supported, from a series of images (.jpg, .bmp, etc.). My platform is Windows Server 2008, 64-bit. If I can make the video from within my C# program, that's great, but I'm not averse to writing a series of image files to a directory and then firing off an external program to make a video from those images.
The only constraints are: it must work on my Windows Server 2008 system, and be scriptable. That is, no GUI programs that require operator intervention.
I found a number of similar questions on StackOverflow, and have tried several of the solutions, all with varying degrees of frustration and none with anything like success.
FFMPEG looks like a great program. Maybe it is, on Linux. The two Windows builds I downloaded are broken. Given this command line:
ffmpeg -r 1 -f image2 -i jpeg\*.jpg video.avi
One of the builds reads the images and then crashes due to data execution prevention. The other reads the first file and then spits out an error message that says "cannot find suitable codec for file jpeg/image2.jpg". Helpful, that. In any case, FFMPEG looks like a non-starter under Windows.
One answer to a previous posting recommended Splicer . It looks like pretty good code. I compiled the samples and tried to run, but got some cryptic error message about a file not found. It looks like a COM class isn't registered. I suppose I need to install something (DirectShow, maybe, although I thought that was already installed?). Depending on what's required, I might have a difficult time justifying its installation on a server. ("What? Why do you need that on a server?")
Another answer suggested the AviFile library from Code Project. That looks simple enough: a wrapper around the Windows AviFile subsystem. Except that the AVI files the package creates appear to have all of the frames, but only the first frame shows when I play the AVI in Windows Media Player. Well, that and if you try to create a compressed video, the program throws an exception.
So, I'm left wondering if there is a good, reliable way to do what I want: on a Windows system, create an AVI or other common video file format from a series of images, either through a .NET API or using an external program. Any help?
After working with it a while and taking a look at x264 and VideoLan, I went back to Splicer. It turns out that the cryptic error message was due to an error in my code.
It looks like Splicer will do what I want: programmatically create videos from a series of images.
Thanks to all who responded.
You can use VideoLan and I'd recommend it.
I've had direct experience in a C# application with VideoLan doing these two things:
Embedding a VLC viewer in my C# application (there are 3-4 C# "wrappers" for the VLC veiwer).
Using vlc.exe in a separate Process by sending it command-line arguments.
The fact that VideoLan has a command-line interface is a great thing. And VLC supports a command-line option that disables any visual element; so the VLC GUI doesn't pop up and neither does a command-line window. Thus, in a C# application you can farm out the video-related work to the VLC client. C# has the Process class which can manage your vlc.exe instances for you. It ends up being a pretty neat solution.
The handling of video on Windows (encoding and playback) is a real quagmire. And .NET isn't going to help you at all - you're always going to be either PInvoking/COM-interoping native APIs or using a .NET wrapper that someone else has built to do the same.
Anyway, I can't say I've used it, but a good bet seems to be the x264 library that is part of the videolan project. That gives you a self-contained C library that you can PInvoke and you don't have to worry about installing and DirectShow garbage on your server. It'll generate H.264 format video which is playable in Flash as I understand it.
I've used mplayer's mencoder to create AVI (with codecs msmpeg4/mp3, readable by default with Windows Media Player) from TGA files on linux like this:
mencoder mf://*.tga -mf fps=25:type=tga \
-audiofile /tmp/test.mp3 -oac copy \
-of lavf -ovc lavc \
-lavcopts vcodec=msmpeg4v2:mbd=2:mv0:trell:cbp:last_pred=3:vbitrate=3000 \
-o /tmp/test.avi
I know mplayer does have a Windows version.
Instead of just going through all the hassle, you could coppy all the images that you want to be included in your video to the clipboard and have it imported into Windows Live Movie Maker (dynamically, of course!) and have it compile for you!
All of this CAN be done through code, but this way, you don't have to create it yourself. As you said that the platform was Windows, and ALL Windows comps have Windows Live Movie Maker installed, so it shouldn't be a problem for anyone using your application unless
a) They uninstalled Windows Live Movie Maker
b) They are using a computer running on a Windows Version more than 8 or 9 years old...

Categories

Resources