how to convert video in multiple bitrate using asp.net - c#

I want to play one video in different bitrates. Like i uploaded one video in 1080P resolution i want play that video in 720P, 480P, 360P, 240P, 144P etc.
I want this solution in asp.net using C#.
Like youtube provide the facility to watch video in different resolutions.
Please help me regarding this.
I tried the following code but not working:
using Softpae.Media;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
Job2Convert myJob = new Job2Convert();
MediaServer ms = new MediaServer();
myJob.pszSrcFile = "E:\\EhabVideoLibrary\\videos\\sinbad.mkv";
myJob.pszDstFile = "E:\\EhabVideoLibrary\\videos\\sinbad.mp4";
myJob.pszDstFormat = "mp4";
myJob.pszAudioCodec = "aac";
myJob.nAudioChannels = 2;
myJob.nAudioBitrate = -1;
myJob.nAudioRate = -1;
myJob.pszVideoCodec = "h264";
myJob.nVideoBitrate = -1;
myJob.nVideoFrameRate = -1;
myJob.nVideoFrameWidth = -1;
myJob.nVideoFrameHeight = -1;
bool ret = ms.ConvertFile(myJob);
}
}
}

You can use FFplay of the FFmpeg project. (ffmpeg.org) With FFmpeg it's possible to encode and transcode almost every codec in the resolution you want. In this thread is the use of a command line application using C# described.
I've never tried it, but there are also libraries provided for .NET using FFmpeg like this:
ffmpegdotnet.codeplex.com
intuitive.sk/fflib
Success with it!
Here is an example code using ffmpeg (I tested it under Win7 VM):
using System;
namespace ConsoleApplication_FFmpegDemo
{
class Program
{
static void Main(string[] args)
{
string inputVideo = #"C:\Users\Public\Videos\Sample Videos\Wildlife.wmv";
string outputVideo = #"C:\Users\Public\Videos\Sample Videos\Wildlife.mp4";
string ffmpegArg = string.Format("-i \"{0}\" -vf scale=320:240 \"{1}\"", inputVideo, outputVideo);
string ffmpegPath = #"C:\Portable\ffmpeg-win32-static\bin\ffmpeg.exe";
FFmpegTask ffmpegTask = new FFmpegTask(ffmpegPath, ffmpegArg);
ffmpegTask.Start();
Console.ReadLine();
}
}
}
And the FFmpegTask.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication_FFmpegDemo
{
public class FFmpegTask
{
public Process process = new Process();
public FFmpegTask(string ffmpegPath, string arguments)
{
process.StartInfo.FileName = ffmpegPath;
process.StartInfo.Arguments = arguments;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
}
public bool Start()
{
return process.Start();
}
}
}

Related

Programatically Switch Off/On Wi-Fi from WPF App

I have a WPF app that has a control(checkbox /toggle switch) . I want to turn Wi-Fi On/Off by using those buttons. I have tried the following code but it doesnt seem to help
I am using Windows 10 and Visual Studio 2015
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// string name = "Hello World";
}
static void Enable(string interfaceName)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.Start();
}
static void Disable(string interfaceName)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.Start();
}
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
string interfaceName = "Local Area Connection";
Disable(interfaceName);
}
}
}
I went through the following link with the first answer but there is no help .
I need some help so that I can programatically turn off/On Wi-Fi with the click of a button.
You can turn on/off Wi-Fi by changing software radio state (not hardware radio state) by Native Wifi API. Using some codes of Managed Wifi API project, I wrote a sample.
using System;
using System.Linq;
using System.Runtime.InteropServices;
using NativeWifi;
public static class WlanRadio
{
public static string[] GetInterfaceNames()
{
using (var client = new WlanClient())
{
return client.Interfaces.Select(x => x.InterfaceName).ToArray();
}
}
public static bool TurnOn(string interfaceName)
{
var interfaceGuid = GetInterfaceGuid(interfaceName);
if (!interfaceGuid.HasValue)
return false;
return SetRadioState(interfaceGuid.Value, Wlan.Dot11RadioState.On);
}
public static bool TurnOff(string interfaceName)
{
var interfaceGuid = GetInterfaceGuid(interfaceName);
if (!interfaceGuid.HasValue)
return false;
return SetRadioState(interfaceGuid.Value, Wlan.Dot11RadioState.Off);
}
private static Guid? GetInterfaceGuid(string interfaceName)
{
using (var client = new WlanClient())
{
return client.Interfaces.FirstOrDefault(x => x.InterfaceName == interfaceName)?.InterfaceGuid;
}
}
private static bool SetRadioState(Guid interfaceGuid, Wlan.Dot11RadioState radioState)
{
var state = new Wlan.WlanPhyRadioState
{
dwPhyIndex = (int)Wlan.Dot11PhyType.Any,
dot11SoftwareRadioState = radioState,
};
var size = Marshal.SizeOf(state);
var pointer = IntPtr.Zero;
try
{
pointer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(state, pointer, false);
var clientHandle = IntPtr.Zero;
try
{
uint negotiatedVersion;
var result = Wlan.WlanOpenHandle(
Wlan.WLAN_CLIENT_VERSION_LONGHORN,
IntPtr.Zero,
out negotiatedVersion,
out clientHandle);
if (result != 0)
return false;
result = Wlan.WlanSetInterface(
clientHandle,
interfaceGuid,
Wlan.WlanIntfOpcode.RadioState,
(uint)size,
pointer,
IntPtr.Zero);
return (result == 0);
}
finally
{
Wlan.WlanCloseHandle(
clientHandle,
IntPtr.Zero);
}
}
finally
{
Marshal.FreeHGlobal(pointer);
}
}
public static string[] GetAvailableNetworkProfileNames(string interfaceName)
{
using (var client = new WlanClient())
{
var wlanInterface = client.Interfaces.FirstOrDefault(x => x.InterfaceName == interfaceName);
if (wlanInterface == null)
return Array.Empty<string>();
return wlanInterface.GetAvailableNetworkList(Wlan.WlanGetAvailableNetworkFlags.IncludeAllManualHiddenProfiles)
.Select(x => x.profileName)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
}
}
public static void ConnectNetwork(string interfaceName, string profileName)
{
using (var client = new WlanClient())
{
var wlanInterface = client.Interfaces.FirstOrDefault(x => x.InterfaceName == interfaceName);
if (wlanInterface == null)
return;
wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
}
}
}
Check available interface names by GetInterfaceNames and then call TurnOn/TurnOff with one of the names. According to MSDN, it should require administrator priviledge but it doesn't on my environment.
SUPPLEMENT
I added two more methods to this class. So the sequence will be something like this.
Get existing Wi-Fi interface names by GetInterfaceNames.
Select an interface and turn it on by TurnOn.
Get profile names associated to available Wi-Fi networks through the interface by GetAvailableNetworkProfileNames.
Select a profile and connect to the network by ConnectNetwork.
After finished using the network, turn the interface off by TurnOff.
You could use the device library from windows universal apps.
Documentation:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.wifi.aspx
Microsoft sample:
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples
In order to use this library with WPF application you could add
< TargetPlatformVersion > 8.0< / TargetPlatformVersion >
to your .csproj file between
< PropertyGroup>.... < /PropertyGroup>

