mattcole
User
 Fresh Boarder
| Posts: 4 |   |
|
FFMPEG wrapper - 2007/04/19 13:57
Hi, Hopefully this is in the right place...
Firstly thanks very much for the work you have done in providing the wrapper.
My question is to do with using the wrapper to convert one type of video to another. I can do this using ffmpeg on the command line, but am pretty unfamiliar with the source of ffmpeg and so am unsure of how to do this through its API. Is this something that I am able to do through your .Net wrapper at this stage?
Thanks, Matt
|
|
|
| | No public write access. Please register. |
Daniel
Admin
 Admin
| Posts: 44 |  |
|
Re:FFMPEG wrapper - 2007/04/19 17:15
My question is to do with using the wrapper to convert one type of video to another. I can do this using ffmpeg on the command line, but am pretty unfamiliar with the source of ffmpeg and so am unsure of how to do this through its API. Is this something that I am able to do through your .Net wrapper at this stage?
Hi,
With this version, it might work with some formats but you'd have to code the defaults parameters which you can only find by searching the original ffmpeg source code.
It would be probably easier to dig the info from: http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html
-daniel
Post edited by: Daniel, at: 2007/04/19 17:16
Post edited by: Daniel, at: 2007/04/19 17:16
|
|
|
| | No public write access. Please register. |
mattcole
User
 Fresh Boarder
| Posts: 4 |   |
|
Re:FFMPEG wrapper - 2007/04/19 23:45
Hi, Thanks for your reply. Specifically, I was looking to turn mpegs into FLVs. Do you think I will be able to do that with this version of the library, and if not, would you mind pointing me in the direction of what I would need to extend to get it to work?
Thanks again for your help, Matt
|
|
|
| | No public write access. Please register. |
Daniel
Admin
 Admin
| Posts: 44 |  |
|
Re:FFMPEG wrapper - 2007/04/20 20:30
IIRC ffmpeg has problems with flv (seeking? not sure, anymore).
Moreover, the binaries include with the lib wasn't compile to support mp3 audio.
So this is probably not a very good solution for transcoding to flv.
I think this app fixes these problems: http://www.rivavx.com/?encoder
-daniel
|
|
|
| | No public write access. Please register. |
mattcole
User
 Fresh Boarder
| Posts: 4 |   |
|
Re:FFMPEG wrapper - 2007/04/23 07:16
Hi, Thanks, but I was hoping to build my own library for dynamic calling from another app, rather than just using an application to do it manually.
I have a copy of the ffmpeg dlls that I am able to use to transcode mpeg to flv with sound intact, if I put those dlls in the directory of your library will it use them? I'm still not sure of the syntax I would need to use to get your library to convert a video from one format to another sorry or I would just test it out.
Thanks, Matt
|
|
|
| | No public write access. Please register. |
Daniel
Admin
 Admin
| Posts: 44 |  |
|
Re:FFMPEG wrapper - 2007/04/23 15:48
if I put those dlls in the directory of your library will it use them?
It should (might have to rebuild).
I've just sent you some code that I've written a couple of months ago to transcode to flv.
It wasn't very efficient but it seemed to work, if I remember correctly.
-daniel
|
|
|
| | No public write access. Please register. |
mattcole
User
 Fresh Boarder
| Posts: 4 |   |
|
Re:FFMPEG wrapper - 2007/04/24 02:56
Hi Daniel,
Thanks very much for the code. It seems to almost work, I get a green bar to the right side of the video and it seems to have sped the video up a bit too. Also, there is no sound but you mentioned that would be the case. I've tried to rebuild AvDnCPP with the lib and include files from the r7215-shared-win build of FFMPEG, it builds ok but doesn't seem to be able to be used by the samples you have included or the sample you sent me last night.
Thank you for the time you have spent helping me, would you consider building a version of your library with the version of FFMPEG that is able to convert to FLV including sound if I was to pay you for your time?
If you're too busy or whatever, thanks again for your help and I'll keep trying to get it going myself.
Thanks again, Matt
|
|
|
| | No public write access. Please register. |
WillSahatdjian
User
 Fresh Boarder