How to execute a scheduled task with “schtasks” without opening a new command line window using C# process?

I am trying to run a scheduled task from C# without opening a new command line window, using the following code without any success (it prompts a window every time I use it)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
Process p1 = new Process();
p1.StartInfo.FileName = #"C:\Windows\System32\schtasks.exe";
p1.StartInfo.Verb = "runas";
p1.StartInfo.Arguments = "/run /tn CCleaner";
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.UseShellExecute = false;
p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p1.StartInfo.CreateNoWindow = true;
p1.Start();
p1.Close();
}
catch (Exception ex)
{
}
}
}
}
How could I solve this problem ?
Thank you so much for your attention
You can use a library that provides access to Task Scheduler API.
For example using http://taskscheduler.codeplex.com/
using (var ts = new TaskService())
{
Task task = ts.GetTask("My task");
task.Run();
}

print pdf in both sides

So, i'm using the PDFSharp lib, and, I whant to print a pdf on both sides....
My printer is a hp2055dn I have tryed all things... btw... my code is that:
using PdfSharp.Pdf.Printing;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace impressao
{
class Program
{
static void Main(string[] args)
{
PdfFilePrinter.AdobeReaderPath = #"C:\Program Files (x86)\Adobe\Acrobat 8.0\Acrobat\Acrobat.exe";
//PdfFilePrinter.AdobeReaderPath = #"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";
PrinterSettings settings = new PrinterSettings();
settings.Collate = false;
settings.Duplex = Duplex.Vertical;
PrintDialog printerDialog = new PrintDialog();
printerDialog.AllowSomePages = false;
printerDialog.ShowHelp = false;
printerDialog.PrinterSettings = settings;
printerDialog.AllowPrintToFile = true;
printerDialog.PrinterSettings.PrintToFile = true;
DialogResult result = printerDialog.ShowDialog();
if (result == DialogResult.OK)
{
PdfFilePrinter printer = new PdfFilePrinter("0.pdf", settings.PrinterName);
try
{
printer.Print();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
}
If have a another way to make this, another lib, or, another settings, or another dll... please tell me
if is possible to make a another program in C or C++ and I just pass the parameters to print... I don't know...
I'm needing a lot of it, and urgently :(
Thanks
Alexandre
I found the solution, I have created a new printer and seted on defaults with print in both sides...

Printing a PDF in C#

I am making a C# Windows application for printing a PDF.
When I open the application, it only opens the Acrobat Reader window and no more printing. Is there anything I have missed in the function of Print()?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
namespace PrintDocumentsApplication
{
public partial class PrintForm : Form
{
public PrintForm()
{
InitializeComponent();
}
private void printPdfButton_Click(object sender, EventArgs e)
{
String File = #"C:\Documents and Settings\larasasasrylo\Desktop\QRCODE_DEMO\test.pdf";
String Printer = "\\vhssadasdasoftaweafs\\HP Color LaserJet 5550 PCL 6";
Print(File, Printer);
}
public static bool Print(string file, string printer)
{
try
{
Process.Start(
Registry.LocalMachine.OpenSubKey(
#"SOFTWARE\Microsoft\Windows\CurrentVersion" +
#"\App Paths\AcroRd32.exe").GetValue("").ToString(),
string.Format("/h /t \"{0}\" \"{1}\"", file, printer));
return true;
}
catch { }
return false;
}
}
}
you try this
Process process = new Process();
process.StartInfo.FileName = pathToPdfOrDocFile;
process.UseShellExecute = true;
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = "\"" + printerName + "\"";
process.Start();
process.WaitForInputIdle();
process.Kill();

How to run external program via a C# program?

How do I run an external program like Notepad or Calculator via a C# program?
Maybe it'll help you:
using(System.Diagnostics.Process pProcess = new System.Diagnostics.Process())
{
pProcess.StartInfo.FileName = #"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
}
Use System.Diagnostics.Process.Start
Hi this is Sample Console Application to Invoke Notepad.exe ,please check with this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Demo_Console
{
class Program
{
static void Main(string[] args)
{
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "Notepad.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
}
}
}
For example like this :
// run notepad
System.Diagnostics.Process.Start("notepad.exe");
//run calculator
System.Diagnostics.Process.Start("calc.exe");
Follow the links in Mitchs answer.

Categories