| Posts: 2 |   |
|
Re:FFMPEG wrapper - 2007/04/25 18:40
Here is a quick and dirty "wrapper" (hah) that I wrote to process uploads and convert to FLV. The output is hard-coded to be FLV and its all synchronous (which may be a problem with larger videos or slower hardware).
I started to make a proper assembly out of it but I am not sure if I have tested it since. Use it at your own risk 
EDIT: The Ifw.Windows namespace was pasted in below before, but it was awful to read in one big listing. Also, you can download them here: http://creativestandards.com/code/ffmpeg-helper.zip
Ifw.Windows.cs:
| Code: |
namespace Ifw.Windows
{
public class CommandLine
{
public enum ReturnOutput
{
Text = 0,
Error = 1
}
public static string Run(string path)
{
return Run(path, ReturnOutput.Error);
}
public static string Run(string path, ReturnOutput returnType)
{
string result;
ProcessStartInfo ProObj = new ProcessStartInfo("cmd.exe");
ProObj.UseShellExecute = false;
ProObj.RedirectStandardOutput = true;
ProObj.RedirectStandardInput = true;
ProObj.RedirectStandardError = true;
try
{
Process proc = Process.Start(ProObj);
StreamReader sOut = (returnType == ReturnOutput.Text) ? proc.StandardOutput : proc.StandardError;
StreamWriter sIn = proc.StandardInput;
sIn.WriteLine(path);
sIn.Close();
proc.Close();
result = sOut.ReadToEnd();
sOut.Close();
}
catch { result = "Error"; }
return result;
}
}
}
|
Ifw.Cgd.Video.cs:
| Code: |
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using Ifw.Windows;
namespace Ifw.Cgd.Video
{
public enum Format
{
WindowsMedia = 0,
QuickTime = 1,
Flash = 2
}
public class MetaData
{
public string FilePath;
public Format Format;
public string Duration = "N/A";
public double FPS = 0;
public short Width;
public short Height;
}
/// <summary>
/// ffmpeg Wrapper to generate video and thumbnails
/// </summary>
public class Encoder
{
public string EncoderPath
{
get
{
if (string.IsNullOrEmpty(_EncoderPath))
throw new ArgumentNullException("Encoder path must be set");
return _EncoderPath;
}
set
{
_EncoderPath = value;
}
}
public string InputPath
{
get
{
if (string.IsNullOrEmpty(_InputPath))
throw new ArgumentNullException("Input path must be set");
return _InputPath;
}
set
{
_InputPath = value;
}
}
public string OutputPath
{
get
{
if (string.IsNullOrEmpty(_OutputPath))
throw new ArgumentNullException("Output path must be set");
return _OutputPath;
}
set
{
_OutputPath = value;
}
}
public short Width
{
get
{
return _Width;
}
set
{
if (value < 2 || value % 2 != 0)
{
throw new ArgumentOutOfRangeException("Nonzero and Even Width requried");
}
_Width = value;
}
}
public short Height
{
get
{
return _Height;
}
set
{
if (value < 2 || value % 2 != 0)
{
throw new ArgumentOutOfRangeException("Nonzero and Even Height requried");
}
_Height = value;
}
}
private string _EncoderPath = null; //Ifw.Cgd.Config.Load().PrivateDataRoot + @"ffmpeg\ffmpeg"
private string _InputPath;
private string _OutputPath;
private short _Width = -1;
private short _Height;
/// <summary>
/// Set the initial path values for ffmpeg to use
/// </summary>
/// <param name="encoderPath">The path to the encoder (ffmpeg)
</param>
/// <param name="inputPath">The input file path</param>
/// <param name="outputPath">The output file path - use null
if retrieving metadata only</param>
public Encoder(string encoderPath, string inputPath, string outputPath)
{
this.InputPath = inputPath;
}
/// <summary>
/// Uses ffmpeg to write a jpeg thumbnail to a specified path
/// </summary>
public void WriteThumbnail()
{
if (this.Width == -1 || this.Height == -1)
{
throw new ArgumentException("Width and Height must be specified");
}
string dir = this.OutputPath.Substring(0,this.OutputPath.LastIndexOf(@"\"));
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string path = String.Format(@"{0} -y -i {1} -ss 00:00:01 -f mjpeg -vframes 1 -an -s {2}x{3} {4}
.jpg", this.EncoderPath, this.InputPath, this.Width, this.Height, this.OutputPath);
//appendix # A1
string result = CommandLine.Run(path);
if (result == "Error") throw new Exception("Error generating thumbnail");
if (result.IndexOf("Unknown format") > -1) throw new Exception("Error: unknown format");
}
/// <summary>
/// Uses ffmpeg to write a Flash Video File (FLV) to a
specified path
/// </summary>
public void WriteFLV()
{
if (this.Width == -1 || this.Height == -1)
{
throw new Exception("Width and Height must be specified");
}
string dir = this.OutputPath.Substring(0, this.OutputPath.LastIndexOf(@"\"));
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string path = String.Format(@"{0} -y -i {1} -ar 44100 -acodec mp3 -f flv -s {2}×{3} -an {4}.flv", this.EncoderPath, this.InputPath, this.Width, this.Height, this.OutputPath);
//appendix # A2
string result = CommandLine.Run(path);
if (result == "Error") throw new Exception("Error generating FLV file");
if (result.IndexOf("Unknown format") > -1) throw new Exception("Error: unknown format");
}
/// <summary>
/// Parses ffmpeg output string to extract video metadata.
/// If any expected delimiters are not present one of the
Split operations will likely throw an IndexOutOfRange exception.
/// ALWAYS use try...catch blocks with this
/// </summary>
/// <returns>A MetaData object with dimensions, duration, etc<
/returns>
public MetaData GetMetaData()
{
string result = CommandLine.Run(this.EncoderPath + " -i " + this.InputPath);
if (result == "Error") throw new Exception("Error retrieving metadata");
if (result.IndexOf("Unknown format") > -1) throw new Exception("Error: unknown format");
MetaData m = new MetaData();
result = Regex.Split(result, "Input #")[1];
result = Regex.Split(result, "Duration: ")[1];
m.Duration = Regex.Split(result, ",")[0];
//Video: flv, yuv420p, 320x256, 25.00 fps
string[] meta = Regex.Split(Regex.Split(Regex.Split(result, "Video:")[1], "fps")[0], ", ");
//example: flv, yuv420p, 320x256, 25.00
/* Use FourCC (codec) info to accurately determine types
try
{
switch (meta[0])
{
case "FLV":
this.Format = Format.Flash;
break;
default:
break;
}
}
catch { }
*/
//TODO: replace with more robust implementation above
string fileExt = Path.GetExtension(this.InputPath).ToLower();
switch (fileExt)
{
case ".flv":
m.Format = Format.Flash;
break;
case ".mov":
m.Format = Format.QuickTime;
break;
case ".wmv":
m.Format = Format.WindowsMedia;
break;
default:
m.Format = Format.Flash;
break;
}
try
{
string[] dimensions = meta[2].Split('x');
m.Width = Convert.ToInt16(dimensions[0]);
m.Height = Convert.ToInt16(dimensions[1]);
}
catch { throw new Exception("Video dimension data is required but not able to be read"); }
try
{
m.FPS = Convert.ToDouble(meta[3]);
}
catch { }
return m;
}
}
/* APPENDIX (command line reference)
*
* A1 : string path2 = this.EncoderPath + " -y -i " +
this.InputPath + @" -pix_fmt rgb16 -f rawvideo -ss 100 -vcodec png -
vframes 1 -s " + this.Width + "×" + this.Height + " -an " +
this.OutputPath + ".png";
* A2 : D:\ffmpeg\ffmpeg -y -i D:\capture-2.avi -f flv -t 00:05:
47 -s 980×822 -an D:\capture2.flv
*
* FFMPEG samples
* Generate Thumb
* X:\ffmpeg\ffmpeg -y -i X:\cgd\native\scanner.mov -pix_fmt
rgb16 -f rawvideo -ss 100 -vcodec png -vframes 1 -s 150×100 -an X:\
cgd\testpng.png
* Write same-quality FLV
* X:\ffmpeg\ffmpeg -i X:\cgd\flv\cgsociety.flv -ar 44100 -sameq
-f flv -s 320x256 X:\cgd\flv\cgs-out.flv
*/
}
|
|
|
|
| | No public write access. Please register. |
Daniel
Admin
 Admin
| Posts: 44 |  |
|
Re:FFMPEG wrapper - 2007/04/25 21:29
@matt
I've sent you a private reply.
@will
Without the code for Ifw.Windows, we can't try it.
It would be better if you can provide the code in a separate download (if you don't have an easy location and it's useful, we'll find you a place on this site).
-daniel
|
|
|
| | No public write access. Please register. |
|